
Example 5−2: PageableT ext.java (continued)
// Create the font we will use, and compute spacing between lines
font = new Font(FONTFAMILY, FONTSTYLE, FONTSIZE);
linespacing = (int) (FONTSIZE * LINESPACEFACTOR);
// Figure out how many lines per page and how many pages
linesPerPage = (int)Math.floor(format.getImageableHeight()/linespacing);
numPages = (lines.size()-1)/linesPerPage + 1;
}
// These are the methods of the Pageable interface.
// Note that the getPrintable() method returns this object, which means
// that this class must also implement the Printable interface.
public int getNumberOfPages() { return numPages; }
public PageFormat getPageFormat(int pagenum) { return format; }
public Printable getPrintable(int pagenum) { return this; }
/**
* This is the print() method of the Printable interface.
*Itdoes most of the printing work.
*/
public int print(Graphics g, PageFormat format, int pagenum) {
// Tell the PrinterJob if the page number is not a legal one
if ((pagenum < 0) | (pagenum >= numPages))
return NO_SUCH_PAGE;
// First time we're called, figure out the baseline for our font.
// We couldn't do this earlier because we needed a Graphics object.
if (baseline == -1) {
FontMetrics fm = g.getFontMetrics(font);
baseline = fm.getAscent();
}
// Clear the background to white. This shouldn't be necessary but is
// required on some systems to work around an implementation bug.
g.setColor(Color.white);
g.fillRect((int)format.getImageableX(), ...