JDK-4186111 : AWT Print engine calls print() twice with same page, ignoring first rendering
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 1998-10-30
  • Updated: 1999-01-19
  • Resolved: 1999-01-19
Related Reports
Relates :  
Description

Name: tb29552			Date: 10/30/98


/*
Using the Pageable approach, the awt print
engine calls the print() method twice.
Anything that is drawn to the graphics
context in the first call is lost, only
the second rendering shows up in the output.

If the print engine needs to call the print()
method twice for a page, the API documentation
indicates that it should do so with different
clipping each time, in order to allow for
banded printing.  In this case, it should either
call print() once, or use different clip regions.

In any case, it should NEVER ask to have
something rendered that doesn't make it to
the page.

The following code demonstrates this on
both Solaris 2.5.1 and Windows NT 4.0
Service Pack 3.

Note that the first pass through the print()
method should draw a grid.  The second pass
draws an ellipse.  The ellipse is the only
thing that shows up in the printed output.

------ TestPrint.java -------
*/

import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;

public class TestPrint  {
    public static void main(String[] argv)
    {
        Report report = new Report();

        report.print();
    }
}

class Report  {
    public void print()
    {
        PrinterJob pj = PrinterJob.getPrinterJob();

        pj.setJobName("Test Report");

        PageFormat pf = pj.defaultPage();

        Book book = createBook(pf);

        pj.setPageable(book);

        if (pj.printDialog())
        {
            try
            {
                pj.print();
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }

    private Book createBook(PageFormat format)
    {
        Book retval = new Book();

        Printable printable = new Printable()
        {
            int count = 0;

            public int print(Graphics g, PageFormat pf, int pi)
            {
                System.out.println("print() called...");
                System.out.println("pi = " + pi + " clip = " + g.getClipBounds());

                g.setColor(Color.black);

                Graphics2D g2d = (Graphics2D) g;

                g2d.setStroke(new BasicStroke(0.01 f));

                double x = pf.getImageableX();
                double y = pf.getImageableY();
                double width = pf.getImageableWidth();
                double height = pf.getImageableHeight();

                switch (count)
                {
                case 0:
                    Line2D.Double line = new Line2D.Double();

                    for (double i = x; i <= x + width; i += 1.0)
                    {
                        line.setLine(i, y, i, y + height);
                        g2d.draw(line);
                    }

                    for (double i = y; i <= y + height; i += 1.0)
                    {
                        line.setLine(x, i, x + width, i);
                        g2d.draw(line);
                    }

                    break;

                case 1:
                    Ellipse2D.Double ellipse =
                        new Ellipse2D.Double(x, y, width, height);

                    g2d.draw(ellipse);

                    break;
                }

                count++;

                return Printable.PAGE_EXISTS;
            }
        };

        retval.append(printable, format);

        return retval;
    }
}

(Review ID: 41695)
======================================================================

Comments
PUBLIC COMMENTS .
10-06-2004

EVALUATION I could not find which documentation the user was referring to, but the behavior is entirely intentional and not at all wrong. The documentation should indicate that banding is only one possible reason why the print method would be called multiple times. In fact, the first time the method is called is never rendered since it is a "discovery phase" and is only used to determine which form of printer output should be used for a given page. If the user could indicate which documentation was confusing about this, we could clear it up. jim.graham@Eng 1998-11-20 ===================================================== Submitter was contacted on 23 November and again on 14 December 1998. No response. Based on the evaluation above, I am closing this as "not a bug". If a reference to the relevant documentation becomes available, this can be re-opened or turned into a documentation bug. tim.bell@Eng 1999-01-19
19-01-1999