Java Reference
In-Depth Information
TRY IT OUT: Printing a Sketch
You should have added the code you saw earlier to the actionPerformed() method in the FileAction
inner class to SketcherView to handle the Print menu item event, and added the code to SketcherView
to implement the Printable interface. If you compile and run Sketcher, you should be able to print a
sketch.
On my system, when I select the toolbar button to print, I get the dialog shown in Figure 21-6 .
FIGURE 21-6
This is the standard dialog for my printer. You should get a dialog for your default printer. The dialog in-
dicates that there are 9999 pages to be printed. Because you haven't specified how many pages there are,
the maximum is assumed. You only print the number of pages that the print() method allows though,
and this should be 1.
How It Works
The code in the actionPerformed() method in the FileAction class executes when you click the tool-
bar button or menu item to print a sketch. This first displays the print dialog by calling the printDia-
log() method for the PrinterJob object that you obtain. Clicking the OK button causes the dialog to
close and the print() method for the PrinterJob object to be called. This in turn causes the print()
method in the SketcherView class to be called one or more times for each page that is to be printed,
and once more to end the process. The number of pages that are printed is determined by the print()
method in the SketcherView class. Only when this method returns the value NO_SUCH_PAGE does the
PrinterJob object cease calling the method.
In the print() method in SketcherView , you adjust the origin of the user coordinate system for the
graphics context so that its position is at the top-left corner of the printable area on the page. Only
one page is printed because you return NO_SUCH_PAGE when the page index value that is passed to the
print() method is greater than 0. Incidentally, if you want to see how many times the print() method
is called for a page, just add a statement at the beginning of the method to output some trace information
to the console.
I used the print facility to print the sketch shown in Figure 21-7 , and frankly, I was disappointed.
 
 
 
Search WWH ::




Custom Search