Java Reference
In-Depth Information
A
PageFormat
object encapsulates information about a page, such as its dimensions, margin sizes, and
orientation. An object of type
Paper
describes the characteristics of a physical sheet of paper that will
be part of a
PageFormat
object. A
Book
object encapsulates a document consisting of a collection of
pages that are typically processed in an individual way. We will be getting into the detail of how you
work with these a little later in this chapter.
There are three interfaces in the
java.awt.print
package:
Printable
Implemented by a class to print a single page.
Pageable
Implemented by a class to print a multipage document where each page may
be printed by a different
Printable
object.
Printer
Graphics
Declares a method for obtaining a reference to the
PrinterJob
object for
use in a method that is printing a page.
When you print a page, an object of a class that implements the
Printable
interface determines what
is actually printed. Such an object is referred to as a
page painter
.
Printer Job Object
supplies objects
for the printer
Graphics
class
YourClass
implements
Printable
Calling the
method for this object
starts printing a set of
pages.
print( )
public int
print(Graphics g.... )
{
when it calls your
method to print a page
print( )
// Your code to print...
}
Basic Printing Operation
The
Printable
interface only defines one method,
print()
, which is called by a
PrinterJob
object when a page should be printed. Therefore the
print()
method does the printing of a page.
Note that we have mentioned two
print()
methods one defined in the
PrinterJob
class that you
call to starting the printing process, and another declared in the
Printable
interface that you
implement in your class that is to do the printing legwork for a single page.
The printing operation you must code when you implement the
print()
method declared in the
Printable
interface works through a graphics context object that provides the means for writing data
to your printer. The first argument that is 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. Just as with writing to the display, you use the
methods defined in the
Graphics
and
Graphics2D
classes to print what you want, and the basic
mechanism for printing 2D graphics or text on a page is identical to drawing on a component. The
Graphics
object for a printer also happens to implement 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 reference to the object that is managing
the print process. You would 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.