views:

57

answers:

1

if you run the following code(from http://www.javacommerce.com/displaypage.jsp?name=printcode.sql&id=18252), both pages are called twice

class PrintObject implements Printable
{
public int print (Graphics g, PageFormat f, int pageIndex)
{
System.out.println("Page "+pageIndex);
Graphics2D g2 = (Graphics2D) g; // Allow use of Java 2 graphics on
// the print pages :

// A rectangle that shows the printable area of the page, allowing
// for margins all round. To be drawn on the first page (index = 0).
Rectangle2D rect = new Rectangle2D.Double(f.getImageableX(),
f.getImageableY(),
f.getImageableWidth(),
f.getImageableHeight());

// A simple circle to go on the second page (index = 1).
Ellipse2D circle = new Ellipse2D.Double(100,100,100,100);

switch (pageIndex)
{
case 0 : g2.setColor(Color.black); // Page 1 : print a rectangle
g2.draw(rect);
return PAGE_EXISTS;
case 1 : g2.setColor(Color.red); // Page 2 : print a circle
g2.draw(circle);
return PAGE_EXISTS;
default: return NO_SUCH_PAGE; // No other pages
}
}


public static void main (String[] args)
{
// Create an object that will hold all print parameters, such as
// page size, printer resolution. In addition, it manages the print
// process (job).
PrinterJob job = PrinterJob.getPrinterJob();

// It is first called to tell it what object will print each page.
job.setPrintable(new PrintObject());

// Then it is called to display the standard print options dialog.
if (job.printDialog())
{
// If the user has pressed OK (printDialog returns true), then go
// ahead with the printing. This is started by the simple call to
// the job print() method. When it runs, it calls the page print
// object for page index 0. Then page index 1, 2, and so on
// until NO_SUCH_PAGE is returned.
try { job.print(); }
catch (PrinterException e) { System.out.println(e); }
}
}
}

I don't know why it would do this in such a simple case but the real problem is that we have code that prints a large JTable, and it retries printing over 500 times before it succeeds. Even for a relatively small JTable it retries 11 times. We are using java.awt.print.PrintJob, turned off double buffering and testing using CutePDF on a machine that has 2G RAM and 1G Heap(-Xmx1000m)

Does anyone know what would cause so many retries?

Thank you for any help Howard

A: 

I assume you're talking about how often it is calling the print(...) method for each page.

Unfortunately, there's no real way of know how many times it will be called, much less controlling it, as it's up the the printing subsystem of the JVM for your system.

All you can do is do your best to optimize the drawing routine knowing that it can be called more than once (or even excessively).

You should perhaps look at the Graphic2D clipBounds as well, it may tell you what subset of the overall page is being printed.

On my simple test, on a Mac, using your code, print was called twice for each page. Once with a Graphics that looks like its used to get an idea about what you're about to print and then another time, with the actual system Graphics2D to do that actual rendering.

Will Hartung