Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
The heart of the matter.
To be able to print, we need a java.awt.print.PrinterJob. We get it by using PrinterJob's method getPrinterJob.
( 1.1 : java.awt.PrintJob !! )
getPrinterJob is a static method so you can call it with the class name preceding it.
We then instruct it to call this instance's print method at the proper times .
Next, the printing dialog window is shown :
If the user hasn't cancelled it, the actual print command is issued. This is NOT a direct call to our print method, but rather starting the job.
FOR EACH PAGE to be printed, our print method will be called .
The class is known to implement this method .
All we want is one page, the first, number zero, to be printed. Any other number will stop the printing process, will stop the print method from being called again.
The Graphics' methods can be used just as in a paint method.
The PageFormat reference argument can be used to retrieve printer settings :
And these are used to draw a rectangle around the paper's printable area :
In the TextArea these settings are shown.
To indicate that what we've done, our logical drawing, has to be hard-copied we return the appropriate constant .
Now the print method will be called again with 1 as pg value, but since we return the constant Printable.NO_SUCH_PAGE, the printing process stops.