Java Reference
In-Depth Information
You know how to do this; it's exactly what you have been doing in the draw() method for each of our
element classes. You call the translate() method for the graphics context to move the origin for the user
coordinate system. Here's how this would work for the print() method in the SketcherView class:
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;
// Move origin to page printing area corner
g2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
paint(g2D); // Draw the sketch
Directory "Sketcher 6 printing a sketch"
Calling the translate() method for the Graphics2D object moves the user coordinate system so that the
(0,0) point is positioned at the top-left corner of the printable area on the page.
Let's see if that works in practice.
Search WWH ::

Custom Search