I have a fairly extensive GUI based entirely around javax.swing, with extensive use of tabs, as well as GridBagLayout. I have written up a little class (
based on this, the relavent parts of my implementation are below). I simply call print with the component I want printed. This is the PDF print I refer to (which happens to be what I need my program to print)
JavaPrint.pdf ( 409.75k )
Number of downloads: 12which doesn't seem too extensive, but when I printed (to a pdf) the print job was 86MB huge, and took 3.5 seconds to generate. Not to mention the time it took PDFCreator to actually receive the document, and then process it's enormous bulk which took at least 10 more seconds. I have not yet tried to use this with a real printer, god forbid a network printer. Even at the theoretical peak performance of a 802.11G network it would take a whopping 13 seconds simply to transfer the documents, and who knows how long it would take a cheap printer to process? So anyway, my question is, how do I make printing more efficient? I have disabled double buffering (you can see that in the code) and I really don't know what else to do. I have considered to resorting to using the Robot class to take a screenshot of the GUI and then print it, but that will more than likely have poor quality, but I'm sure it will be incredibly fast. If anyone knows any way to make it faster I would be very grateful. I just think something is wrong when a fairly small piece of GUI takes up 86MB of space. Thanks.
java
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
if(page > 0)
{ /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/*
* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d= (Graphics2D) g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
// scale to one full page
g2d.scale(pf.getImageableWidth() / componentToPrint.getWidth(), pf.getImageableHeight() / componentToPrint.getHeight());
/* Now print the window and its visible contents */
componentToPrint.printAll(g);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
/**
* Prints the JComponent it is passed using the PrinterJob that is already
* established
*
* @param ctp
* JComponent to be printed
*/
public void print(Component ctp)
{
RepaintManager.currentManager(ctp).setDoubleBufferingEnabled(false);
System.out.println("printing");
componentToPrint= ctp;
if(!ok)
getJob();
if(ok)
{
try
{
long t = System.currentTimeMillis();
job.print();
System.out.println(System.currentTimeMillis()-t);
} catch(PrinterException ex)
{
ex.printStackTrace();
}
}
RepaintManager.currentManager(ctp).setDoubleBufferingEnabled(true);
}
ps: why did nobody tell me I could say code=java to make it color-code, and number lines and other such great things? This should be made widely know!
This post has been edited by RoboAlex: 26 Jun, 2008 - 09:40 PM