Java Reference
In-Depth Information
includes information about the size of the font as well as the resolution of the output device — the print-
er, in this case.
You can now implement the Pageable interface in the SketcherView class. You must add three methods
to the class: getNumberOfPages() , which returns the number of pages to be printed; getPrint-
able() , which returns a reference to a Printable object that prints a page with a given index; and
getPageFormat() , which returns a reference to a PageFormat object corresponding to a particular page:
// Import statements as before...
class SketcherView extends JComponent
implements Observer, ActionListener, Printable,
Pageable {
// Constructor and othe code as before...
// Method to return page count - always two pages
public int getNumberOfPages() {
return 2;
}
// Method to return the Printable object that will render the page
public Printable getPrintable(int pageIndex) {
if(pageIndex == 0) {
// For the first page
// return the cover page
return new SketchCoverPage(theApp.getWindow().getSketchName());
} else {
return this;
}
public PageFormat getPageFormat(int pageIndex) {
// Code to define the PageFormat object for the page...
}
// Print the sketch on a local printer
public int print(Graphics g, // Graphics
context for printing
PageFormat pageFormat, // The page
format
int pageIndex) // Index number
of current page
throws PrinterException {
// Code to test pageIndex must be removed...
//if(pageIndex > 0) {
// return NO_SUCH_PAGE;
//}
Graphics2D g2D = (Graphics2D) g;
// Get sketch bounds
Rectangle rect = theApp.getModel().getModelExtent();
// Rest of the code as before...
}
Search WWH ::




Custom Search