java.lang.Object
org.bzdev.gio.PrinterGraphics
- All Implemented Interfaces:
GraphicsCreator
,OSGraphicsOps
Output-Stream graphics implementation for printing.
This class allows the OSGraphics interface to be used for
printing. Aside from some printer-specific arguments in
the constructors, one will typically specify an image
height and width used for drawing. The image will then
be scaled to fit on a page.
As an example, one can print a graph to the default printer as follows:
The implementation ofimport org.bzdev.graph.*; import org.bzdev.gio.*; ... int width = ...; int height = ...; PrinterGraphics pg = new PrinterGraphics(width, height); Graph graph = new Graph(pg); ... graph.write();
graph.write()
calls
pg.imageComplete()
, which initiates the actual printing.
To allow the user to choose a printer, one can call
In this case, the orientation will be chosen to maximize the size of the printed image.... PrinterJob pjob = PrinterJob.getPrinterJob(); if (pjob.printDialog()) { PrinterGraphics pg = new PrinterGraphics(pjob, null, width, height); ... } PrinterJob job =
To allow the user to choose a printer, orientation, and other properties, use
import bzdev.gio.PrinterGraphics; import java.awt.print.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; ... PrintRequestAttributeSet aset = new PrintRequestAttributeSet(); PrinterJob pjob = PrinterJob.getPrinterJob(); if (pjob.printDialog(aset)) { PrinterGraphics pg = new PrinterGraphics(pjob, aset, width, height); ... }
-
Constructor Summary
ConstructorsConstructorDescriptionPrinterGraphics
(boolean landscape) Constructor specifying an orientation.PrinterGraphics
(int width, int height) Constructor.PrinterGraphics
(PrinterJob pjob, PrintRequestAttributeSet aset, boolean landscape) Constructor specifying a print job, a print-request attribute set, and an explicit image orientation.PrinterGraphics
(PrinterJob pjob, PrintRequestAttributeSet aset, int width, int height) Constructor specifying a print job, a print-request attribute set, a width, and a height. -
Method Summary
Modifier and TypeMethodDescriptionboolean
canReset()
Test if the methodOSGraphicsOps.reset()
is supported.void
close()
Close resources.Get a graphics context for drawing.void
flush()
Flush the output.Get the color model for the image that will be produced.int
Get the image height parameter in user space.double
Get the image height as a double-precision value.int
getWidth()
Get the image width parameter in user space.double
Get the image width as a double-precision value.void
Final processing for writing an image file to the output stream.boolean
Determine if this instance is requesting an alpha channel.void
reset()
Reset this graphics output stream.static void
setAttributes
(PrintRequestAttributeSet aset, int width, int height) Set the print-request attribute for the image orientation if missing.
-
Constructor Details
-
PrinterGraphics
Constructor. The default printer will be used. The orientation (portrait or landscape) will be chosen so that the printed image will be as large as possible- Parameters:
width
- the width in pixels for the area to be printedheight
- the height in pixel for the area to be printed- Throws:
PrinterException
- a suitable printer was not availableIllegalArgumentException
- the width or height was not a positive integer
-
PrinterGraphics
public PrinterGraphics(PrinterJob pjob, PrintRequestAttributeSet aset, int width, int height) throws PrinterException, IllegalArgumentException Constructor specifying a print job, a print-request attribute set, a width, and a height. Unlike the other constructor, the orientation (portrait or landscape) is the one explicitly provided in the attribute set, provided that the attribute set is not null. If the attribute set is null, the orientation is set to make the printed image as large as possible.- Parameters:
pjob
- the print jobaset
- the print job's attributes; null for defaultswidth
- the width in pixels for the area to be printedheight
- the height in pixel for the area to be printed- Throws:
PrinterException
- a suitable printer was not availableIllegalArgumentException
- the width or height was not a positive integer
-
PrinterGraphics
Constructor specifying an orientation. The default printer will be used and the image will not be scaled.- Parameters:
landscape
- true if a landscape orientation should be used; false if a portrait orientation should be used- Throws:
PrinterException
- if printing failed
-
PrinterGraphics
public PrinterGraphics(PrinterJob pjob, PrintRequestAttributeSet aset, boolean landscape) throws PrinterException Constructor specifying a print job, a print-request attribute set, and an explicit image orientation.- Parameters:
pjob
- the print jobaset
- the print job's attributes; null for defaultslandscape
- true if a landscape orientation should be used; false if a portrait orientation should be used- Throws:
PrinterException
- if printing failed
-
-
Method Details
-
setAttributes
public static void setAttributes(PrintRequestAttributeSet aset, int width, int height) throws NullPointerException Set the print-request attribute for the image orientation if missing. This method adds an OrientationRequested attribute to a PrintRequestAttributeSet if one is not already present. The orientation is based on the width and height arguments: if the width is larger than the height, a landscape orientation is chosen; otherwise a portrait orientation is This should be called before a print dialog is shown to preselect the orientation. A print dialog can override this selection, if desired.- Parameters:
aset
- the print-request attribute set to modifywidth
- the width of an image to be printedheight
- the height of an image to be printed- Throws:
NullPointerException
- the attribute set argument was null
-
requestsAlpha
public boolean requestsAlpha()Description copied from interface:OSGraphicsOps
Determine if this instance is requesting an alpha channel. The value may be changed from that provided in the constructor due to the capabilities of a particular image format.- Specified by:
requestsAlpha
in interfaceOSGraphicsOps
- Returns:
- true if an alpha channel is requested; false otherwise
-
getWidth
public int getWidth()Description copied from interface:OSGraphicsOps
Get the image width parameter in user space. Unless a graphics context is modified, this value represents an upper bound on the X coordinate of points that will appear in the image in the coordinate system used by the graphics context independent of the orientation.The parameter typically refers to some object such as a buffered image so that the value returned by this method is constant.
- Specified by:
getWidth
in interfaceOSGraphicsOps
- Returns:
- the width in user-space coordinates
-
getHeight
public int getHeight()Description copied from interface:OSGraphicsOps
Get the image height parameter in user space. Unless a graphics context is modified, this value represents an upper bound on the Y coordinate of points that will appear in the image in the coordinate system used by the graphics context independent of the orientation.The parameter typically refers to some object such as a buffered image so that the value returned by this method is constant.
- Specified by:
getHeight
in interfaceOSGraphicsOps
- Returns:
- the height in user-space coordinates
-
getWidthAsDouble
public double getWidthAsDouble()Get the image width as a double-precision value. For constructors that explicitly provide an image width, this value is the same as that returned bygetWidth()
. For ones that do not, the value returned is the one provided by the print job's page format (i.e., the value returned may not be an integer).- Returns:
- the image width
-
getHeightAsDouble
public double getHeightAsDouble()Get the image height as a double-precision value. For constructors that explicitly provide an image height, this value is the same as that returned bygetHeight()
. For ones that do not, the value returned is the one provided by the print job's page format (i.e., the value returned may not be an integer).- Returns:
- the image height
-
close
Description copied from interface:OSGraphicsOps
Close resources. Typically this method will perform some action when a class that implements this interface has an associated output stream or a resource that can be closed. If there are no such resources, this method should simply return. Classes that implement this method should document what they actually do.- Specified by:
close
in interfaceOSGraphicsOps
- Throws:
IOException
- if an IO error occurred
-
flush
Description copied from interface:OSGraphicsOps
Flush the output. This method will provide a partial image or partial graphics if possible or feasible. After this method is called, the user must not use graphics contexts that were previously created. Whether this method performs any action depends on the implementation of each class or subclass implementing this interface.- Specified by:
flush
in interfaceOSGraphicsOps
- Throws:
IOException
- - if an IO exception occurred- See Also:
-
getColorModel
Description copied from interface:OSGraphicsOps
Get the color model for the image that will be produced.- Specified by:
getColorModel
in interfaceOSGraphicsOps
- Returns:
- the color model
-
createGraphics
Description copied from interface:OSGraphicsOps
Get a graphics context for drawing. The graphics context created is not valid afterOSGraphicsOps.flush()
orOSGraphicsOps.reset()
is called.- Specified by:
createGraphics
in interfaceGraphicsCreator
- Specified by:
createGraphics
in interfaceOSGraphicsOps
- Returns:
- a new graphics context.
-
canReset
public boolean canReset()Description copied from interface:OSGraphicsOps
Test if the methodOSGraphicsOps.reset()
is supported.- Specified by:
canReset
in interfaceOSGraphicsOps
- Returns:
- true if
OSGraphicsOps.reset()
is supported; false otherwise
-
reset
Description copied from interface:OSGraphicsOps
Reset this graphics output stream. This is an optional operation as it is appropriate for some graphics output streams but not others- Specified by:
reset
in interfaceOSGraphicsOps
- Throws:
UnsupportedOperationException
- an instance does not support resets
-
imageComplete
Description copied from interface:OSGraphicsOps
Final processing for writing an image file to the output stream. This method does not close an IO stream, but will flush it.Subclasses should implement this method so that it throws an exception if called multiple times without a successful intervening call to
OSGraphicsOps.reset()
. The methodOSGraphicsOps.canReset()
can be called to test if resets are allowed.- Specified by:
imageComplete
in interfaceOSGraphicsOps
- Throws:
IOException
- IO failure, or a PrintException (which will be provided as the cause of the IOException), or an indication that this method was called multiple times
-