Printing with the Java 1.4 API

Example 13-3 is a further subclass of our Julia set component, which once again overrides the print( ) method and this time uses the Java 1.4 Printing API to produce the printed output shown in Figure 13-3. In addition, it defines a save( ) method that uses the Java 1.4 Printing API to print the image of the Julia set directly to a PostScript file. The actual printing of the image is done by the PrintableComponent class inherited from the previous example: what is different in this example is the way the Printable object is sent to a printer.

A Julia set printed with the Java 1.4 API

Figure 13-3. A Julia set printed with the Java 1.4 API

The print( ) and save( ) methods start by obtaining an appropriate PrintService object that represents a printer or print engine that can print to a file. Both methods then call the printToService( ) method, which uses the inherited PrintableComponent class to print the Julia set to the PrintService. One interesting feature of this method is its use of a PrintJobListener to monitor the progress of the print job and display its current status in a dialog box.

Example 13-3. JuliaSet3.java

package je3.print; import javax.swing.*; import java.awt.print.*; // Java 1.4 can use the 1.2 Printable API import javax.print.*; // Java 1.4 API import javax.print.event.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; import java.io.*; /** * This class ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.