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

Printing moderatly large images takes FOREVER...

XMLWordPrintable

    • 2d
    • generic, x86, sparc
    • generic, solaris_2.5.1, windows_95, windows_nt

      scale,scale);
                                   tableView.paint(g2);
                                   g2.scale(1/scale,1/scale);
                                   g2.translate(0f,pageIndex*pageHeightForTable);
                                   g2.translate(0f, -headerHeightOnPage);
                                   g2.setClip(0, 0,(int) Math.ceil(tableWidthOnPage),
                                     (int)Math.ceil(headerHeightOnPage));
                                   g2.scale(scale,scale);
                                   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;
        else
        {
         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();
        printJob.setPrintable(PT,pageFormat);
       
        Frame f = new Frame("Printing Example");
        f.setSize(300, 200);
        f.add("Center", PT);
        PT.setSize(300, 200);
        f.show();


        if (printJob.printDialog())
        {
         try
         {
          printJob.print();
         }
         catch (Exception e)
         {
          System.out.println(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());
                
           getContentPane().printComponents(g2);
           
          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 )
      twice.
      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 );
          i++;
          return Printable.PAGE_EXISTS;
        }
      }
      (Review ID: 93721)
      ======================================================================

      Name: skT88420 Date: 08/16/99


      OS:
      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"

      Printer:
      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();
      format.setOrientation(PageFormat.PORTRAIT);
      book.append(new Test(), format, 1);
      job.setPageable(book);

      if(job.printDialog()) {
      try {
      job.print();
      }
      catch(PrinterException e) {
      e.printStackTrace();
      }
      }
      }

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

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

      Image img =
      Toolkit.getDefaultToolkit().
                        createImage(getClass().
                        getResource("javalogo.gif"));

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

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

      graphics.drawImage(
                        img,
                        (int) pageFormat.getImageableX(),
      (int) pageFormat.getImageableY(),
      null
                      );

      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:
       
      http://developer.java.sun.com/developer/onlineTraining/Programming/JDCBook/advprint.html

      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());
                                frame.getContentPane().add(BorderLayout.CENTER,scrollpane);
                                frame.pack();
                                JButton printButton= new JButton();

                                printButton.setText("print me!");

                                frame.getContentPane().add(BorderLayout.SOUTH,printButton);

                                // for faster printing turn double buffering off

                                RepaintManager.currentManager(
                                   frame).setDoubleBufferingEnabled(false);

                                printButton.addActionListener( new ActionListener(){
                                   public void actionPerformed(ActionEvent evt) {
                                     PrinterJob pj=PrinterJob.getPrinterJob();
                                     pj.setPrintable(Report.this);
                                     pj.printDialog();
                                     try{
                                       pj.print();
                                     }catch (Exception PrintException) {}
                                     }
                                   });

                                   frame.setVisible(true);
                                }

                                public int print(Graphics g, PageFormat pageFormat,
                                   int pageIndex) throws PrinterException {
                                   Graphics2D g2 = (Graphics2D) g;
                                   g2.setColor(Color.black);
                                   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)
                                           tableView.getColumnModel().getTotalColumnWidth();
                                   double scale = 1;
                                   if (tableWidth >= pageWidth) {
                                           scale = pageWidth / tableWidth;
                                   }

                                   double headerHeightOnPage=
                                                 tableView.getTableHeader().getHeight()*scale;
                                   double tableWidthOnPage=tableWidth*scale;

                                   double oneRowHeight=(tableView.getRowHeight()+
                                                 tableView.getRowMargin())*scale;
                                   int numRowsOnAPage=
                                                 (int)((pageHeight-headerHeightOnPage)/oneRowHeight);
                                   double pageHeightForTable=oneRowHeight*numRowsOnAPage;
                                   int totalNumPages= (int)Math.ceil((
                                                 (double)tableView.getRowCount())/numRowsOnAPage);
                                   if(pageIndex>=totalNumPages) {
                                                 return NO_SUCH_PAGE;
                                   }

                                   g2.translate(pageFormat.getImageableX(),
                                                  pageFormat.getImageableY());
                           //bottom center
                                   g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35,
                                                 (int)(pageHeight+fontHeight-fontDesent));

                                   g2.translate(0f,headerHeightOnPage);
                                   g2.translate(0f,-pageIndex*pageHeightForTable);

                                   //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.
                                   else{
                                        g2.setClip(0, (int)(pageHeightForTable*pageIndex),
                                        (int) Math.ceil(tableWidthOnPage),
                                        (int) Math.ceil(pageHeightForTable));
                                   }

                                   g2.scale(

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: