Java Reference
In-Depth Information
Example 12−3: HardcopyWriter.java (continued)
if (Character.isWhitespace(buffer[i]) &&
!Character.isSpaceChar(buffer[i]) && (buffer[i] != '\t'))
continue;
// If no more characters will fit on the line, start new line.
if (charnum >= chars_per_line) {
newline();
// Also start a new page, if necessary
if (page == null) newpage();
}
// Now print the character:
// If it is a space, skip one space, without output.
// If it is a tab, skip the necessary number of spaces.
// Otherwise, print the character.
// It is inefficient to draw only one character at a time, but
// because our FontMetrics don't match up exactly to what the
// printer uses we need to position each character individually
if (Character.isSpaceChar(buffer[i])) charnum++;
else if (buffer[i] == '\t') charnum += 8 - (charnum % 8);
else {
page.drawChars(buffer, i, 1,
x0 + charnum * charwidth,
y0 + (linenum*lineheight) + lineascent);
charnum++;
}
}
}
}
/**
* This is the flush() method that all Writer subclasses must implement.
* There is no way to flush a PrintJob without prematurely printing the
* page, so we don't do anything.
**/
public void flush() { /* do nothing */ }
/**
* This is the close() method that all Writer subclasses must implement.
* Print the pending page (if any) and terminate the PrintJob.
*/
public void close() {
synchronized(this.lock) {
if (page != null) page.dispose();
// Send page to the printer
job.end();
// Terminate the job
}
}
/**
* Set the font style. The argument should be one of the font style
* constants defined by the java.awt.Font class. All subsequent output
* will be in that style. This method relies on all styles of the
* Monospaced font having the same metrics.
**/
public void setFontStyle(int style) {
synchronized (this.lock) {
// Try to set a new font, but restore current one if it fails
Font current = font;
Search WWH ::




Custom Search