Java Reference
In-Depth Information
public class SketchCoverPage implements Printable {
public SketchCoverPage(String sketchName) {
this.sketchName = sketchName;
}
// 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 String sketchName;
}
Directory "Sketcher 12 using the Pageable interface"
You will use the TextLayout class in the implementation of the print() method.
The name of the sketch to print on the cover page is passed to the SketchCoverPage constructor. Because
the need has come up, you should add a getSketchName() method to the SketcherFrame class that sup-
plies a reference to a String object containing the file name:
// Method to return the name of the current sketch
public String getSketchName() {
return currentSketchFile == null ? DEFAULT_FILENAME.toString() :
currentSketchFile.getFileName().toString();
}
Directory "Sketcher 12 using the Pageable interface"
You can use this method when you create a SketchCoverPage object. The print() method in the
SketchCoverPage class 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 I'm just putting 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 you could do that:
public int print(Graphics g,
PageFormat pageFormat,
int pageIndex)
throws PrinterException {
Search WWH ::




Custom Search