Java Reference
In-Depth Information
// Move origin to page printing area corner
g2D.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
g2D.scale(scale, scale);
// Apply scale factor
g2D.translate(-rect.x, -rect.y); // Move origin to rect top
left
paint(g2D); // Draw the sketch
return PAGE_EXISTS;
}
Directory "Sketcher 7 printing the whole sketch"
If you compile and run Sketcher with these changes, you should now be able to print each sketch within
a page.
How It Works
You calculate the scaling factors for each axis as the ratio of the dimension of the printable area on the
page to the corresponding dimension of the rectangle enclosing the sketch. You then take the minimum
of these two scale factors as the scale to be applied to both axes. As long as the scale transformation is
applied before the translation of the top-left corner of rect to the top-left corner of the printable page
area, one or other dimension of the sketch fits exactly within the printable area of the page.
The output is now fine, but if the width of the sketch is greater than the height, you waste a lot of space
on the page. Ideally in this situation you should print with a landscape orientation rather than the default
portrait orientation. Let's see what possibilities you have for doing that.
Printing in Landscape Orientation
You can easily determine when a landscape orientation would be preferable by comparing the width of a
sketch with its height. If the width is larger than the height, a landscape orientation makes better use of the
space on the paper and you get a larger-scale picture.
You can set the orientation of the output in relation to the page by calling the setOrientation() method
for the PageFormat object. You can pass one of three possible argument values to this method, which are
defined within the PageFormat class (shown in Table 21-1 ) :
TABLE 21-1 : setOrientation() Method Arguments
 
 
Search WWH ::




Custom Search