Java Reference
In-Depth Information
CHAPTER 12
Printing
The Graphics and Graphics2D objects represent a “drawing surface”; in Chapter
11, Graphics , we saw examples of using both the screen and an off-screen buffer
as drawing surfaces. Printing in Java is simply a matter of obtaining a Graphics
object that uses a printer as a drawing surface. Once you have a Graphics object,
you can print text and draw graphics to the printer just as you do onscreen. The
only trick is in obtaining the Graphics object for a printer. Java 1.1 introduced one
API for doing this, and then Java 1.2 introduced a new and different API. * This
chapter includes examples of printing components and multipage documents
using both APIs.
Printing with the Java 1.1 API
Example 12-1 harks back to the “scribble” programs of Chapter 10, Graphical User
Inter faces ; it is a GUI component that allows the user to scribble with the mouse.
The example stores the coordinates of the mouse scribbles, and when the user
clicks the Pr int button, it prints the scribble out. This example uses the AWT (not
Swing) and the Java 1.1 Printing API. The printScribble() method shows the
steps required to use this API. The actual printing occurs in the paint() method,
which is the method that also does onscreen drawing.
Example 12−1: ScribblePrinter1.java
package com.davidflanagan.examples.print;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* A "scribble" application that remembers the scribble and allows the user
* to print it. It displays an AWT API and uses the Java 1.1 printing API.
**/
* Then Java 1.3 added enhancements to the Java 1.1 API, in the form of the JobAttributes and PageAt-
tributes classes. And Java 1.4 is expected to enhance the Java 1.2 Printing API.
Search WWH ::




Custom Search