Java Reference
In-Depth Information
Figure 19.1 A picture of the print application. Note the rectangle at the margins of the
panel. This is added to control whether the whole panel is printed
19.1
Interface Printable
As mentioned above, a component has to implement the interface Printable to
become printable. Interface Printable demands the implementation of a single
method:
public int print(Graphics graphics, PageFormat pageFormat,
int pageIndex) throws PrinterException
This method is called by the runtime system when the component has to be
printed. The runtime system also supplies the parameters graphics , pageFormat
and pageIndex . This method plays the same role for printing as paintComponent
does for drawing. In fact, one can think of printing as drawing to another device.
The actual printing is started by calling paint inside method print of the compo-
nent to be printed. As with paintComponent we should not call print ourselves.
The parameters have the following meaning:
Graphics graphics links the application to the platform-specific printer drivers.
This is similar to the Graphics object in the method paintComponent which
links to the rendering routines of the operating system. The Graphics object
is created by the runtime system.
PageFormat pageFormat is also created by the runtime system. It contains in-
formation on the page size and layout of the selected printer (or the default
printer). The class provides get -methods to access these parameters.
int pageIndex is the number of the page to be printed. The runtime system
calls print with varying values of pageIndex , namely 0
.Ifthe
print command is successful it returns the integer constant PAGE_EXISTS
(defined in interface Printable ). Otherwise print returns NO_SUCH_PAGE and
,
1
,
2
,
3
,...
Search WWH ::




Custom Search