Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why printing is so slow

fsze88hohoNov 14 2008 — edited Mar 1 2009
HI,
I just try to create a java project. While I am trying to print simply words on printer I need to wait about 5 seconds for printing at first and second times afterward it can do the printing job normally (within 1 second).
Anybody can let me why and how to overcome this problem?
The codes as following, I hope somebody willing to help me.
package client;

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.print.*;
import java.awt.Graphics2D;

public class printTestFrame extends JFrame implements Printable {
private BorderLayout layoutMain = new BorderLayout();
private JPanel panelCenter = new KennamJPanel();
private JLabel statusBar = new JLabel();
private PrinterJob printerJob = PrinterJob.getPrinterJob();

public printTestFrame() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}

private void jbInit() throws Exception {
printerJob.setPrintable(this);
this.getContentPane().setLayout( layoutMain );
panelCenter.setLayout( null );
this.setSize(new Dimension(800, 600));
statusBar.setText( "" );
this.getContentPane().add( statusBar, BorderLayout.SOUTH );
this.getContentPane().add( panelCenter, BorderLayout.CENTER );
testPrint();
}
private void testPrint(){
boolean doPrint = printerJob.printDialog();
if (doPrint) {
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}

Graphics2D g2d = (Graphics2D)graphics;
g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

graphics.drawString("Hello world!", 100, 100);

return PAGE_EXISTS;
}
} 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 29 2009
Added on Nov 14 2008
1 comment
419 views