Class PSPrinter

  • java.lang.Object
    • at.ofai.music.util.PSPrinter
  • All Implemented Interfaces:
    java.awt.print.Printable


    public class PSPrinter
    extends java.lang.Object
    implements java.awt.print.Printable
    A utility class for converting graphical user interface components to PostScript, which can be sent directly to a printer or printed to a file. This gives much higher quality illustrations for articles than if a screenshot is used, since scaling should not reduce quality. The only requirement is that the component to be printed has a paint(Graphics) method.

    There are some bugs in this code which require manual editing of the PostScript file. First, there doesn't seem to be any way to include the bounding box, although it is possible to calculate it. Second, the cliprect produced in the PostScript output is wrong. (Check: has this been fixed in more recent Java versions? Apparently not, as of 1.5.0, but if scaling is not performed, the cliprect is OK and the bounding box correct.) See print(Graphics, PageFormat, int)

    • Field Summary

      • Fields inherited from interface java.awt.print.Printable

        NO_SUCH_PAGE, PAGE_EXISTS
    • Constructor Summary

      Constructors 
      Constructor and Description
      PSPrinter(java.awt.Component c, int res)
      Constructs a PSPrinter for a given graphical component and resolution.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      void doPrint()
      Produces a print dialog and executes the requested print job.
      static void print(java.awt.Component c)
      Print a GUI component to a PostScript printer or file.
      static void print(java.awt.Component c, int r)
      Print a GUI component to a PostScript printer or file.
      int print(java.awt.Graphics g, java.awt.print.PageFormat f, int pg)
      The callback method for performing the printing / Postscript conversion.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PSPrinter

        public PSPrinter(java.awt.Component c,
                         int res)
        Constructs a PSPrinter for a given graphical component and resolution.
        Parameters:
        c - the component to be rendered in PostScript
        res - the resolution of the printer in pixels per inch; set res to -1 for no scaling (avoids the apparently buggy cliprect)
    • Method Detail

      • print

        public static void print(java.awt.Component c,
                                 int r)
        Print a GUI component to a PostScript printer or file. The 2 forms of this method are the normal ways of accessing this class. This form has problems printing some components. It is recommended to use the other version.
        Parameters:
        c - the component to be rendered in PostScript
        r - the resolution of the printer in pixels per inch
      • print

        public static void print(java.awt.Component c)
        Print a GUI component to a PostScript printer or file. The 2 forms of this method are the normal ways of accessing this class. If no resolution is given, the picture is not scaled, and the cliprect is then correct. This is the recommended version to use.
        Parameters:
        c - the component to be rendered in PostScript
      • doPrint

        public void doPrint()
        Produces a print dialog and executes the requested print job. The print job performs its task by callback of the print(Graphics, PageFormat, int) method.
      • print

        public int print(java.awt.Graphics g,
                         java.awt.print.PageFormat f,
                         int pg)
                  throws java.awt.print.PrinterException
        The callback method for performing the printing / Postscript conversion. The resulting PostScript file requires some post-editing. In particular, there are two problems to be dealt with:

        1) The file has no bounding box. This method prints the correct bounding box to stardard output, and it must then be cut and pasted into the PostScript file. (There must be a better way!)

        2) The cliprect is wrong (but only if the resolution is specified). This is solved by using resolution = -1 or by deleting the lines in the PostScript file from newpath to clip. (I don't know if this causes problems for components that try to draw outside of their area.)

        Specified by:
        print in interface java.awt.print.Printable
        Parameters:
        g - the graphics object used for painting
        f - the requested page format (e.g. A4)
        pg - the page number (must be 0, or we report an error)
        Returns:
        the error status; if the page is successfully rendered, Printable.PAGE_EXISTS is returned, otherwise if a page number greater than 0 is requested, Printable.NO_SUCH_PAGE is returned
        Throws:
        java.awt.print.PrinterException - thrown when the print job is terminated