Java Reference
In-Depth Information
Perhaps the most important pieces of information you can get are where the top left corner of the
printable area (or imageable area to use the terminology of the method names) is on the page, and its
width and height, since this is the area you have available for printing your output. The printable area
on a page is simply the area within the current margins defined for your printer. The position of the
printable area is returned by the methods getImageableX() and getImageableY() . These return
the x and y coordinates of the upper left corner of the printable area in user coordinates of type double
for the printing context, which happen to be in units of 1/72 of an inch, which corresponds to a point -
as in point size for a font. The width and height of the printable area are returned in the same units by
the getImageableWidth() and getImageableHeight() methods.
The origin of the page coordinate system, the point (0,0), corresponds initially to the top left corner of
the paper. If you want the output to be placed in the printable area on the page, the first step will be to
move the origin of the graphics context that you will use for writing to the printer to the position of the
top left corner of the printable area.
The top left of the printable area on the page:
0,0
Original axes
position
x is
Y is
pageFormat.getImageableX()
pageFormat.getImageableY()
0,0
New axes
position
Boundary of the
printable area
The paper
g2D.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
moves the coordinate system's origin from the top left corner
of the paper to the top left of the printable area
We know how to do this - it's exactly what we have been doing in the draw() method for each of our
element classes. We call the translate() method for the graphics context. Here's how this would
work for the print() method in the SketchView class:
public int print(Graphics g, // Graphics context for printing
PageFormat pageFormat, // The page format
int pageIndex) // Index number of current page
throws PrinterException {
if(pageIndex>0)
Search WWH ::




Custom Search