Java Reference
In-Depth Information
Example 12−3: HardcopyWriter.java (continued)
hw.setFontStyle(Font.BOLD);
out.println("Dimensions:");
hw.setFontStyle(Font.PLAIN);
out.println("\tResolution: " + hw.pagedpi + " dots per inch");
out.println("\tPage width (pixels): " + hw.pagesize.width);
out.println("\tPage height (pixels): " + hw.pagesize.height);
out.println("\tWidth inside margins (pixels): " + hw.width);
out.println("\tHeight inside margins (pixels): " + hw.height);
out.println("\tCharacters per line: " + cols);
out.println("\tLines per page: " + rows);
// Skip down to the bottom of the page
for(int i = 0; i < rows-30; i++) out.println();
// And mark the lower left and lower right
out.print("+"); // lower-left
for(int i=0;i<cols-2;i++) out.print(" "); // space-over
out.print("+");
// lower-right
// Close the output stream, forcing the page to be printed
out.close();
}
}
}
Printing Swing Documents
Example 10-21 in Chapter 10 demonstrated the power of javax.swing.
text.JTextComponent to display complex documents, including HTML documents.
One feature missing from JTextComponent , however, is the ability to print those
documents. Example 10-21 included a printing feature based on a PrintableDocu-
ment class, which is listed in Example 12-4.
PrintableDocument uses the Java 1.2 Printing API. In addition to implementing the
Printable interface, this example also implements the Pageable interface. Page-
able represents a multipage document and defines three methods: getNumberOf-
Pages() , which returns the number of pages in the document; getPageFormat() ,
which returns the size and orientation of each page of the document; and get-
Printable() , which returns a Printable object that represents each page.
Beyond demonstrating the use of the Pageable and Printable interfaces, the
example also shows the inner workings of the javax.swing.text package. In this
package, Document objects are composed of nested Element objects, which are dis-
played on the screen (or a printer) by a parallel hierarchy of View objects.
javax.swing.text is one of the more obscure areas of Swing, so don't worry if
you don't understand all the details.
Unfortunately, the PrintableDocument class is not as robust and reliable as I
would like. In my tests, I have found that it occasionally breaks a page in the mid-
dle of a line of text, placing half the line at the bottom of one page and the rest at
the top of the next page. I am uncertain whether the bug lies in PrintableDocu-
ment or the javax.swing.text package. Also, PrintableDocument doesn't handle
unbreakable wide lines very well. When a document contains an unbreakable line
Search WWH ::




Custom Search