Java Reference
In-Depth Information
A translation of the paper origin to the top-left corner of the printable area, followed by a translation of
the new origin to point (-rect.x , -rect.y ) makes the position ( rect.x , rect.y ) the top-left corner of the
printable area on the paper.
You have the sketch in the right place on the page, but it won't necessarily fit into the space available on
the paper. You must scale the sketch so that it doesn't hang out beyond the right side or below the bottom of
the printable page area.
Scaling the Sketch to Fit
You saw earlier that you can get the width and height of the printable area on a page by calling the
getImageableWidth() and getImageableHeight() methods for the PageFormat object that is passed to
the print() method. You also have the width and height of the rectangle that encloses the entire sketch.
This provides the information that you need to scale the sketch to fit on the page. There are a couple of
tricky aspects that you need to think about, though. Figure 21-11 shows what happens when you scale up by
a factor of 2, for example.
FIGURE 21-11
First, note that when you scale a coordinate system, a unit of length along each of the axes changes so
things move relative to the origin as well as relative to one another. When you scale up with a factor greater
than 1, everything moves away from the origin. You can see in Figure 21-11 how scaling up by a factor of
2 causes the dimensions of the rectangle to double, and the distances of the new rectangle from each of the
axes are also doubled.
The reverse happens with scale factors less than 1. You want to make sure that you scale the sketch to fit
the page while keeping its top-left corner at the top left of the printable area. This means that you can't just
apply the scaling factor necessary to make the sketch fit the page in the new coordinate system I showed in
the previous illustration. If you were to scale with this coordinate system, the sketch would move in relation
to the origin, away from it if you are scaling up, as is the case in Figure 21-11 , or toward it if you are scaling
down. As a consequence, the top-left corner of the sketch would no longer be at the top left of the printable
area. Thus you must apply the scaling operation to make the sketch fit on the page after you have trans-
lated the paper origin to the top-left corner of the printable area, but before you translate this origin point
to make the top-left corner of the sketch appear at the top-left corner of the printable area. This ensures that
 
 
Search WWH ::




Custom Search