Java Reference
In-Depth Information
// Get right and bottom margins
double rightMargin =
paper.getWidth()-paper.getImageableWidth()-leftMargin;
double bottomMargin =
paper.getHeight()-paper.getImageableHeight()-topMargin;
// Double the margin sizes
leftMargin *= 2.0;
rightMargin *= 2.0;
topMargin *= 2.0;
bottomMargin *= 2.0;
// Set new printable area for the paper
paper.setImageableArea(leftMargin, topMargin,
paper.getWidth() - leftMargin -
rightMargin,
paper.getHeight() -
topMargin-bottomMargin);
pageFormat.setPaper(paper);
// Restore the
paper
pageFormat.setOrientation(PageFormat.LANDSCAPE);
return pageFormat;
// Return the
page format
}
// For pages after the first, use the object from the app window
return theApp.getWindow().getPageFormat();
}
Directory "Sketcher 12 using the Pageable interface"
You don't want to mess up the
PageFormat
object from the application window for use when you are
printing the cover page, so you duplicate it by calling its
clone()
method. The
PageFormat
class spe-
cifically overrides the
clone()
method that it inherits from the
Object
class with a
public
member to
allow a
PageFormat
object to be duplicated. The
clone()
method returns a reference of type
Object
, so
you must cast it to the correct type.
The sizes for the left and top margins correspond to the values of the
x
and
y
coordinates of the top-left
corner of the imageable area for the page, so you call the methods for the
Paper
object to retrieve these
values. You calculate the size of the right margin by subtracting the width of the imageable area and the
value for the left margin from the overall width of the page. You produce the value for the size of the
bottom margin in a similar way. After doubling the margin sizes, you redefine the position and size of
the imageable area for the
Paper
objects from these and then restore the modified Paper object in the
PageFormat
object.
If it's not the cover page, you just return a reference to the
PageFormat
object that is stored in the applic-
ation window object.
The last thing you need to do is alter the code in the
actionPerformed()
method for the inner class
FileAction
in
SketcherFrame
. You must replace the
setPrintable()
method call with a
setPage-
able()
call: