Java Reference
In-Depth Information
Example 12−4: PrintableDocument.java (continued)
* is used as the parent of the root View object we want to print
**/
static class ParentView extends View {
ViewFactory viewFactory; // The ViewFactory for the hierarchy of views
Container container;
// The Container for the hierarchy of views
public ParentView(View v, ViewFactory viewFactory, Container container)
{
super(v.getElement());
this.viewFactory = viewFactory;
this.container = container;
}
// These methods return key pieces of information required by
// the View hierarchy.
public ViewFactory getViewFactory() { return viewFactory; }
public Container getContainer() { return container; }
// These methods are abstract in View, so we've got to provide
// dummy implementations of them here, even though they're never used.
public void paint(Graphics g, Shape allocation) {}
public float getPreferredSpan(int axis) { return 0.0f; }
public int viewToModel(float x,float y,Shape a,Position.Bias[] bias) {
return 0;
}
public Shape modelToView(int pos, Shape a, Position.Bias b)
throws BadLocationException {
return a;
}
}
}
Exercises
12-1. In Java 1.3, java.awt.JobAttributes and java.awt.PageAttributes are
new classes that enhance the Java 1.1 Printing API. Read the documentation
for these classes, then modify the HardcopyWriter class so that you can pass
PageAttributes and JobAttributes objects to the HardcopyWriter construc-
tor. Test your modification by writing a program that prints in landscape
mode and uses duplex (two-sided) printing (if you have a printer that sup-
ports this feature).
12-2. With the Java 1.2 Printing API, the print() method of a Printable object
may be called multiple times per page to facilitate the printing of high-reso-
lution images and drawings. For this reason, it is not possible to print a char-
acter stream with the Java 1.2 API, as is done in with the Java 1.1 API in the
HardcopyWriter class. Nevertheless, it is still possible to print multipage text
documents using the Java 1.2 API. Write a program that reads a text file and
prints its contents using the Java 1.2 API.
12-3. Modify your program from the previous exercise to support “2-up” print-
ing—printing two reduced-size pages side by side in landscape mode. You'll
want to use Java 2D scaling and rotation features to accomplish this.
Search WWH ::




Custom Search