Java Reference
In-Depth Information
object of the print -method identifies the upper left corner of the component to be
printed with the upper left corner of the paper. Without any correction the image
of the component would not show parts outside the printable area. We therefore
have to shift the image such that it lies completely in the printable area. Class
Graphics provides the method translate for this purpose. The command
g.translate( int xNew, int yNew)
moves the origin of the coordinate system of the Graphics object g to the point
( x new ,
0
then the image is shifted to the right and downwards. It remains to determine
the amount of the translation. This information is contained in the PageFormat -
object. The commands
y new ) (in the coordinates before the translation). If x new >
0 and y new >
double pageFormat.getImageableX()
double pageFormat.getImageableY()
return the x - and y -coordinates of the upper left corner of the printable area in
the printer coordinates. As these are double values, they should be rounded to
the next larger integer. Altogether we have the following implementation of the
interface Printable :
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
if (pageIndex > 0)
{
return (NO_SUCH_PAGE);
}
else
{
int x=( int )pageFormat.getImageableX() + 1;
int y=( int )pageFormat.getImageableY() + 1;
g.translate(x,y);
RepaintManager currentManager =
RepaintManager.currentManager( this );
currentManager.setDoubleBufferingEnabled( false );
this .paint(g);
currentManager.setDoubleBufferingEnabled( true );
return (PAGE_EXISTS);
}
}
19.2
Class PrinterJob
Class PrinterJob is the Java equivalent of - surprisingly - a printer job, i.e. a
!
request to print the provided data. Class PrinterJob has a constructor, which,
however, is not used. Instead method getPrinterJob is applied. A print job is
created like this:
PrinterJob printJob = PrinterJob.getPrinterJob();
Search WWH ::




Custom Search