Welcome to Dream.In.Code
Become a Java Expert!

Join 150,101 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,845 people online right now. Registration is fast and FREE... Join Now!




Printing a Swing GUI

 
Reply to this topicStart new topic

Printing a Swing GUI, Why must it be 70MB of unholy large printing document?

RoboAlex
26 Jun, 2008 - 12:08 PM
Post #1

New D.I.C Head
*

Joined: 26 Jun, 2008
Posts: 39



Thanked: 3 times
My Contributions
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)
Attached File  JavaPrint.pdf ( 409.75k ) Number of downloads: 12

which 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
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Printing A Swing GUI
27 Jun, 2008 - 03:41 AM
Post #2

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
It is so big, because it is sent to the printer as an uncompressed image, and it's dimensions are scaled up according to the DPI of the printing.
I am not really sure if you can do anything about it without losing quality.
(You could check if you can print from a regular application the same picture with the same quality with less traffic, and if you can, then it might be possible to do something in Java as well)
User is offlineProfile CardPM
+Quote Post

RoboAlex
RE: Printing A Swing GUI
27 Jun, 2008 - 05:50 AM
Post #3

New D.I.C Head
*

Joined: 26 Jun, 2008
Posts: 39



Thanked: 3 times
My Contributions
But is there any way to make it more efficient? When I print a web page, or a pdf, it's never nearly that large. Is there any way to compress it, or lower the resolution, and still have it print to a full page in decent quality?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:36AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month