Link to home
Start Free TrialLog in
Avatar of kmenzel
kmenzel

asked on

Why is print called twice in

I am implementing my first program that does printing in Java. I tried an example that should work great for printing a continuous stream of data (multiple pages) from a database. I noticed that the print() method is called twice for every page. Why is this? It makes printing a result set difficult. Can someone point me to more information on why this happens and possibly how to get called only once per page? Can I ignore one of the calls or do I have to do the exact same thing on each call? Example below.

Thanks for the help,
Kerry

/**
 * Class: Example1 <p>
 *
 * @author Jean-Pierre Dube <jpdube@videotron.ca>
 * @version 1.0
 * @since 1.0
 * @see Printable
 */

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


public class Example1 implements Printable {

   //--- Private instances declarations
   private final double INCH = 72;



   /**
    * Constructor: Example1 <p>
    *
    */
   public Example1 () {
     
      //--- Create a printerJob object
      PrinterJob printJob = PrinterJob.getPrinterJob ();

      //--- Set the printable class to this one since we
      //--- are implementing the Printable interface
      printJob.setPrintable (this);
       
      //--- Show a print dialog to the user. If the user
      //--- click the print button, then print otherwise
      //--- cancel the print job
      if (printJob.printDialog()) {
         try {
            printJob.print();  
         } catch (Exception PrintException) {
            PrintException.printStackTrace();
         }
      }

   }


   /**
    * Method: print <p>
    *
    * This class is responsible for rendering a page using
    * the provided parameters. The result will be a grid
    * where each cell will be half an inch by half an inch.  
    *
    * @param g a value of type Graphics
    * @param pageFormat a value of type PageFormat
    * @param page a value of type int
    * @return a value of type int
    */
   public int print (Graphics g, PageFormat pageFormat, int page) {

      System.out.println("Page: " + page);
     
      int i;
      Graphics2D g2d;
      Line2D.Double line = new Line2D.Double ();

      //--- Validate the page number, we only print the first page
      if (page < 2) {

         //--- Create a graphic2D object a set the default parameters
         g2d = (Graphics2D) g;
         g2d.setColor (Color.black);

         //--- Translate the origin to be (0,0)
         g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
         
         g2d.drawString ("Page " + page, 72, 72 * 2);
         
         return (PAGE_EXISTS);
      }
      else
         return (NO_SUCH_PAGE);
   }
   
   
   public static void main (String args[]) {
            new Example1();
            System.exit (0);
    }

} //Example1
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Sun reckons it's a feature.

From jdk1.3/guide/2d/spec/j2d-print.fm2.html

"The printing system might request that a particular page be rendered more than once or request that pages be rendered out of order. The application must be able to generate the proper page image, no matter which page the printing system requests. In this respect, the printing system is similar to the window toolkit, which can request components to repaint at any time, in any order."

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbormann
mbormann

http://manning.spindoczine.com/sbe/files/uts2/Chapter22html/Chapter22.htm

In reality, it is often the case that print() will be called for each page more than once. From a draft of an overview of the Java Printing API: "This *callback* printing model is necessary to support printing on a wide range of printers and systems...This model also enables printing to a bitmap printer from a computer that doesn't have enough memory or disk space to buffer a full-page bitmap. In this situation, a page is printed as a series of small bitmaps or *bands*. For example, if only enough memory to buffer one tenth of a page is available, the page is divided into ten bands. The printing system asks the application to render each page ten times, once to fill each band. The application does not need to be aware of the number or size of the bands; it simply must be able to render each page when requested."

>>>
Can I ignore one of the calls or do I have to do the exact same thing on each call?

Do the exact same thing, that's what I did and it worked for me.

Cheers.
Avatar of kmenzel

ASKER

Thanks for the help. I especially liked mbormann's chapter on printing.
Thanks for the points :)