Java Reference
In-Depth Information
In each case the long side of the paper is in the same orientation as the y-axis, but note that an Apple
Macintosh landscape specification has the origin at the top-right corner of the page rather than the top-left
or bottom-left.
You might think that you can incorporate
LANDSCAPE
orientation into the
print()
method in
Sketcher-
View
by changing the
PageFormat
object:
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) {
return NO_SUCH_PAGE;
}
Graphics2D g2D = (Graphics2D) g;
// Get sketch bounds
Rectangle rect = theApp.getModel().getModelExtent();
// If the width is more than the height, set landscape - not effective!
if(rect.width>rect.height) {
pageFormat.setOrientation(pageFormat.LANDSCAPE);
}
// Rest of the code as before...
}
Having set the orientation for the
PageFormat
object, the methods returning the coordinates for the posi-
tion of the printable area, and the width and height all return values consistent with the orientation. Thus the
width of the printable area is greater than the height if the orientation has been set to
LANDSCAPE
. Everything
looks fine until you try it out. It just doesn't work. The
PageFormat
object that is passed to the
print()
method here is a carrier of information — the information that the
PrinterJob
method used when it created
the graphics context object. The coordinate system in the graphics context has already been set up with
whatever paper orientation was set in the
PageFormat
object, and changing it here is too late. The solution