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

Example 13-1 is a Swing component that displays a fractal image known as a "Julia set," pictured in Figure 13-1. The image is fascinating, and the mathematics interesting, but the real point of the example is the print( ) method, which demonstrates how to print the Julia set using the Java 1.1 API and the Java 1.3 extensions to that API. The print( ) code is straightforward: it sets some default attribute values, then displays a dialog box to the user, to allow him to modify those attributes or cancel the print request. The dialog box returns a PrintJob object. The print( ) method then obtains a Graphics object from the PrintJob. Next, it draws the Julia set to this Graphics object and, finally, calls the Graphics.dispose( ) method to tell the printer that printing is done. These basic steps are repeated in the next two examples as well: when studying the examples of the Java 1.2 and Java 1.4 APIs, look for the code that sets attributes, displays a dialog, and obtains the Graphics object.

A Julia set printed with the Java 1.1 API

Figure 13-1. A Julia set printed with the Java 1.1 API

Example 13-1 does not include a main( ) method. To display the component, use the ShowBean program from Chapter 11, and then use the Commands menu to test the print( ) method:

java je3.gui.ShowBean je3.print.JuliaSet1

Example 13-1. JuliaSet1.java

package je3.print; import javax.swing.*; import java.awt.*; import java.awt.image.*; ...

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