Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4197648

Printing moderatly large images takes FOREVER...



    • Subcomponent:
    • CPU:
      generic, x86, sparc
    • OS:
      generic, solaris_2.5.1, windows_95, windows_nt


                                   g2.translate(0f, -headerHeightOnPage);
                                   g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage),
                                   tableView.getTableHeader().paint(g2);//paint header at top

                                   return Printable.PAGE_EXISTS;

                              public static void main(String[] args) {
                                   new Report();
      (Review ID: 94012)

      Name: diC59631 Date: 12/15/98

      This program simply opens a jpeg file ?youngdad.jpeg? (an image from your online tutorial, but any large jpeg or gif file will do) and
      prints it to the selected printer, after the print dialog box is presented to the user. I?m using java 1.2RC2, with the Microsoft?s HP
      LaserJet5 unidriver Version 3.37, on NT4.0 (service pack 3). The problem, assuming this code correctly constructed for printing an
      image, is that it takes FOREVER to print, doesn?t always print, and consumes all of the virtual memory on my machine
      (over 119 megabytes). There is something definitely wrong here. I have seen other bug reports on performance and images, but I
      couldn?t find any with printing images, on RC2. I expect some performance degradation using Java, but not what I am seeing here.
      Particularly if we are considering using Java for rapid application and tool developnent (which we are). As a point of reference printing
      the same image on a blank page using Word all other parameters the same the whole thing takes 20 seconds, click to clunk, and
      memory usage barely hi-cups.

      import java.awt.*;
      import java.awt.print.*;
      import java.awt.print.PrinterJob;

      public class PrintTest extends Canvas implements Printable

       Image img;

       public PrintTest()
        String s = "youngdad.jpeg";
        img = getToolkit().getImage(s);

       public void paint(Graphics g)
        g.drawImage(img, 100, 100,null);

       public int print(Graphics g, PageFormat pf, int pi) throws PrinterException
        if(pi > 0) return Printable.NO_SUCH_PAGE;
         g.drawImage(img, 100, 100, null);
          return Printable.PAGE_EXISTS;

       public static void main(String[] args)

        PrintTest PT = new PrintTest();
        PrinterJob printJob = PrinterJob.getPrinterJob();
        PageFormat pageFormat = printJob.defaultPage();
        Frame f = new Frame("Printing Example");
        f.setSize(300, 200);
        f.add("Center", PT);
        PT.setSize(300, 200);

        if (printJob.printDialog())
         catch (Exception e)
      (Review ID: 47286)

      Name: krT82822 Date: 04/07/99

      I'm printing a contents of a JInternalFrame:

        public int print(Graphics g, PageFormat pf, int pageIndex) {
          if (pageIndex != 0) return Printable.NO_SUCH_PAGE;
          Graphics2D g2 = (Graphics2D)g;
          g2.translate(pf.getImageableX(), pf.getImageableY());
          return Printable.PAGE_EXISTS;

      This ContentPane containes a class for displaying 2D plots (about 1500 data points)

      The printing itself takes too long (about 3 meg printing file),
      and after it finished I could not open any program in Win95-
      it gives me "low memory- try to close some windows" message.
      Closing of java DO NOT solve the problem-
      memory leak persists even after closing jre.
      The only solution i found- windows restart

      Name: krT82822 Date: 08/10/99

      Concerns: java.awt.print.*

      Bug or feature?

      PrinterJob.print( ) calls
      Printable.print( Graphics g, PageFormat pf, int pageIndex )
      Annoying when you do some calculations (writing a bill!) in the
      print( g, pf, pageIndex )-method. It also takes twice the time
      to print...

      Sample code:
      /* file doubleprint.java */
      // Interfaces
      import java.awt.print.Printable;
      // Classes
      import java.awt.Graphics;
      import java.awt.print.PageFormat;
      import java.awt.print.PrinterJob;
      // Exceptions
      import java.awt.print.PrinterException;
      public class doubleprint {
        public doubleprint( ) {
          PrinterJob pj = PrinterJob.getPrinterJob( );
          pj.setPrintable( new testprint( ) );
          if ( pj.printDialog( ) ) {
            try {
      pj.print( );
            } catch ( PrinterException ex ) {
      System.err.println( "Error while printing!" );
      System.exit( 1 );
          System.exit( 0 );
        public static void main( String[ ] args ) {
          new doubleprint( );
      class testprint implements Printable {
        int i=0;
        public int print( Graphics g, PageFormat pf, int pageIndex ) throws PrinterException {
          if ( pageIndex>0 ) return Printable.NO_SUCH_PAGE;
          System.out.println( "Printing page number "+pageIndex+", count: "+i );
          return Printable.PAGE_EXISTS;
      (Review ID: 93721)

      Name: skT88420 Date: 08/16/99

      Windows 98

      java -version:
      java version "1.2", HotSpot VM (1.0fcs, mixed mode, build E)

      java -fullversion:
      JAVA.EXE full versino "JDK-1.2-V"

      HP LaserJet 4V

      I am not able to print images. This test program just hangs on the 'g.drawImage' line:


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

      class Test implements Printable {

      public static void main(String[] args) {

      PrinterJob job = PrinterJob.getPrinterJob();
      Book book = new Book();
      PageFormat format = new PageFormat();
      book.append(new Test(), format, 1);

      if(job.printDialog()) {
      try {
      catch(PrinterException e) {

      public int print(Graphics graphics,
                               PageFormat pageFormat,
                               int pageIndex)
      if(pageIndex != 0)
      return NO_SUCH_PAGE;

      System.out.println("Creating image...");

      Image img =

      System.out.println("Image created.");

      System.out.println("Drawing image...");

                        (int) pageFormat.getImageableX(),
      (int) pageFormat.getImageableY(),

      System.out.println("Image drawn.");

      return PAGE_EXISTS;

      Running the program gives:

      c:\java Test
      Createing image...
      Image created.
      Drawing image...

      ...and then -> Nothing! And nothing on the printer :(
      (Review ID: 93955)

      Name: skT88420 Date: 08/17/99

      taking your example from:

      replacing first column in JTable with an ImageIcon,
      (outcommented lines in "final Object[][] data")
      will cause no output when printing!

                           import javax.swing.*;
                           import javax.swing.table.*;
                           import java.awt.print.*;
                           import java.util.*;
                           import java.awt.*;
                           import java.awt.event.*;
                           import java.awt.geom.*;
                           import java.awt.Dimension;

                           public class Report implements Printable{
                             JFrame frame;
                             JTable tableView;

                             public Report() {
                               frame = new JFrame("Sales Report");
                               frame.addWindowListener(new WindowAdapter() {
                               public void windowClosing(WindowEvent e) {System.exit(0);}});

                               final String[] headers = {"Description", "open price",
                                   "latest price", "End Date", "Quantity"};
                               final Object[][] data = {
                                   {new ImageIcon(Toolkit.getDefaultToolkit().getImage("javalogo.gif")), "1.00", "4.99", new Date(), new Integer(2)},
                                   {new ImageIcon(Toolkit.getDefaultToolkit().getImage("javalogo.gif")), "0.10", "0.14", new Date(), new Integer(1)},
                                   {new ImageIcon(Toolkit.getDefaultToolkit().getImage("javalogo.gif")), "1.00", "2.49", new Date(), new Integer(1)},
                                   {new ImageIcon(Toolkit.getDefaultToolkit().getImage("javalogo.gif")), "1.00", "1.49", new Date(), new Integer(1)},
                                   {new ImageIcon(Toolkit.getDefaultToolkit().getImage("javalogo.gif")), "4.00", "4.49", new Date(), new Integer(1)},
                                   {new ImageIcon(Toolkit.getDefaultToolkit().getImage("javalogo.gif")), "1.00", "2.29", new Date(), new Integer(5)}
      /* {"Box of Biros", "1.00", "4.99", new Date(), new Integer(2)},
                                   {"Blue Biro", "0.10", "0.14", new Date(), new Integer(1)},
                                   {"legal pad", "1.00", "2.49", new Date(), new Integer(1)},
                                   {"tape", "1.00", "1.49", new Date(), new Integer(1)},
                                   {"stapler", "4.00", "4.49", new Date(), new Integer(1)},
                                   {"legal pad", "1.00", "2.29", new Date(), new Integer(5)}
      */ };

                               TableModel dataModel = new AbstractTableModel() {
                                   public int getColumnCount() { return headers.length; }
                                   public int getRowCount() { return data.length;}
                                   public Object getValueAt(int row, int col) {
                                           return data[row][col];}
                                   public String getColumnName(int column) {
                                           return headers[column];}
                                   public Class getColumnClass(int col) {
                                           return getValueAt(0,col).getClass();}
                                   public boolean isCellEditable(int row, int col) {
                                           return (col==1);}
                                   public void setValueAt(Object aValue, int row, int column) {
                                           data[row][column] = aValue;

                                tableView = new JTable(dataModel);
                                JScrollPane scrollpane = new JScrollPane(tableView);

                                scrollpane.setPreferredSize(new Dimension(500, 80));
                                frame.getContentPane().setLayout(new BorderLayout());
                                JButton printButton= new JButton();

                                printButton.setText("print me!");


                                // for faster printing turn double buffering off


                                printButton.addActionListener( new ActionListener(){
                                   public void actionPerformed(ActionEvent evt) {
                                     PrinterJob pj=PrinterJob.getPrinterJob();
                                     }catch (Exception PrintException) {}


                                public int print(Graphics g, PageFormat pageFormat,
                                   int pageIndex) throws PrinterException {
                                   Graphics2D g2 = (Graphics2D) g;
                                   int fontHeight=g2.getFontMetrics().getHeight();
                                   int fontDesent=g2.getFontMetrics().getDescent();

                                   //leave room for page number
                                   double pageHeight = pageFormat.getImageableHeight()-fontHeight;
                                   double pageWidth = pageFormat.getImageableWidth();
                                   double tableWidth = (double)
                                   double scale = 1;
                                   if (tableWidth >= pageWidth) {
                                           scale = pageWidth / tableWidth;

                                   double headerHeightOnPage=
                                   double tableWidthOnPage=tableWidth*scale;

                                   double oneRowHeight=(tableView.getRowHeight()+
                                   int numRowsOnAPage=
                                   double pageHeightForTable=oneRowHeight*numRowsOnAPage;
                                   int totalNumPages= (int)Math.ceil((
                                   if(pageIndex>=totalNumPages) {
                                                 return NO_SUCH_PAGE;

                           //bottom center
                                   g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35,


                                   //If this piece of the table is smaller than the size available,
                                   //clip to the appropriate bounds.
                                   if (pageIndex + 1 == totalNumPages) {
                                      int lastRowPrinted = numRowsOnAPage * pageIndex;
                                      int numRowsLeft = tableView.getRowCount() - lastRowPrinted;
                                      g2.setClip(0, (int)(pageHeightForTable * pageIndex),
                                        (int) Math.ceil(tableWidthOnPage),
                                        (int) Math.ceil(oneRowHeight * numRowsLeft));
                                   //else clip to the entire area available.
                                        g2.setClip(0, (int)(pageHeightForTable*pageIndex),
                                        (int) Math.ceil(tableWidthOnPage),
                                        (int) Math.ceil(pageHeightForTable));



          Issue Links



              prr Philip Race
              dindrigo Daniel Indrigo (Inactive)
              0 Vote for this issue
              1 Start watching this issue