O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required