Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

Printing slow as an Applet

Thread view: 
jianghong2010@gmail.com - 30 Jun 2005 15:41 GMT
Hi, I have a question. I have a simple Java program which creates a
JTable and print the content. The JTable is 300 rows and 6 columns. And
I am using JBuilder as IDE.

The problem is when I run my code as a stand alone program using java
version 1.5.0_02-b09 VM, the printing is fast and the printjob size is
about 9 MB.

However, after I add the same code to an applet, when I run the same
code as an applet, the print job is huge (59 MB) and it took several
minutes to spooling. And eventually it says failed to print.

The browser I am using is IE and it connects to the same 1.5.0_02-b09
VM I used for the stand alone program.

Please help me out.

Thanks.

Hong
jianghong2010@gmail.com - 30 Jun 2005 15:43 GMT
The following is my code. It's a JTable with a print function. When I
run it as a standalone program use main, it prints OK. But the applet
prints extremely slow.
Does anyone know what the possibilities are?

Thanks.

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;
import java.applet.Applet;

public class Report extends Applet 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",
                                "Description", "open price",
                                "latest price", "End Date",
"Quantity"};

       final Object[][] data = new Object[300][10];
       for (int i = 0; i < 300; i++) {
           String filename = "sent.gif";
           data[i][0] = new ImageIcon(getClass().
                                      getResource(filename));
           data[i][1] = new Integer(i).toString();
           data[i][2] = "6.99ABCDEFGASDGKJSDGKJSDLGKHJDFBINP_WEOTJU";
           data[i][3] = new Date().toString();
           data[i][4] = "asl;dkghs;odfgjhs[qwe
r[pwusdhljkfnhg;osdfghw[ioere";

           data[i][9] = new ImageIcon(getClass().
                                      getResource(filename));
           data[i][8] = new Integer(i).toString();
           data[i][7] = "6.99ABCDEFGASDGKJSDGKJSDLGKHJDFBINP_WEOTJU";
           data[i][6] = new Date().toString();
           data[i][5] = "asl;dkghs;odfgjhs[qwe
r[pwusdhljkfnhg;osdfghw[ioere";

       }

       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(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();
      }

   public void init() {
       try {
           javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
               public void run() {
                  new Report();
               }
           });

       } catch (Exception e) {
           e.printStackTrace();
       }
   }

}
jianghong2010@gmail.com - 30 Jun 2005 15:45 GMT
Hi, I just realized when I load the same Applet from my home, the
printing is very fast, just like a normal printing speed. But the
problem still exists when I load the applet using my office computer.

Both my home and my office computer are using Widows XP and they all
connect to JRE version 1.5.0_02.

If anyone knows what's going on please help me.

Hong
Andrew Thompson - 30 Jun 2005 15:59 GMT
> I am using JBuilder as IDE.

Is JBuilder signing the code for you?

You example is throwing big, fat, juicy
AccessControlExceptions here..

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

HJ - 30 Jun 2005 16:38 GMT
Hi, I have sign the applet.
http://personal.vsnl.com/sureshms/javasign1.html

Thanks for reply.

Hong
Andrew Thompson - 30 Jun 2005 16:48 GMT
> Hi, I have sign the applet.
> http://personal.vsnl.com/sureshms/javasign1.html

OK.. change this line..

               } catch (Exception PrintException) {}

..to this..

               } catch (Exception e) {
                  e.printStackTrace();
               }

..recompile/sign/upload it and let us know.
(Of course, ..and look at it in your browser's Java console.)

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

HJ - 30 Jun 2005 20:12 GMT
Hi, I changed it, but there is no exception printed out from the Java
console...

Thanks.

Hong
Andrew Thompson - 30 Jun 2005 20:26 GMT
> Hi, I changed it, ..

The new files are uploaded?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

HJ - 30 Jun 2005 20:37 GMT
yeah.
Andrew Thompson - 30 Jun 2005 21:31 GMT
>>> Hi, I changed it, ..

>> The new files are uploaded?

> yeah.

..*Where*?  

Where is your (signed, compiled) applet?  
What URL can I visit to see this applet?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

HJ - 30 Jun 2005 21:39 GMT
Hi, thanks for your reponse. The wired thing is I can load the applet
and print the JTable from home computer very well. But it's not woring
for my office (computer & printer).
I don't know even I give you the URL it's going to be very helpful.

what is the possibilities there are can make this occur?

Thanks

Hong
Andrew Thompson - 30 Jun 2005 23:09 GMT
> I don't know even I give you the URL it's going to be very helpful.

It will help in so far as I might asssist you further.

Your problem, your call.

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

HJ - 30 Jun 2005 20:44 GMT
HJ - 30 Jun 2005 21:02 GMT
thanks you very much.
I changed the line, but there is nothing printed out from Java console.
I can't see any exception throw out.

thanks.

Hong


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.