sophware.print
Class Printer

java.lang.Object
  extended bysophware.print.Printer

public class Printer
extends java.lang.Object

Printer offers an easy to use interface to print any graphical component. Print options can be modified programmatically using the methods described below, or with the optional Page Setup dialog.

This Page Setup Dialog allows its user to modify the paper margins, paper size and paper orientation. It also allows scaling of the image to be printed and anchoring the image to different locations within the page and for JTextComponents it will allow automatic page break detection.

The thumbnail preview shows the pages to be printed. When the thumbnail is clicked upon, it pops up a dialog containing a full scale preview.

Indirect Use:
The simplest way to use the Printer class is indirectly through the JPrintButton or JPrintMenuItem components. Including either of these components in your frame provides it with instant Printer access.

 	import sophware.print.*;
 		...
 	JPanel p = new JPanel();
 	p.add(new JPrintButton()); // add a print button to your frame

 	       and/or

 	JMenubar mb = new JMenuBar();
 	JMenu m = new JMenu("File");
 	m.add(new JPrintMenuItem()); // add a print button to your menu bar
 	mb.add(m);
 	setJMenuBar(mb);
 
Direct Use:
To use the Printer class directly, the standard pattern is roughly as follows:
	import sophware.print.*;
		...
	Printer printer = new Printer(myFrame);
	printer.setDefaultMaxWidth(1); // prevent image from extending beyond one page in width
 	printer.print(myComponent); // print my component
 

Version:
1.1 3/2/01
Author:
Russell Soph
See Also:
JPrintButton, JPrintMenuItem

Field Summary
static int CENTER
          Anchor component in the center
static int EAST
          Anchor component right justified centered vertically
static int NORTH
          Anchor component to the top of the page centered
static int NORTHEAST
          Anchor component to the top of the page right justified
static int NORTHWEST
          Anchor component to the top of the page left justified
static int SOUTH
          Anchor component to the bottom and centered
static int SOUTHEAST
          Anchor component to the bottom right justified
static int SOUTHWEST
          Anchor component to the bottom left justified
static int UNLIMITED_PAGES
          Value for unlimited number of pages
static int WEST
          Anchor component left justified centered vertically
 
Constructor Summary
Printer()
          Constructs a new Printer instance With the default constructor, none of the setup dialogs can be used unless a Frame is supplied on a subsequent call to print
Printer(java.awt.Frame frame)
          Constructs a new Printer instance
 
Method Summary
 int getDefaultAnchor()
          Gets the default anchor position
 int getDefaultMaxHeight()
          Returns the default maximum height (in pages).
 int getDefaultMaxWidth()
          Returns the default width (in pages).
 int getDefaultOrientation()
          Get the default page orientation
 double getDefaultPaperHeight()
          Gets the default paper height (in inches).
 double getDefaultPaperWidth()
          Gets the default paper width (in inches).
 double getDefaultTextScale()
          Get default scaling for text components.
 boolean isAutoOrientEnabled()
          Indicates whether or not automatic page orientation is enabled whether or not automatic orientation adjustments are enabled
 boolean isDefaultTextFormatingEnabled()
          Indicates whether or not text formating is enabled whether or not text formating is enabled
 boolean isFormatedAsText(java.awt.Component comp)
          Indicates whether or not the given component will be formatted as text.
 boolean isNativePageSetupEnabled()
          Indicates whether or not the native page setup Dialog is enabled.
 boolean isNativePrinterSetupEnabled()
          Indicates whether or not the native Printer Setup Dialog is enabled.
 boolean isPageSetupEnabled()
          Indicates whether or not the Page Setup Dialog is enabled.
 boolean print(java.awt.Component comp)
          Print the given component.
 boolean print(java.awt.Component comp, int copies)
          Print the given number of copies of the given component.
 boolean print(java.awt.Frame frame, java.awt.Component comp, int copies)
          Print the given number of copies of the given component using the given base Frame.
 void restoreDefaults()
          Restore printer values to their defaults.
 void setAutoOrientEnabled(boolean enable)
          Enables and disables automatic orientation changes.
 void setDefaultAnchor(int anchor)
          Sets the default position of how the component should be placed within the page or pages.
 void setDefaultMargins(double top, double bottom, double left, double right)
          Sets the default margins (in inches) for subsequent calls to print().
 void setDefaultMaxHeight(int pages)
          Sets the default maximum height (in pages) of a print request.
 void setDefaultMaxWidth(int pages)
          Sets the default maximum width (in pages) of a print request.
 void setDefaultOrientation(int orientation)
          Sets the default page orientation for subsequent calls to print().
 void setDefaultPaperSize(double width, double height)
          Sets the default paper size (in inches).
 void setDefaultTextFormatingEnabled(boolean enable)
          Enables and disables text formating on text components.
 void setDefaultTextScale(double scale)
          Set default scaling for text components.
 void setNativePageSetupEnabled(boolean enabled)
          Enables or disables the native Page Setup Dialog The native Page Setup Dialog is a platform specific dialog.
 void setNativePrinterSetupEnabled(boolean enabled)
          Enables or disables the native printer Setup Dialog.
 void setPageSetupEnabled(boolean enabled)
          Enables or disables the class Page Setup Dialog
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNLIMITED_PAGES

