Java Reference
In-Depth Information
The defaultPage() method returns a
reference to a PageFormat object that is
initialized to the default size and orientation for
the current printer. We pass this reference to the
pageDialog() method, and this displays the
dialog shown using the values from the
PageFormat object.
As you can see, with my printer I can select the paper size and the source tray. I can also set the margin
sizes as well as select portrait or landscape orientation. When the dialog is closed normally with the OK
button, the method returns a new PageFormat object that incorporates the values set by the user in the
dialog. If the Cancel button is used to close the dialog, the original reference that was passed as an
argument is returned. The overloaded setPrintable() method with two parameters will use the
PageFormat object passed as the second argument when the print() method for the object
referenced by the first argument is called.
Of course, the print() method in SketchView should just use whatever PageFormat object is
handed to it. This will be set with the options that the user chose in the dialog and the Graphics object
will be created consistent with that. This corresponds with the version of the print() method that we
used in the example 'Printing the Whole Sketch' .
Using the Java Print Dialog
The overloaded version of the printDialog() method that we sidestepped earlier generates a Java
based print dialog rather than using the native dialog. This method requires a single argument of type
PrintRequestAttributeSet . This interface type is defined in the javax.print.attribute
package, which declares methods for adding attributes relating to a print request to a set of such
attributes. These attributes specify things such as the number of copies to be printed, the orientation of
the image on the paper, or the media or media tray to be selected for the print job. The
HashPrintRequestAttributeSet class that is defined in the same package implements this
interface and encapsulates a set of print request attributes stored as a hash map. It will be useful if we
can define a set of attributes that have some persistence in Sketcher so they can be carried forward from
one print request to the next. We can add an initially empty set of print request attributes to the
SketchFrame class like this:
Search WWH ::




Custom Search