Java Reference
In-Depth Information
User Coordinates
rect.x, rect.y
0, 0
x
rect.width
rect
Produced as the union of the rectangles
for all of the sketch elements
y
Finding the Rectangle Enclosing a Sketch
You can see from the illustration how the rectangle returned by the getModelExtent() method is
simply the rectangle that encloses all the bounding rectangles for the individual elements. If you
visualize the origin of the user coordinate system being placed at the top left corner of the printable area
on the page, you can appreciate that a section of the sketch in the illustration will be hanging out to the
left outside the printable area. This can arise quite easily in Sketcher, when you are drawing a circle
with the center close to either of the axes, for instance, or if you move a shape so this is the case. We
can avoid missing part of the sketch from the printed output by first translating the origin of the
coordinate system to the top left corner of rect , and then translating the origin at this position to the
top left corner of the printable area on the page.
The following code in the print() method in the SketchView class will do 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());
// Move origin to rect top left
g2D.translate(-rect.x, -rect.y);
paint(g2D); // Draw the sketch
return PAGE _ EXISTS;
}
Search WWH ::




Custom Search