Java Reference
In-Depth Information
sketchBook.append(new SketchCoverPage(theApp), pageFormat);
Apart from the first statement and the last statement that appends the
Printable
object that represents
the page painter, all this code is essentially the same as the code in the previous example for creating the
PageFormat
object for the cover page.
To add the second page of the sketch to the
Book
object, you could write the following:
sketchBook.append(theApp.getView(), pageFormat);
The arguments to the
append()
method specify that the view object prints the page, and that the
PageFormat
object that should be used is the one stored in the
pageFormat
field in the
SketcherFrame
class.
Now that you have assembled the topic containing the two pages for the print job, you can tell the
Print-
erJob
object that you want to print the topic:
printJob.setPageable(sketchBook); // The topic is the source of pages
All you need to do is call the
print()
method for the
PrinterJob
object to start printing. To expedite
printing, the
PrinterJob
object communicates with the
Book
object to get the number of pages to be printed
and to get the page painter and page format appropriate to print each page. The total number of pages is
returned by the
getNumberOfPages()
method for the
Book
object. In this case it always returns 2 because a
printed sketch consists of just a cover page plus the sketch. A reference to the
Printable
object for a given
page index is returned by the
getPrintable()
method for the
Book
object, and the
PageFormat
object for
a given page index is returned by the
getPageFormat()
method. Obviously, in the case of Sketcher, using a
Book
object doesn't offer much advantage over the
Pageable
object that you used in the previous example.
In situations where you have more complex documents with a lot of pages with diverse formats, it can make
things much easier.
You use the other version of
append()
for a
Book
object to add a given number of pages to a book that is
produced by a single
Printable
object, and where all the pages have the same format. Here's an example:
Book book = new Book();
book.append(painter, pageFormat, pageCount);
Here the
painter
argument is a reference of type
Printable
that prints
pageCount
pages all with the
same format,
pageFormat
. A typical instance where you might use this might be a long text document. The
document could consist of many pages, but they all are printed with the same page format. The view object
for the document typically provides a method to figure out the number of pages that are necessary to output
the document.
Printing Swing Components
Printing components is easier than you might think. Swing components are particularly easy to print because
they already know how to draw themselves. You should not call a Swing component's
paint()
method
when you want to print it, though. Rendering of Swing components is buffered by default to improve the
efficiency of displaying them but printing one by calling its
paint()
method adds a lot of unnecessary over-
head to the printing operation. Instead you should call the
print()
method that is defined in the
JCompon-
ent
class. This renders the component directly to the graphics context that is passed as an argument, so there