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

Example 13-2 is a listing of JuliaSet2. This subclass of Example 13-1 displays a different Julia set (pictured in Figure 13-2) and overrides the print( ) method, to demonstrate printing out the set using the Java 1.2 API. Like its superclass, JuliaSet2 should be tested using the ShowBean program.

A Julia set printed with the Java 1.2 API

Figure 13-2. A Julia set printed with the Java 1.2 API

The key to this example is the inner class PrintableComponent , which implements the java.awt.print.Printable interface to print a component and a title for that component. Pay attention to the return value of the Printable.print( ) method: the Printable interface can represent multipage documents, and uses its return value to tell the printing system when the last page of the document has been reached.

The java.awt.print package also defines an interface named Pageable , which is useful for defining multipage documents for which the number of pages is known in advance. It allows different pages of the document to be printed using different PageFormat objects, which allows for different margins, orientations, and so forth. Pageable documents are not demonstrated in this chapter.

Example 13-2. JuliaSet2.java

package je3.print; import javax.swing.*; import java.awt.*; import java.awt.print.*; // This package is the Java 1.2 printing API /** * This class extends JuliaSet1 and overrides the print( ) method to ...

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