Java Reference
In-Depth Information
Example 12−3: HardcopyWriter.java (continued)
try { font = new Font("Monospaced", style, fontsize); }
catch (Exception e) { font = current; }
// If a page is pending, set the new font. Otherwise newpage() will
if (page != null) page.setFont(font);
}
}
/** End the current page. Subsequent output will be on a new page. */
public void pageBreak() { synchronized(this.lock) { newpage(); } }
/** Return the number of columns of characters that fit on the page */
public int getCharactersPerLine() { return this.chars_per_line; }
/** Return the number of lines that fit on a page */
public int getLinesPerPage() { return this.lines_per_page; }
/** This internal method begins a new line */
protected void newline() {
charnum = 0; // Reset character number to 0
linenum++; // Increment line number
if (linenum >= lines_per_page) { // If we've reached the end of page
page.dispose();
// send page to printer
page = null;
// but don't start a new page yet.
}
}
/** This internal method begins a new page and prints the header. */
protected void newpage() {
page = job.getGraphics(); // Begin the new page
linenum = 0; charnum = 0; // Reset line and char number
pagenum++; // Increment page number
page.setFont(headerfont); // Set the header font.
page.drawString(jobname, x0, headery); // Print job name left justified
String s = "- " + pagenum + " -";
// Print the page # centered.
int w = headermetrics.stringWidth(s);
page.drawString(s, x0 + (this.width - w)/2, headery);
w = headermetrics.stringWidth(time); // Print date right justified
page.drawString(time, x0 + width - w, headery);
// Draw a line beneath the header
int y = headery + headermetrics.getDescent() + 1;
page.drawLine(x0, y, x0+width, y);
// Set the basic monospaced font for the rest of the page.
page.setFont(font);
}
/**
* This is the exception class that the HardcopyWriter constructor
* throws when the user clicks "Cancel" in the print dialog box.
**/
public static class PrintCanceledException extends Exception {
public PrintCanceledException(String msg) { super(msg); }
}
/**
* A program that prints the specified file using HardcopyWriter
Search WWH ::




Custom Search