ewe.fx.print
Class PrintableObject

java.lang.Object
  extended byewe.fx.print.PrintableObject
All Implemented Interfaces:
Printable

public abstract class PrintableObject
extends Object
implements Printable

A very useful base class for implementing a Printable object, which uses PrintPage objects to paint to each page.


Field Summary
protected  PageFormat currentPageFormat
          This is the PageFormat for the page being printed now.
protected  PageRect currentPageRect
          This is the PageRect that is currently being drawn on.
protected  PrintPage currentPrintPage
          This is the PrintPage being used for the current page.
protected  double defaultBlockSize
           
protected  double drawDPI
          This is 360 by default, but you can change it to any value you want.
protected  boolean useImageableAreaOnly
          By default whenever a new page is started a PrintPage is created that covers the entire area.
 
Fields inherited from interface ewe.fx.print.Printable
UNKNOWN_NUMBER_OF_PAGES
 
Constructor Summary
PrintableObject()
           
 
Method Summary
protected  void adjustForPen(double penSize, Rect source)
           
protected  Rect adjustForPen(double penSize, Rect source, Rect destination)
          A Pen size that is greater than 1 will result in lines that extend to either side of the actual line.
 int countPages(PageFormat pf)
          By default this returns UNKNOWN_NUMBER_OF_PAGES.
protected  PointRect getAreaFor(int pageIndex)
          This should return the area on the specified page that you want to draw on.
protected  PointRect getCurrentArea()
          Get the current area within the current PrintPage (not within the current page).
protected  PointRect getCurrentFullPage()
          Return the full size of the current page in points.
protected  PointRect getCurrentImageableArea()
          Return the current page imageable area in points.
protected  int getHintsFor(int pageIndex)
          By default this returns 0.
protected  PrintPage getPrintPageFor(PrintSurface printSurface, int pageIndex)
          This is done at the start of each page.
protected  double getXdpiFor(int pageIndex)
           
protected  double getYdpiFor(int pageIndex)
           
protected  boolean isWithinCurrentArea(PointRect pr)
          Return if the specified area given in Points, relative to the top-left point of the current print area (as selected by getAreaFor) is within the current PageRect buffer.
protected  boolean print(Handle h, Graphics g, int pageIndex)
          This is the main method you must override.
 boolean print(Handle handle, PrintSurface printSurface, PageFormat page, int pageIndex)
          Prints the page at the specified index into the specified Graphics context in the specified format.
protected  int scaleDimension(double pointSize)
          Scale a dimension, width or height value from point size to pixels for the current PrintPage.
protected  Rect scaleToPixels(PointRect pr, Rect dest)
          Scale the specified PointRect to integer pixel co-ordinates for the current PageRect area.
abstract  boolean validatePage(PageFormat pf, int pageIndex)
          You must override to indicate whether a page is valid or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

drawDPI

protected double drawDPI
This is 360 by default, but you can change it to any value you want. It is used by getXdpiFor() and getYdpiFor().


defaultBlockSize

protected double defaultBlockSize

useImageableAreaOnly

protected boolean useImageableAreaOnly
By default whenever a new page is started a PrintPage is created that covers the entire area. However if this variable is set true, the the PrintPage will cover the imageable area only.


currentPageFormat

protected PageFormat currentPageFormat
This is the PageFormat for the page being printed now.


currentPageRect

protected PageRect currentPageRect
This is the PageRect that is currently being drawn on.


currentPrintPage

protected PrintPage currentPrintPage
This is the PrintPage being used for the current page.

Constructor Detail

PrintableObject

public PrintableObject()
Method Detail

getCurrentImageableArea

protected PointRect getCurrentImageableArea()
Return the current page imageable area in points. The x and y co-ordinats of the returned PointRect will always be 0,0.


getCurrentFullPage

protected PointRect getCurrentFullPage()
Return the full size of the current page in points. The x and y co-ordinats of the returned PointRect will always be 0,0.


getXdpiFor

protected double getXdpiFor(int pageIndex)

getYdpiFor

protected double getYdpiFor(int pageIndex)

getAreaFor

protected PointRect getAreaFor(int pageIndex)
This should return the area on the specified page that you want to draw on. By default this will return a PointRect equal to the entire area of the page, unless useImageableAreaOnly is true, in which case it will cover the imageable area of the currentPageFormat.


getHintsFor

protected int getHintsFor(int pageIndex)
By default this returns 0.


getPrintPageFor

protected PrintPage getPrintPageFor(PrintSurface printSurface,
                                    int pageIndex)
This is done at the start of each page. By default this method will create a PrintPage that will cover the area returned by getAreaFor() using the dpis as returned by getXdpiFor() and getYdpiFor(), but you can override this to get a PrintPage in a different way.


scaleToPixels

protected Rect scaleToPixels(PointRect pr,
                             Rect dest)
Scale the specified PointRect to integer pixel co-ordinates for the current PageRect area.


isWithinCurrentArea

protected boolean isWithinCurrentArea(PointRect pr)
Return if the specified area given in Points, relative to the top-left point of the current print area (as selected by getAreaFor) is within the current PageRect buffer. If it is not then you should not attempt to draw anything within that area.


getCurrentArea

protected PointRect getCurrentArea()
Get the current area within the current PrintPage (not within the current page).


scaleDimension

protected int scaleDimension(double pointSize)
Scale a dimension, width or height value from point size to pixels for the current PrintPage. In this case the result will always be at least 1 pixel.


adjustForPen

protected Rect adjustForPen(double penSize,
                            Rect source,
                            Rect destination)
A Pen size that is greater than 1 will result in lines that extend to either side of the actual line. This method will adust the bounds of a Rect such that when it is drawn with a specific pen size (in points)


adjustForPen

protected void adjustForPen(double penSize,
                            Rect source)

print

protected boolean print(Handle h,
                        Graphics g,
                        int pageIndex)
This is the main method you must override. The default implementation draws a box with diagonals across the printable area of the page.


print

public final boolean print(Handle handle,
                           PrintSurface printSurface,
                           PageFormat page,
                           int pageIndex)
Description copied from interface: Printable
Prints the page at the specified index into the specified Graphics context in the specified format. A PrinterJob calls the Printable interface to request that a page be rendered into the context specified by graphics. The format of the page to be drawn is specified by pageFormat. The zero based index of the requested page is specified by pageIndex.

Within this method you should call printSurface.getPageRect() to get a Rectangular window onto the virtual printer page. You can then draw into the image provided by that PageRect and then afterwards, transfer the PageRect to the printer using printSurface.putPageRect().

The PageMosaic class is an excellent class for allowing you to draw onto an arbitrary section on the output page.

Specified by:
print in interface Printable
Parameters:
handle - a Handle that an outside thread can use to monitor the printing. During the printing you should check the shouldStop value of the Handle, and if it is true, then you should abort printing and return false.
printSurface - A printSurface that you should call getPageRect() on in order to get buffers to draw on.
page - the format for the page you are printing on.
pageIndex - the index of the page you are printing on, starting from index 0.
Returns:
true if printing was successful, false if the shouldStop value of handle was set.

countPages

public int countPages(PageFormat pf)
By default this returns UNKNOWN_NUMBER_OF_PAGES.

Specified by:
countPages in interface Printable

validatePage

public abstract boolean validatePage(PageFormat pf,
                                     int pageIndex)
You must override to indicate whether a page is valid or not. Returning false will end printing. The pageIndex starts from 0 for the first page.

Specified by:
validatePage in interface Printable