public abstract class ExtendedPrintable extends Object
Printable which provides additional information and options
which also provides useful information when saving to an image.
May be supplied by a DocumentViewer
The following example shows how to construct an ExtendedPrintable as an anonymous inner class.
Note that this code comes from the example in
DocumentViewerFactory and assumes that there is a global String variable called residues present.
final Font font = new Font("monospaced",Font.PLAIN,48);
ExtendedPrintable ep = new ExtendedPrintable(){
//This Options appears at the top of the print options dialog that is opened when
//the user selects "Print"
public Options getOptions(boolean isSavingToFile) {
Options options = new Options(this.getClass());
options.addLabel("It is ludicrous to print this");
return options;
}
//we need to be aware of what page we are on, and print the correct section of the sequence.
//in a more robust implementation we would deal with characters being split between two pages.
public int print(Graphics2D g, Dimension d, int pageIndex, Options options) throws PrinterException {
//clear the background
g.setColor(Color.white);
g.fillRect(0,0,d.width,d.height);
//set a clip to make sure that the graphics does not draw outside the printable area
g.setClip(0,0,d.width,d.height);
//move the graphics so that the correct section of the sequence is drawn to the page
g.translate(-d.width*pageIndex,0);
//paint the page
g.setColor(Color.black);
g.setFont(font);
g.drawString(residues,10,50);
//tell the printing subsystem whether the page requested was a valid page or not.
return (getPagesRequired(d, null) >= pageIndex)? Printable.PAGE_EXISTS : Printable.NO_SUCH_PAGE;
}
//these two methods are used by the save to image feature
public int getRequiredWidth(Options options) {
FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
Rectangle2D fontBounds = font.getStringBounds(residues, frc);
return (int)fontBounds.getWidth()+20;
}
//these two methods are used by the save to image feature
public int getRequiredHeight(Options options, int width) {
FontRenderContext frc = new FontRenderContext(new AffineTransform(), false, false);
Rectangle2D fontBounds = font.getStringBounds(residues, frc);
return (int)fontBounds.getHeight()+60;
}
//returns the number of pages required to print the entire document.
public int getPagesRequired(Dimension dimensions, Options options) {
return (int)Math.ceil(((double)getRequiredWidth(null))/dimensions.width);
}
};
| Modifier and Type | Class and Description |
|---|---|
static class |
ExtendedPrintable.Factory
Provides factory methods for creating ExtendedPrintables
|
static class |
ExtendedPrintable.Wrapper
Wrapper that delegates all methods to the source ExtendedPrintable.
|
| Constructor and Description |
|---|
ExtendedPrintable() |
| Modifier and Type | Method and Description |
|---|---|
int |
getDefaultHeight(int width,
Options options)
If this component does not have a required height (i.e.
|
int |
getDefaultWidth(Options options)
If this component does not have a required width (i.e.
|
String |
getDocumentName()
Returns the name of the document that we are printing (if applicable)
|
int |
getMaximumHeight(int width,
Options options)
Get the maximum height (inclusive) of the component when saved as an image.
|
int |
getMaximumWidth(Options options)
Get the maximum width (inclusive) of the component when saved as an image.
|
int |
getMinimumHeight(int width,
Options options)
Get the minimum height (inclusive) of the component when saved as an image.
|
int |
getMinimumWidth(Options options)
Get the minimum width (inclusive) of the component when saved as an image.
|
Options |
getOptions(boolean isSavingToFile)
Optionally provide additional printing and/or saving image to file options.
|
abstract int |
getPagesRequired(Dimension dimensions,
Options options) |
int |
getRequiredHeight(int width,
Options options)
Deprecated.
|
int |
getRequiredWidth(Options options)
Deprecated.
|
abstract int |
print(Graphics2D graphics,
Dimension dimensions,
int pageIndex,
Options options)
print a single page of this component.
|
public Options getOptions(boolean isSavingToFile)
isSavingToFile - true when saving to a file, false when printingpublic String getDocumentName()
public abstract int print(Graphics2D graphics, Dimension dimensions, int pageIndex, Options options) throws PrinterException
graphics - the graphics to render to.dimensions - the dimensions of the page to be printed to.pageIndex - index of the page to be printed or saved as an image.options - an options panel returned by getOptions(boolean)Printable.PAGE_EXISTS or Printable.NO_SUCH_PAGEPrinterException - the printerexception@Deprecated public int getRequiredWidth(Options options)
getMinimumWidth(Options), getMaximumWidth(Options) and getDefaultWidth(Options) instead.options - an options panel returned by getOptions(boolean)public int getDefaultWidth(Options options)
getRequiredWidth(Options) returned 0)), then this
returns the default width for the component, which the user may adjust.options - an options panel returned by getOptions(boolean)getMinimumWidth(Options),
getMinimumWidth(Options),
getDefaultHeight(int, Options)public int getMaximumWidth(Options options)
options - an options panel returned by getOptions(boolean)public int getMinimumWidth(Options options)
options - an options panel returned by getOptions(boolean)@Deprecated public int getRequiredHeight(int width, Options options)
getMinimumWidth(Options), getMaximumWidth(Options) and getDefaultWidth(Options) instead.options - an options panel returned by getOptions(boolean)width - a width chosen either by the user or returned from getRequiredWidth(Options)public int getDefaultHeight(int width,
Options options)
getRequiredHeight(int, Options) returned 0), then this
returns the default height for the component, which the user may adjust.width - a width chosen either by the user or returned from getDefaultWidth(Options)options - an options panel returned by getOptions(boolean)getMinimumHeight(int, Options),
getMaximumHeight(int, Options),
getDefaultWidth(Options)public int getMaximumHeight(int width,
Options options)
width - a width chosen either by the user or returned from getDefaultWidth(Options)options - an options panel returned by getOptions(boolean)public int getMinimumHeight(int width,
Options options)
width - a width chosen either by the user or returned from getDefaultWidth(Options)options - an options panel returned by getOptions(boolean)public abstract int getPagesRequired(Dimension dimensions, Options options)
dimensions - the dimensionsoptions - an options panel returned by getOptions(boolean)