Java Reference
In-Depth Information
The Printable interface defines only one method, print() , which is called by a PrinterJob object
when a page should be printed, so this method prints a page. Note that I have mentioned two print() meth-
ods, one defined in the PrinterJob class that you call to starting the printing process and another declared
in the Printable interface. You implement the latter in your class that is to do the printing legwork for a
single page.
The printing operation that you must code when you implement the Printable interface works through
a graphics context object that provides the means for writing data to your printer. The first argument passed
to your print() method when it is called by a PrinterJob object is a reference of type Graphics that
represents the graphics context for the printer. The object that it references is actually of type Graphics2D ,
which parallels the process you are already familiar with for drawing on a component. You use the methods
defined in the Graphics and Graphics2D classes to print what you want, and the basic mechanism for print-
ing 2D graphics or text on a page is identical to drawing on a component. The Graphics object for a printer
implements the PrinterGraphics interface (not to be confused with the PrintGraphics interface in the
java.awt package!) that declares just one method, getPrinterJob() . You call this method to obtain a ref-
erence to the object that is managing the print process. You do this if you need to call PrinterJob methods
to extract information about the print job, such as the job name or the user name.
A class that implements the Pageable interface defines an object that represents a set of pages to be
printed, rather than a single page. You would implement this interface for more complicated printing situ-
ations in which a different page painter may print each page using an individual PageFormat object. It's the
job of the Pageable object to supply information to the PrinterJob object about which page painter and
PageFormat object should be used to print each page. The Pageable interface declares three methods:
getNumberOfPages() : Returns the number of pages to be printed as type int , or the constant
value UNKNOWN_NUMBER_OF_PAGES if the number of pages is not known. This constant is defined
in the Pageable interface.
getPageFormat(int pageIndex) : Returns a PageFormat object describing the size and orient-
ation of the page specified by the argument. An exception of type IndexOutOfBoundsException
is thrown if the page does not exist.
getPrintable(int pageIndex) : Returns a reference to the Printable object responsible for
printing the page specified by the argument. An exception of type IndexOutOfBoundsException
is thrown if the page does not exist.
A Book object also encapsulates a document that consists of a number of pages, each of which may be
processed individually for printing. The difference between this and an object of a class that implements the
Pageable interface is that you can add individual pages to a Book object programmatically, whereas an ob-
ject of a class that implements Pageable encapsulates all the pages. You look at how both of these options
work later in this chapter.
Search WWH ::




Custom Search