Java Reference
In-Depth Information
Example 12−3: HardcopyWriter.java (continued)
protected int width, height; // Size (in dots) inside margins
protected int headery; // Baseline of the page header
protected int charwidth; // The width of each character
protected int lineheight; // The height of each line
protected int lineascent; // Offset of font baseline
protected int chars_per_line; // Number of characters per line
protected int lines_per_page; // Number of lines per page
protected int charnum = 0, linenum = 0; // Current column and line position
protected int pagenum = 0;
// Current page number
// A field to save state between invocations of the write() method
private boolean last_char_was_return = false;
// A static variable that holds user preferences between print jobs
protected static Properties printprops = new Properties();
/**
* The constructor for this class has a bunch of arguments:
* The frame argument is required for all printing in Java.
* The jobname appears left justified at the top of each printed page.
* The font size is specified in points, as on-screen font sizes are.
* The margins are specified in inches (or fractions of inches).
**/
public HardcopyWriter(Frame frame, String jobname, int fontsize,
double leftmargin, double rightmargin,
double topmargin, double bottommargin)
throws HardcopyWriter.PrintCanceledException
{
// Get the PrintJob object with which we'll do all the printing.
// The call is synchronized on the static printprops object, which
// means that only one print dialog can be popped up at a time.
// If the user clicks Cancel in the print dialog, throw an exception.
Toolkit toolkit = frame.getToolkit();
// get Toolkit from Frame
synchronized(printprops) {
job = toolkit.getPrintJob(frame, jobname, printprops);
}
if (job == null)
throw new PrintCanceledException("User cancelled print request");
pagesize = job.getPageDimension();
// query the page size
pagedpi = job.getPageResolution();
// query the page resolution
// Bug Workaround:
// On windows, getPageDimension() and getPageResolution don't work, so
// we've got to fake them.
if (System.getProperty("os.name").regionMatches(true,0,"windows",0,7)){
// Use screen dpi, which is what the PrintJob tries to emulate
pagedpi = toolkit.getScreenResolution();
// Assume a 8.5" x 11" page size. A4 paper users must change this.
pagesize = new Dimension((int)(8.5 * pagedpi), 11*pagedpi);
// We also have to adjust the fontsize. It is specified in points,
// (1 point = 1/72 of an inch) but Windows measures it in pixels.
fontsize = fontsize * pagedpi / 72;
}
// Compute coordinates of the upper-left corner of the page.
// I.e. the coordinates of (leftmargin, topmargin). Also compute
// the width and height inside of the margins.
Search WWH ::




Custom Search