public static final int UNLIMITED_PAGES
Value for unlimited number of pages

See Also:
setDefaultMaxWidth(int), setDefaultMaxHeight(int), getDefaultMaxWidth(), getDefaultMaxHeight(), Constant Field Values

NORTHWEST

public static final int NORTHWEST
Anchor component to the top of the page left justified

See Also:
Constant Field Values

NORTH

public static final int NORTH
Anchor component to the top of the page centered

See Also:
Constant Field Values

NORTHEAST

public static final int NORTHEAST
Anchor component to the top of the page right justified

See Also:
Constant Field Values

WEST

public static final int WEST
Anchor component left justified centered vertically

See Also:
Constant Field Values

CENTER

public static final int CENTER
Anchor component in the center

See Also:
Constant Field Values

EAST

public static final int EAST
Anchor component right justified centered vertically

See Also:
Constant Field Values

SOUTHWEST

public static final int SOUTHWEST
Anchor component to the bottom left justified

See Also:
Constant Field Values

SOUTH

public static final int SOUTH
Anchor component to the bottom and centered

See Also:
Constant Field Values

SOUTHEAST

public static final int SOUTHEAST
Anchor component to the bottom right justified

See Also:
Constant Field Values
Constructor Detail

Printer

public Printer()
Constructs a new Printer instance With the default constructor, none of the setup dialogs can be used unless a Frame is supplied on a subsequent call to print

See Also:
print(java.awt.Component)

Printer

public Printer(java.awt.Frame frame)
Constructs a new Printer instance

Parameters:
frame - is the base frame associated with any dialogs
Method Detail

setDefaultOrientation

public void setDefaultOrientation(int orientation)
Sets the default page orientation for subsequent calls to print(). Use of this method disables automatic orientation.

Parameters:
orientation - one of the constants PageFormat.PORTRAIT, PageFormat.LANDSCAPE, PageFormat.REVERSE_LANDSCAPE
See Also:
getDefaultOrientation(), setAutoOrientEnabled(boolean), print(java.awt.Component)

getDefaultOrientation

public int getDefaultOrientation()
Get the default page orientation

Returns:
default page orientation
See Also:
setDefaultOrientation(int), print(java.awt.Component)

setDefaultMargins

public void setDefaultMargins(double top,
                              double bottom,
                              double left,
                              double right)
Sets the default margins (in inches) for subsequent calls to print(). Margins are subject to physical constraints of your printer. Your values may be adjusted to conform to your printer specifications.

Parameters:
top - margin at top of page in inches & @param bottom margin at bottom of page in inches
left - margin at left of page in inches
right - margin at right of page in inches
See Also:
print(java.awt.Component)

setDefaultMaxWidth

public void setDefaultMaxWidth(int pages)
Sets the default maximum width (in pages) of a print request. Print requests will be scaled so as to not extend beyond this limit.

Parameters:
pages - the default width or constant UNLIMITED_PAGES default is UNLIMITED_PAGES.
See Also:
getDefaultMaxWidth(), print(java.awt.Component)

getDefaultMaxWidth

public int getDefaultMaxWidth()
Returns the default width (in pages).

Returns:
default maximum width in pages
See Also:
setDefaultMaxWidth(int), print(java.awt.Component)

setDefaultMaxHeight

public void setDefaultMaxHeight(int pages)
Sets the default maximum height (in pages) of a print request. The resulting image will be scaled to fit within these constraints.

Parameters:
pages - the default height or constant UNLIMITED_PAGES. Default is UNLIMITED_PAGES.
See Also:
getDefaultMaxHeight(), print(java.awt.Component)

getDefaultMaxHeight

public int getDefaultMaxHeight()
Returns the default maximum height (in pages).

Returns:
default maximum height in pages
See Also:
setDefaultMaxHeight(int), print(java.awt.Component)

setDefaultPaperSize

public void setDefaultPaperSize(double width,
                                double height)
Sets the default paper size (in inches). Paper sizes are subject to physical constraints of your printer. Your values may be adjusted to conform to your printer specifications.

Parameters:
width - paper width in inches
height - paper width in inches
See Also:
getDefaultPaperWidth(), getDefaultPaperHeight()

getDefaultPaperWidth

public double getDefaultPaperWidth()
Gets the default paper width (in inches).

Returns:
paper width in inches
See Also:
setDefaultPaperSize(double, double)

getDefaultPaperHeight

public double getDefaultPaperHeight()
Gets the default paper height (in inches).

