The JDK 1.1 API puts your program in the driver’s seat: you
decide what to print and when to print it. But first, you have to let
the user pick a printer, which you can do by calling the
Toolkit method getPrinterJob( )
. This pops up a platform-specific print chooser dialog,
and if the user picks a printer, you get back a
PrintJob
object (otherwise you get back null).
Your program is in charge of
pagination (breaking the data into pages)
and drawing each page onto a print buffer. How? For each page you
want to print, call the
PrintJob
’s getGraphics( )
method to retrieve a Graphics
object.
Use it as you will; any of its draw or fill methods will draw, not to
the screen, but onto paper. Your best bet is to pass it to your
paint( )
method, if you have one. This is one of the few places where you do
call paint( )
directly. When the page is done,
call the Graphics
object’s dispose( )
method. When the whole print job is
done, call the PrintJob
’s end( )
method, and you’re
finished -- the data is on its way to the printer.
Here’s a little program that displays a simple graphical
component called a DemoGFXCanvas
.
When you click the Print button at the
bottom, the program prints the contents of the
DemoGFXCanvas
(this is shown graphically in Figure 12-6). When you click on the Print button in the
main window, the printer dialog shown at the bottom of the figure
appears. Example 12-5 is the code that makes it all
happen. (The push button and the addActionListener
code will be explained in Chapter 13; suffice it to
say that this causes an action to be performed when the button is
pressed.)
Example 12-5. PrintDemoGfx (JDK 1.1 version)
import java.awt.*; import java.awt.event.*; import javax.swing.*; /** PrintDemoGfx -- Construct and print a GfxDemoCanvas. JDK1.1 VERSION. */ public class PrintDemoGfx1_1 { /** Simple demo main program. */ public static void main(String[] av) { final JFrame f = new JFrame("Printing Test Dummy Frame"); // Construct the object we want to print. Contrived: // this object would already exist in a real program. final GfxDemoCanvas thing = new GfxDemoCanvas(500, 300); f.getContentPane( ).add(thing, BorderLayout.CENTER); JButton printButton = new JButton("Print"); f.getContentPane( ).add(printButton, BorderLayout.SOUTH); printButton.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { PrintJob pjob = Toolkit.getDefaultToolkit( ).getPrintJob(f, "Printing Test", null); if (pjob == null) return; // user cancelled // Fetch the Print Graphics object Graphics pg = pjob.getGraphics( ); // Now (drum roll please), ask "thing" to paint itself // on the printer, by calling its paint( ) method with // a Printjob Graphics instead of a Window Graphics. thing.paint(pg); pg.dispose( ); // end of this page pjob.end( ); // end of print job. } }); f.pack( ); f.setVisible(true); } }
One limitation of the 1.1 API is that it offers no way to print without a screen connection for the GUI-based printer dialog, so you can’t use the 1.1 API in a background job or cron job on Unix, or in a service on other platforms. For that, use the Java 2 API.
3.147.59.198