Java Reference
In-Depth Information
public int print(Graphics g,
PageFormat pageFormat,
int pageIndex)
throws PrinterException {
if(pageIndex>0)
return NO _ SUCH _ PAGE;
Graphics2D g2D = (Graphics2D) g;
float x = (float)pageFormat.getImageableX();
float y = (float)pageFormat.getImageableY();
GeneralPath path = new GeneralPath();
path.moveTo(x+1, y+1);
path.lineTo(x+(float)pageFormat.getImageableWidth()-1, y+1);
path.lineTo(x+(float)pageFormat.getImageableWidth()-1,
y+(float)pageFormat.getImageableHeight()-1);
path.lineTo(x+1, y+(float)pageFormat.getImageableHeight()-1);
path.closePath();
g2D.setPaint(Color.red);
g2D.draw(path);
// Get a 12 pt bold version of the default font
Font font = g2D.getFont().deriveFont(12.f).deriveFont(Font.BOLD);
g2D.setFont(font); // Set the new font
String sketchName = theApp.getWindow().getSketchName();
Rectangle2D textRect = new java.awt.font.TextLayout(sketchName, font,
g2D.getFontRenderContext()).getBounds();
double centerX = pageFormat.getWidth()/2;
double centerY = pageFormat.getHeight()/2;
Rectangle2D.Double surround = new Rectangle2D.Double(
centerX-textRect.getWidth(),
centerY-textRect.getHeight(),
2*textRect.getWidth(),
2*textRect.getHeight());
g2D.draw(surround);
// Draw text in the middle of the printable area
g2D.setPaint(Color.blue);
g2D.drawString(sketchName, (float)(centerX-textRect.getWidth()/2),
(float)(centerY+textRect.getHeight()/2));
return PAGE _ EXISTS;
}
To center the file name on the page we need to know the width and height of the text string when it is
printed. The getStringBounds() method in the Font class returns the rectangle bounding the
string. The second argument is a reference to an object of type FontRenderContext that is returned
by the method we have called here for g2D . A FontRenderContext object contains all the
information the getStringBounds() method needs to figure out the rectangle bounding the text
when it is printed. This includes information about the size of the font as well as the resolution of the
output device - the printer in our case.
Search WWH ::




Custom Search