Returns:
paper height in inches
See Also:
setDefaultPaperSize(double, double)

setNativePrinterSetupEnabled

public void setNativePrinterSetupEnabled(boolean enabled)
Enables or disables the native printer Setup Dialog. The Printer Setup Dialog is a platform specific dialog. The Printer class has no control over its look and feel. If your application requires access to more than one printer, or if you want to be able to access the paper source, you will need to have this feature activated.

Parameters:
enabled - true or false. The default value is false.
See Also:
isNativePrinterSetupEnabled()

isNativePrinterSetupEnabled

public boolean isNativePrinterSetupEnabled()
Indicates whether or not the native Printer Setup Dialog is enabled.

Returns:
status of the native Printer Setup Dialog
See Also:
setNativePrinterSetupEnabled(boolean)

setNativePageSetupEnabled

public void setNativePageSetupEnabled(boolean enabled)
Enables or disables the native Page Setup Dialog The native Page Setup Dialog is a platform specific dialog. The Printer class has no control over its look and feel. This dialog may or may not recognize your default settings for (margins, orientation, etc...). This is dependent upon the implementation and printer configuration.

Parameters:
enabled - true or false. The default value is false.
See Also:
setPageSetupEnabled(boolean), isNativePageSetupEnabled()

isNativePageSetupEnabled

public boolean isNativePageSetupEnabled()
Indicates whether or not the native page setup Dialog is enabled.

Returns:
status of the native printer Dialog
See Also:
setNativePageSetupEnabled(boolean)

setPageSetupEnabled

public void setPageSetupEnabled(boolean enabled)
Enables or disables the class Page Setup Dialog

Parameters:
enabled - true or false. The default value is true.
See Also:
isPageSetupEnabled()

isPageSetupEnabled

public boolean isPageSetupEnabled()
Indicates whether or not the Page Setup Dialog is enabled.

Returns:
status of the Page Setup Dialog
See Also:
setPageSetupEnabled(boolean)

setAutoOrientEnabled

public void setAutoOrientEnabled(boolean enable)
Enables and disables automatic orientation changes. If enabled the page will be oriented according to how it will best accomodate the component being printed.

Parameters:
enable - true or false. The default value is true.
See Also:
isAutoOrientEnabled()

isAutoOrientEnabled

public boolean isAutoOrientEnabled()
Indicates whether or not automatic page orientation is enabled whether or not automatic orientation adjustments are enabled

See Also:
setAutoOrientEnabled(boolean)

getDefaultTextScale

public double getDefaultTextScale()
Get default scaling for text components.

See Also:
setDefaultTextScale(double)

setDefaultTextScale

public void setDefaultTextScale(double scale)
Set default scaling for text components.

Parameters:
scale - scaling value to be used by default
See Also:
getDefaultTextScale()

setDefaultTextFormatingEnabled

public void setDefaultTextFormatingEnabled(boolean enable)
Enables and disables text formating on text components. If enabled the Printer class will attempt to find best fit page breaks when the component being printed is a JTextComponent.

Parameters:
enable - true or false. The default value is true.
See Also:
isDefaultTextFormatingEnabled()

isDefaultTextFormatingEnabled

public boolean isDefaultTextFormatingEnabled()
Indicates whether or not text formating is enabled whether or not text formating is enabled

See Also:
setDefaultTextFormatingEnabled(boolean)

isFormatedAsText

public boolean isFormatedAsText(java.awt.Component comp)
Indicates whether or not the given component will be formatted as text.

Returns:
whether or not the component will be formatted as text
See Also:
setDefaultTextFormatingEnabled(boolean), isDefaultTextFormatingEnabled()

setDefaultAnchor

public void setDefaultAnchor(int anchor)
Sets the default position of how the component should be placed within the page or pages. Default is NORTHWEST.

Parameters:
anchor - one of 9 positions (e.g. NORTH, NORTHEAST, ...)
See Also:
getDefaultAnchor()

getDefaultAnchor

public int getDefaultAnchor()
Gets the default anchor position

See Also:
setDefaultAnchor(int)

restoreDefaults

public void restoreDefaults()
Restore printer values to their defaults.


print

public boolean print(java.awt.Component comp)
Print the given component.

Parameters:
comp - Component to be printed
Returns:
returns true if print successful, false otherwise.

print

public boolean print(java.awt.Component comp,
                     int copies)
Print the given number of copies of the given component.

Parameters:
comp - Component to be printed
copies - number of copies to be printed
Returns:
returns true if print successful, false otherwise.

print

public boolean print(java.awt.Frame frame,
                     java.awt.Component comp,
                     int copies)
Print the given number of copies of the given component using the given base Frame. base Frame for any subsequent dialogs, overrides that which was given in the constructor.

Parameters:
frame - base Frame used for dialogs
comp - Component to be printed
copies - number of copies to be printed
Returns:
returns true if print successful, false otherwise.