Solved

Why is print called twice in

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
HashTable highest marks enumeration alternative 9 42
Adding multiple JVM environments to RedHat 6 7 46
java mysql insert application 14 25
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

809 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