Package org.bzdev.gio

Class PrinterGraphics

java.lang.Object
org.bzdev.gio.PrinterGraphics
All Implemented Interfaces:
GraphicsCreator, OSGraphicsOps

public class PrinterGraphics extends Object implements 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:


 import org.bzdev.graph.*;
 import org.bzdev.gio.*;
 ...
     int width = ...;
     int height = ...;
     PrinterGraphics pg = new PrinterGraphics(width, height);
     Graph graph = new Graph(pg);
     ...
     graph.write();
 
The implementation of graph.write() calls pg.imageComplete(), which initiates the actual printing.

To allow the user to choose a printer, one can call


      ...
        PrinterJob pjob = PrinterJob.getPrinterJob();
      if (pjob.printDialog()) {
          PrinterGraphics pg = new PrinterGraphics(pjob, null, width, height);
          ...
      }
   PrinterJob job = 
 
In this case, the orientation will be chosen to maximize the size of the printed image.

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 Details

    • PrinterGraphics

      public PrinterGraphics(int width, int height) throws PrinterException, IllegalArgumentException
      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 printed
      height - the height in pixel for the area to be printed
      Throws:
      PrinterException - a suitable printer was not available
      IllegalArgumentException - 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 job
      aset - the print job's attributes; null for defaults
      width - the width in pixels for the area to be printed
      height - the height in pixel for the area to be printed
      Throws:
      PrinterException - a suitable printer was not available
      IllegalArgumentException - the width or height was not a positive integer
    • PrinterGraphics

      public PrinterGraphics(boolean landscape) throws PrinterException
      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 job
      aset - the print job's attributes; null for defaults
      landscape - 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 modify
      width - the width of an image to be printed
      height - 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 interface OSGraphicsOps
      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 interface OSGraphicsOps
      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 interface OSGraphicsOps
      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 by getWidth(). 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 by getHeight(). 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

      public void close() throws IOException
      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 interface OSGraphicsOps
      Throws:
      IOException - if an IO error occurred
    • flush

      public void flush() throws IOException
      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 interface OSGraphicsOps
      Throws:
      IOException - - if an IO exception occurred
      See Also:
    • getColorModel

      public ColorModel getColorModel()
      Description copied from interface: OSGraphicsOps
      Get the color model for the image that will be produced.
      Specified by:
      getColorModel in interface OSGraphicsOps
      Returns:
      the color model
    • createGraphics

      public Graphics2D createGraphics()
      Description copied from interface: OSGraphicsOps
      Get a graphics context for drawing. The graphics context created is not valid after OSGraphicsOps.flush() or OSGraphicsOps.reset() is called.
      Specified by:
      createGraphics in interface GraphicsCreator
      Specified by:
      createGraphics in interface OSGraphicsOps
      Returns:
      a new graphics context.
    • canReset

      public boolean canReset()
      Description copied from interface: OSGraphicsOps
      Test if the method OSGraphicsOps.reset() is supported.
      Specified by:
      canReset in interface OSGraphicsOps
      Returns:
      true if OSGraphicsOps.reset() is supported; false otherwise
    • reset

      public void reset() throws UnsupportedOperationException
      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 interface OSGraphicsOps
      Throws:
      UnsupportedOperationException - an instance does not support resets
    • imageComplete

      public void imageComplete() throws IOException
      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 method OSGraphicsOps.canReset() can be called to test if resets are allowed.

      Specified by:
      imageComplete in interface OSGraphicsOps
      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