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

Printing still produce huge spool files and takes too long

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.3.0
    • client-libs
    • 2d
    • x86
    • windows_95



      Name: yyT116575 Date: 01/15/2001


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      This application (which is taken partially from your Report.java - Java
      Tutorial) prints a 2-pages Tabel with some titles. In page number one
      there should be a logo image, which I've substituted with a rectangle.
      It doesn't give me any errors, but the first page take 3 minutes 20
      seconds for spooling (note: without the logo image) and the second
      2 minutes 20 seconds. My printer is an HP Laserjet 1100 perfectly
      working, new, lastest model of laser printers, and my PC is a PENTIUM II
      233 Mhz 128 Mb RAM. A similar table printed, for example with
      MS Word 97 takes 20-25 seconds for both pages. I know these are
      incomparable, but 3-4 minutes per page it's TOO MUCH for any
      user!! The problem almost doesn't exist if I use an inkjet printer. I think the
      problem is related with PCL printer (?).


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

      public class PrintTab implements Printable
      {
        private JTable tabella;
        //private Image logo;
        private String anno,nomeCategoria,allenatore,telefono,telefono2;
        public PrintTab()
        {
          TabellaListaModel model=new TabellaListaModel();
          anno=new String ("Title Number One");
          nomeCategoria=new String("Main Title ABCDEFGHI");
          allenatore=new String("String on the right");
          telefono=new String("String number two");
          tabella=new JTable(model);
          tabella.setGridColor(Color.black);
          tabella.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
          tabella.setRowSelectionAllowed(false);tabella.setColumnSelectionAllowed(false);
          tabella.setBorder(new LineBorder(Color.black,1));
          tabella.getTableHeader().setBorder(new LineBorder(Color.black,1));
          if (model instanceof TabellaListaModel)
          {
            tabella.getColumnModel().getColumn(0).setPreferredWidth(20);
            tabella.getColumnModel().getColumn(1).setPreferredWidth(180);
            tabella.getColumnModel().getColumn(2).setPreferredWidth(160);
            tabella.getColumnModel().getColumn(3).setPreferredWidth(80);
            tabella.getColumnModel().getColumn(4).setPreferredWidth(260);
            tabella.getColumnModel().getColumn(5).setPreferredWidth(85);
            tabella.getColumnModel().getColumn(6).setPreferredWidth(100);
            tabella.getColumnModel().getColumn(7).setPreferredWidth(70);
          }
          /*
          Toolkit tk=Toolkit.getDefaultToolkit();
          logo=tk.getImage("progetto/icone/audax.gif");
          MediaTracker mt=new MediaTracker(this);
          mt.addImage(logo,1);
          try {
            mt.waitForAll();
          }
          catch (InterruptedException e) {
          }
          */

          tabella.setRowHeight(23);

          JFrame pippo=new JFrame();
          pippo.getContentPane().add(new JScrollPane(tabella));
          pippo.pack();
          pippo.setVisible(false);

          RepaintManager.currentManager(pippo).setDoubleBufferingEnabled(false);
        }

        public int print (Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException
        {
          tabella.setEditingRow(-1);
          Graphics2D g2=(Graphics2D)g;
          pageFormat.setOrientation(PageFormat.LANDSCAPE);

          Paper p=new Paper();
          p.setSize(594,880);
          p.setImageableArea(p.getImageableX(),p.getImageableY()-20,p.getImageableWidth(),p.getImageableHeight()+40);
          pageFormat.setPaper(p);
          g2.setColor(Color.black);
          Font fontNPagina=new Font ("Times New Roman",Font.PLAIN,10);
          g2.setFont(fontNPagina);

          int fontHeight=g2.getFontMetrics().getHeight();
          int fontDescent=g2.getFontMetrics().getDescent();

          int intestHeight;
          if (pageIndex==0) intestHeight=80;
          else intestHeight=0;
          double pageHeight=pageFormat.getImageableHeight()-fontHeight-intestHeight;
          double pageWidth=pageFormat.getImageableWidth();

          double tableWidth=(double) tabella.getColumnModel().getTotalColumnWidth();
          double scale=(pageWidth/tableWidth);

          double headerHeightOnPage=tabella.getTableHeader().getHeight()*scale;
          double tableWidthOnPage=tableWidth*scale;
          double oneRowHeight=(tabella.getCellRect(0,1,true).height)*scale;
          int numRowsOnAPage;
          numRowsOnAPage=(int)((pageHeight-headerHeightOnPage)/oneRowHeight);

          System.out.println ("Numer of lines in page n."+(pageIndex+1)+": "+numRowsOnAPage);
          double pageHeightForTable=oneRowHeight*numRowsOnAPage;
          int totalNumPages=(int)Math.ceil(((double)tabella.getRowCount())/numRowsOnAPage);
          if (pageIndex>=totalNumPages) return Printable.NO_SUCH_PAGE;

          g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
          // number of page:
          if (pageIndex!=0)
            g2.drawString(new String(nomeCategoria+" Pagina: "+(pageIndex+1)),(int)pageWidth/2-70,(int)(pageHeight+fontHeight+intestHeight-fontDescent));

          // if first page print the titles
          if (pageIndex==0)
          {
            Font fontGrande= new Font("Arial",Font.BOLD,25);
            Font fontPiccolo= new Font("Arial",Font.PLAIN,18);
            Font fontMedio=new Font ("Times New Roman",Font.PLAIN,20);
            g2.setClip(0,-20,(int)pageWidth,intestHeight+20);
            g2.setFont(fontPiccolo);
            g2.drawString(new String("Anno Sportivo:"+anno),110,g2.getFontMetrics().getHeight()+g2.getFontMetrics().getDescent()+0);
            g2.setFont(fontMedio);

            if (allenatore!=null)
            {
              g2.drawString(allenatore,(int)pageWidth-g2.getFontMetrics().stringWidth(allenatore)-10,intestHeight-40);
              g2.drawString(telefono,(int)pageWidth-g2.getFontMetrics().stringWidth(telefono)-10,intestHeight-20);
            }
            g2.setFont(fontGrande);
            if (nomeCategoria!=null)
              g2.drawString(nomeCategoria,110,g2.getFontMetrics().getHeight()+g2.getFontMetrics().getDescent()+25);

            int alt=70;int larg=70;
            // draw the image:
            //int larg=(int)logo.getWidth(null)*alt/logo.getHeight(null);
            //g2.drawImage(logo,0,-10,larg,alt,null);
            //i write a rectangle instead of the image:
            g2.drawRect(0,-10,larg,alt);
          }

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

          if (pageIndex+1==totalNumPages)
          {
            int lastRowPrinted=numRowsOnAPage*pageIndex;
            int numRowsLeft=tabella.getRowCount()-lastRowPrinted;
            g2.setClip(-3,(int)(pageHeightForTable*pageIndex),(int)Math.ceil(tableWidthOnPage)+3,(int)Math.ceil(oneRowHeight*numRowsLeft));
          }
          else
          {
            g2.setClip(-3,(int)(pageHeightForTable*pageIndex),(int)Math.ceil(tableWidthOnPage)+3,(int)Math.ceil(pageHeightForTable));
          }
          g2.scale(scale,scale);
          tabella.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);
          tabella.getTableHeader().paint(g2);
          g2.scale(1/scale,1/scale);
          return Printable.PAGE_EXISTS;
        }

        public static void main(String[] args)
        {
          try {
            UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          }
          catch (Exception e) {}
          PrintTab p=new PrintTab();
          PrinterJob pj=PrinterJob.getPrinterJob();
          pj.setPrintable(p);
          if (pj.printDialog()) {
            try {
              pj.print();
            } catch (Exception ex) {
              ex.printStackTrace();
            }
          }
        }
      }


      class TabellaListaModel extends AbstractTableModel
      {
        private static Object dati[][];
        final String nomiColonne[]= {"N?","Cognome e Nome","Nato a","Il","Indirizzo","Telefono","Cellulare", "N? Tessera"};

        public TabellaListaModel ()
        {
          super();

          dati=new Object[40][8];
          // fill the table with data:
          for (int i=0;i<40;i++)
          {
            dati[i][0]=new String(""+(i+1));
            dati[i][1]=new String ("BlaBlaBlaBla BlaBlaBlaBla");
            dati[i][2]=new String("Qwertyuiop asdf");
            dati[i][3]=new String ("23/07/1992");
            dati[i][4]=new String("Via Trento Trieste,32 12345 Campobasso");
            dati[i][5]=new String("0345 123234");
            dati[i][6]=new String("0339 23123423");
            dati[i][7]=new String("123232");
          }
        }
        public int getColumnCount() {
          return nomiColonne.length;
        }
        public int getRowCount() {
          return dati.length;
        }
        public String getColumnName(int col) {
          return nomiColonne[col];
        }
        public Object getValueAt(int row, int col) {
          return dati[row][col];
        }
        public boolean isCellEditable(int row, int col) {
          return false;
        }
      }
      (Review ID: 115045)
      ======================================================================

            prr Philip Race
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: