Solved

Why is print called twice in

Posted on 2001-08-01
5
1,291 Views
Last Modified: 2006-11-17
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
0
Comment
Question by:kmenzel
  • 3
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6340654
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."

0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6340664
0
 
LVL 5

Expert Comment

by:mbormann
ID: 6346022
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.
0
 

Author Comment

by:kmenzel
ID: 6349412
Thanks for the help. I especially liked mbormann's chapter on printing.
0
 
LVL 92

Expert Comment

by:objects
ID: 6350507
Thanks for the points :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now