Java Reference
In-Depth Information
The following code in the print() method in the SketcherView class does this:
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();
// Move origin to page printing area corner
g2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2D.translate(-rect.x, -rect.y); // Move origin to rect top left
paint(g2D); // Draw the sketch
return PAGE_EXISTS;
}
You get the rectangle bounding the sketch by calling the getModelExtent() method that you put togeth-
er just now. You move the origin for the graphics context so that it corresponds to the top-left corner of the
printable area on the page. You then use rect to position the new origin at the top-left corner of rect . You
could have combined these two translations into one, but it's better in this instance to keep them separate,
first to make it easier to see what is going on, and second because you'll later be adding some other trans-
formations in between these translations. There is a potentially puzzling aspect to the second translation —
why are the arguments to the translate() method negative to move the origin to the top-left corner of
rect ?
To understand this, it is important to be clear about what you are doing. It's easy to get confused, so I'm
taking it step by step. First of all, the paper is a physical entity with given dimensions, and its coordinate
system just defines where each point ends up on the paper when you print something. Of course, you can
move the coordinate system for the paper about, and you can scale or even rotate it to get something printed
where you want. None of this affects what you are printing. If you have a rectangle object at (5,5) with a
Search WWH ::




Custom Search