Java Reference
In-Depth Information
// Plus the rest of the class as before...
}
Directory "Sketcher 12 using the Pageable interface"
The first two methods are already fully defined here. You always print two pages, the first page being
printed by a SketchCoverPage object and the second page by the view object. The print() method
should now print the page when called without testing the pageIndex value for zero. The page with in-
dex 0 is printed by the SketchCoverPage object.
Let's see how you can produce the PageFormat object for a page. You should use the PageFormat object
that's stored as a field in the application window, so add a method to the SketcherFrame class to make
it accessible from another class:
// Method to return a reference to the current PageFormat object
public PageFormat getPageFormat() {
return pageFormat;
}
Directory "Sketcher 12 using the Pageable interface"
To exercise some of the methods in the Paper class that I've discussed, you can arbitrarily double the
size of the margins for the cover page but leave the margins for the other page at their default sizes.
You can also set the cover page to landscape orientation, but leave the second page as whatever is set in
the pageFormat field of the application object. Here's the code for the getPageFormat() method in the
SketcherView class that does that:
public PageFormat getPageFormat(int pageIndex) {
if(pageIndex == 0) {
// If it's the cover page...
// ...make the margins twice the
size
// Create a duplicate of the current page format
PageFormat pageFormat =
(PageFormat)(theApp.getWindow().getPageFormat().clone());
Paper paper = pageFormat.getPaper();
// Get top and left margins - x & y coordinates of top-left
corner
// of imageable area are the left & top margins
double leftMargin = paper.getImageableX();
double topMargin = paper.getImageableY();
Search WWH ::




Custom Search