Java Reference
In-Depth Information
Firstly, note that when you scale a coordinate system, a unit of length along each of the axes changes in
size, 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. The illustration above shows the effect of
scaling a rectangle up by a factor of 2. The dimensions of the rectangle are doubled, but so is the
distance of the new rectangle from each of the axes.
The reverse happens with scale factors less than 1. We want to make sure that we scale the sketch to fit
the page while keeping its top left corner at the top left of the printable area. This means that we can't
just apply the scaling factor necessary to make the sketch fit the page in the new coordinate system we
showed in the previous illustration. If we were to scale with this coordinate system, the sketch will move
in relation to the origin, away from it if we are scaling up as is the case in the illustration, or towards it if
we 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 we must apply the scaling operation to make the sketch fit on the page
after we have translated the paper origin to the top left corner of the printable area, but before we
translate this origin point to make the top left corner of the sketch appear at the top left corner of the
printable area. This will ensure that the sketch is scaled to fit the page and the top left of the sketch will
stay at the top left corner of the printable area on the page and not will move to some other point.
Secondly, we want to make sure that we scale x and y by the same factor. If we apply different scales to
the x and y axes in the user coordinate system, the relative proportions of a sketch will not be
maintained and our circles will become ellipses and squares will become rectangles.
We can calculate the scale factors we need to apply to get the sketch to fit within the printable area of
the page with the following statements:
// Calculate the x and y scales to fit the sketch to the page
double scaleX = pageFormat.getImageableWidth()/rect.width;
double scaleY = pageFormat.getImageableHeight()/rect.height;
We are using variables of type double for the scale factors here because the getImageableWidth()
and getImageableHeight() methods return values of type double . The scale factor for the x axis
needs to be such that when we multiply the width of the sketch, rect.width , by the scale factor, the
result is the width of the printable area on the page returned by getImageableWidth() , and
similarly for scaling the y axis. Since we want to apply the same scale to both axes, we should calculate
the minimum of the scale factors scaleX and scaleY . If we then apply this minimum to both axes, the
sketch will fit within the width and height of the page and still be in proportion.
Try It Out - Printing the Whole Sketch
We just need to add some code to the print() method in SketchView to calculate the required scale
factor, and then use the scale() method for the Graphics2D object to apply the scaling transformation:
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;
Search WWH ::




Custom Search