edu.northwestern.at.utils.swing.printing
Class PrintUtilities

java.lang.Object
  extended by edu.northwestern.at.utils.swing.printing.PrintUtilities

public class PrintUtilities
extends java.lang.Object

Print Utilities.

This class collects together a variety of methods useful when printing. These include the printPreview method which performs a print preview, and the printComponent method which prints any Swing component. If the component defines the Printable, Pageable, or PrintProgress interface, PrintComponent uses that to print the component. If the component does not define any type of printable interface, a PrintAdapter is used to "paint" a snapshot of the component to the printer.


Constructor Summary
protected PrintUtilities()
          Can't instantiate but can override.
 
Method Summary
protected static void doPrintPreview(java.awt.Component printableObject, java.awt.print.PageFormat pageFormat, java.lang.String title)
          Helper for printPreview.
static int getPageCount(java.awt.print.Printable printable, java.awt.print.PageFormat pageFormat)
          Calculate number of pages required to print a printable object.
static int getPageCount(java.awt.print.Printable printable, java.awt.print.PageFormat pageFormat, int wPage, int hPage, boolean showProgress)
          Calculate number of pages required to print a printable object.
static double getViewScaleFactor(int viewWidth, int viewHeight, int printerPageWidth, int printerPageHeight)
          Get scaling factor for a component or view.
static void printComponent(java.awt.Component componentToPrint, java.lang.String title, java.awt.print.PrinterJob printerJob, java.awt.print.PageFormat pageFormat, boolean showPrinterDialog)
          Print a component.
static void printPreview(java.awt.Component printableObject)
          Create print preview for a printable object.
static void printPreview(java.awt.Component printableObject, java.awt.print.PageFormat pageFormat)
          Create print preview for a printable object.
static void printPreview(java.awt.Component printableObject, java.awt.print.PageFormat pageFormat, java.lang.String title)
          Create print preview for a printable object with a specified title.
static void printPreview(java.awt.Component printableObject, java.lang.String title)
          Create print preview for a printable object with a specified title.
static void setDoubleBuffering(java.awt.Component component, boolean onOff)
          Enable or disable double buffering for a component to be printed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrintUtilities

protected PrintUtilities()
Can't instantiate but can override.

Method Detail

getPageCount

public static int getPageCount(java.awt.print.Printable printable,
                               java.awt.print.PageFormat pageFormat,
                               int wPage,
                               int hPage,
                               boolean showProgress)
Calculate number of pages required to print a printable object.

Parameters:
printable - The printable object. Must implement Printable interface.
pageFormat - The page format to use when laying out the pages.
wPage - Page width, which must be less than or equal to the printer page width specified by the pageFormat.
hPage - Page height, which must be less than or equal to the printer page height specified by the pageFormat.
showProgress - true to display print progress dialog.

The printable's "print" method is called to "print" each page into a buffered image which is the same size as the specified printer page (e.g., width=wPage, height=hPage). The number of pages is determined by the highest page index for which the printable object returns "Printable.PAGE_EXISTS".

Normally the width and height of the printer page is provided by the pageFormat object. However, for some systems (e.g., Windows) it may be necessary to restrict the page size to be be smaller than that indicated by pageFormat in order to prevent clipping.


getPageCount

public static int getPageCount(java.awt.print.Printable printable,
                               java.awt.print.PageFormat pageFormat)
Calculate number of pages required to print a printable object.

Parameters:
printable - The printable object. Must implement Printable interface.
pageFormat - The page format to use when laying out the pages.

setDoubleBuffering

public static void setDoubleBuffering(java.awt.Component component,
                                      boolean onOff)
Enable or disable double buffering for a component to be printed.

Parameters:
component - The component.
onOff - True to enable double buffering, false to disable.

Disabling double buffering while printing a component improves print speed and quality for some versions of Java.


getViewScaleFactor

public static double getViewScaleFactor(int viewWidth,
                                        int viewHeight,
                                        int printerPageWidth,
                                        int printerPageHeight)
Get scaling factor for a component or view.

Parameters:
viewWidth - Width of component/view.
viewHeight - Height of component/view.
printerPageWidth - Width of printable printer page area.
printerPageHeight - Height of printable printer page area. Note: this is typically smaller than the actual printer page size because some space may be taken up by the header and/or footer.
Returns:
The scaling factor. = 1.0 if no scaling required. < 1.0 if scaling required.

The scaling factor is the value to by which multiply the view size so that the printed version of the view fits within the confines of a single printed page. Images are typical examples of views whose dimensions may exceed the printer page size. The scaling factor calculated here provides the value by which to resize the view to fit the printer page size.

A scaling factor > 1.0 is never return since such a scale factor means no scaling is needed.


printComponent

public static void printComponent(java.awt.Component componentToPrint,
                                  java.lang.String title,
                                  java.awt.print.PrinterJob printerJob,
                                  java.awt.print.PageFormat pageFormat,
                                  boolean showPrinterDialog)
Print a component.

Parameters:
componentToPrint - The component to print.
title - The page title for the output.
printerJob - The printer job to use. If null, PrinterSettings.printerJob is used.
pageFormat - The page format to use. If null, PrinterSettings.pageFormat is used.
showPrinterDialog - True to show a printer dialog.

printPreview

public static void printPreview(java.awt.Component printableObject)
Create print preview for a printable object.

Parameters:
printableObject - The printable object. Must implement Printable interface.

printPreview

public static void printPreview(java.awt.Component printableObject,
                                java.awt.print.PageFormat pageFormat)
Create print preview for a printable object.

Parameters:
printableObject - The printable object. Must implement Printable interface.
pageFormat - The page format to use when laying out the preview.

printPreview

public static void printPreview(java.awt.Component printableObject,
                                java.lang.String title)
Create print preview for a printable object with a specified title.

Parameters:
printableObject - The printable object. Must implement Printable interface.
title - Title for print preview.

printPreview

public static void printPreview(java.awt.Component printableObject,
                                java.awt.print.PageFormat pageFormat,
                                java.lang.String title)
Create print preview for a printable object with a specified title.

Parameters:
printableObject - The printable object. Must implement Printable interface.
pageFormat - The page format to use when laying out the preview.
title - Title for print preview.

doPrintPreview

protected static void doPrintPreview(java.awt.Component printableObject,
                                     java.awt.print.PageFormat pageFormat,
                                     java.lang.String title)
Helper for printPreview.