Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4186111

AWT Print engine calls print() twice with same page, ignoring first rendering

XMLWordPrintable

    • 2d
    • generic
    • generic



      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)
      ======================================================================

            mpeircesunw Michael Peirce (Inactive)
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: