Java Reference
In-Depth Information
Because the Book class implements the Pageable interface, you print a book in the same way as you
print a Pageable object. After you have assembled all the pages you want in a Book object, you just pass a
reference to it to the setPageable() method for your PrinterJob object. Let's take it from the top.
The Book class has only a default constructor and that creates an empty book. Thus, you create a book
like this:
Book sketchBook = new Book();
You add a page to a book by calling the append() method for the Book object. There are two overloaded
versions of append() : one to add a Printable object that represents a single page, and the other to add a
Printable object that represents several pages. In the latter case, all the pages are printed using the same
PageFormat object.
The first version of append() accepts two arguments: a reference to the Printable object and a reference
to an associated PageFormat object. This means each page in a book can have its own formatting. Suppose
you want to create a Book object for Sketcher, and the object would be created in the SketcherFrame object
somewhere. You could add the cover page of a sketch just as in the previous example to the sketchBook
object like this:
PageFormat pageFormat = pageFormat.clone();
Paper paper = pageFormat.getPaper();
double leftMargin = paper.getImageableX(); // Top left corner is indented
double topMargin = paper.getImageableY(); // by the left and top margins
double rightMargin = paper.getWidth()-paper.getImageableWidth()-leftMargin;
double bottomMargin = paper.getHeight()-paper.getImageableHeight()-topMargin;
leftMargin *= 2.0; // Double the left margin...
rightMargin *= 2.0; // ...and the right...
topMargin *= 2.0; // ...and the top...
bottomMargin *= 2.0; // ...and the bottom
paper.setImageableArea(leftMargin, topMargin, // Set new printable area
paper.getWidth()-leftMargin-rightMargin,
paper.getHeight()-topMargin-bottomMargin);
pageFormat.setPaper(paper); // Restore the paper
Search WWH ::




Custom Search