Java Reference
In-Depth Information
If you are printing on letter size paper, a one-inch margin might be more appropriate so you would write:
double marginSize = 72.0; // 1 inch wide
paper.setImageableArea(marginSize, marginSize, // Top left
widthLetterSize-2.0*marginSize, // Width
heightLetterSize-2.0*marginSize); // Height
Of course, there's no reason why a class that implements Pageable cannot also implement
Printable , so we could do this in Sketcher, just to get a feel for the Pageable interface in action.
Try It Out - Using the Pageable Interface
We will just print two pages in a print job in Sketcher, a cover page with a title for the sketch, plus the
sketch itself, which may be portrait or landscape of course. We could produce both pages in
SketchView , but to make it more interesting, let's define a separate class to represent a Printable
object for the cover page:
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;
class SketchCoverPage implements Printable {
public SketchCoverPage(Sketcher theApp) {
this.theApp = theApp;
}
// Print the cover page
public int print(Graphics g,
PageFormat pageFormat,
int pageIndex)
throws PrinterException {
// If it's page 0 print the cover page...
}
private Sketcher theApp;
}
The sole reason for having the Sketcher field in the class is so we can get at the name of the sketch to
print on the cover page. Since the need has come up, we should add a getSketchName() method to
the SketchFrame class that will supply a reference to the String object containing the file name:
public String getSketchName() {
return filename;
}
We can use this in the implementation of the print() method in the SketchCoverPage class. The
method needs to recognize when it is being called to print the first page - that is when the page index is
zero - and then print the cover page. You could do whatever you like here to produce a fancy cover
page, but we'll just put the code to draw a line border inset from the page, and put the sketch file name
in the middle in a box. Here's how we would do that:
Search WWH ::




Custom Search