may be called to render a page more than once, each page is painted in
order. We may, therefore, keep track of changes in the page being rendered
by setting the currentPage variable to equal the pageIndex, and then
comparing these variables on subsequent calls to this method. When the two
variables match, it means that the page is being rendered for the second or
third time. When the currentPage differs from the pageIndex, a new page is
being requested.
The highlights of the process used print a page are as follows:
I. The Graphics object is cast to a Graphics2D object to allow for
scaling.
II. The JEditorPane is laid out using the width of a printable page.
This will handle line breaks. If the JEditorPane cannot be sized at
the width of the graphics clip, scaling will be allowed.
III. The root view of the JEditorPane is obtained. By examining this root
view and all of its children, printView will be able to determine
the location of each printable element of the document.
IV. If the scaleWidthToFit option is chosen, a scaling ratio is
determined, and the graphics2D object is scaled.
V. The Graphics2D object is clipped to the size of the printable page.
VI. currentPage is checked to see if this is a new page to render. If so,
pageStartY and pageEndY are reset.
VII. To match the coordinates of the printable clip of graphics2D and the
allocation rectangle which will be used to lay out the views,
graphics2D is translated to begin at the printable X and Y
coordinates of the graphics clip.
VIII. An allocation Rectangle is created to represent the layout of the
Views.
The Printable Interface always prints the area indexed by reference
to the Graphics object. For instance, with a standard 8.5 x 11 inch
page with 1 inch margins the rectangle X = 72, Y = 72, Width = 468,
and Height = 648, the area 72, 72, 468, 648 will be painted regardless
of which page is actually being printed.
To align the allocation Rectangle with the graphics2D object two
things are done. The first step is to translate the X and Y
coordinates of the graphics2D object to begin at the X and Y
coordinates of the printable clip, see step VII. Next, when printing
other than the first page, the allocation rectangle must start laying
out in coordinates represented by negative numbers. After page one,
the beginning of the allocation is started at minus the page end of
the prior page. This moves the part which has already been rendered to
before the printable clip of the graphics2D object.
X. The printView method is called to paint the page. Its return value
will indicate if a page has been rendered.
Although public, print should not ordinarily be called by programs other
than PrinterJob.