Java Reference
In-Depth Information
Example 12−4: PrintableDocument.java (continued)
// For a document of n pages, this list stores the lengths of pages
// 0 through n-2. The last page is assumed to have a full length
ArrayList pageLengths = new ArrayList();
// For a document of n pages, this list stores the starting offset of
// pages 1 through n-1. The offset of page 0 is always 0
ArrayList pageOffsets = new ArrayList();
/**
* Break a page at the specified Y coordinate. Store the necessary
* information into the pageLengths and pageOffsets lists
**/
void breakPage(double y) {
double pageLength = y-pageStart-printY;
pageStart = y-printY;
pageLengths.add(new Double(pageLength));
pageOffsets.add(new Double(pageStart));
}
/** Return the number of pages. This is a Pageable method.
*/
public int getNumberOfPages() { return numPages; }
/**
* Return the PageFormat object for the specified page. This is a
* Pageable method. This implementation uses the computed length of the
* page in the returned PageFormat object. The PrinterJob will use this
* as a clipping region, which will prevent extraneous parts of the
* document from being drawn in the top and bottom margins.
**/
public PageFormat getPageFormat(int pagenum) {
// On the last page, just return the user-specified page format
if (pagenum == numPages-1) return format;
// Otherwise, look up the height of this page and return an
// appropriate PageFormat.
double pageLength = ((Double)pageLengths.get(pagenum)).doubleValue();
PageFormat f = (PageFormat) format.clone();
Paper p = f.getPaper();
if (f.getOrientation() == PageFormat.PORTRAIT)
p.setImageableArea(printX*scalefactor, printY*scalefactor,
printWidth*scalefactor, pageLength*scalefactor);
else
p.setImageableArea(printY*scalefactor, printX*scalefactor,
pageLength*scalefactor, printWidth*scalefactor);
f.setPaper(p);
return f;
}
/**
* This Pageable method returns the Printable object for the specified
* page. Since this class implements both Pageable and Printable, it just
* returns this.
**/
public Printable getPrintable(int pagenum) { return this; }
/**
* This is the basic Printable method that prints a specified page
**/
Search WWH ::




Custom Search