Solved

Why is print called twice in

Posted on 2001-08-01
5
1,332 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jsp login check 12 52
servlet and mdb, jms error 1 54
Java class and jar 3 41
Strange router problem - can't access hotmail.com 14 35
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

713 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