Solved

Why is print called twice in

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mockito example issue 8 72
Java Jpanels and Jframe 8 33
replace a word with other 1 44
How do I remove an object from a 3 25
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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