Printing a Swing GUI

Why must it be 70MB of unholy large printing document?

Page 1 of 1

2 Replies - 3554 Views - Last Post: 27 June 2008 - 06:50 AM Rate Topic: -----

#1 RoboAlex  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 26-June 08

Printing a Swing GUI

Posted 26 June 2008 - 01:08 PM

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: 376
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.

	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 June 2008 - 10:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Printing a Swing GUI

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Printing a Swing GUI

Posted 27 June 2008 - 04:41 AM

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)
Was This Post Helpful? 0
  • +
  • -

#3 RoboAlex  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 39
  • Joined: 26-June 08

Re: Printing a Swing GUI

Posted 27 June 2008 - 06:50 AM

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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1