The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Swing & AWT

Thread: Print a JPanel in headless mode

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 13 - Last Post: Jul 31, 2009 1:39 PM by: shikida Threads: [ Previous | Next ]
josandres

Posts: 3
Print a JPanel in headless mode
Posted: Apr 3, 2008 6:34 AM
  Click to reply to this thread Reply

Hi all!
I'm developing an application from printing plotcharts in headless mode, that is, every hour my app prints a plotchart with some information.
What i have is a JPanel (the plot chart) and when i invoke the printAll method, it checks the method isShowing() from Component class. this method checks that the Component is visible and its parent also is visible.
My question is if there is any way of printing a JPanel without being visible...and how can i solve this.

Thanks in advance.

cowwoc

Posts: 1,055
Re: Print a JPanel in headless mode
Posted: Apr 4, 2008 8:19 AM   in response to: josandres
  Click to reply to this thread Reply

I don't know what the situation is now but a few years ago I wanted to do the same and Swing was horribly broken on this.

Printing a Swing component should absolutely work independently of whether or not it is visible on the physical screen!

gipak

Posts: 13
Re: Print a JPanel in headless mode
Posted: Apr 4, 2008 9:12 AM   in response to: josandres
  Click to reply to this thread Reply

I would try it like that:

Create an Image, get the graphics from it and pass it to JPanel's "paint" or "paintComponent" method. Then close the graphic and print Image. Not sure if it will work in headless but worth a try.

cowwoc

Posts: 1,055
Re: Print a JPanel in headless mode
Posted: Apr 4, 2008 9:26 AM   in response to: gipak
  Click to reply to this thread Reply

The problem, at least in the past, was that invisible components skip the layout (as in layout manager) phase.

Looking at http://bugs.sun.com/view_bug.do?bug_id=4639354 it doesn't seem this was ever fixed.

tball

Posts: 34
Re: Print a JPanel in headless mode
Posted: Apr 4, 2008 9:45 AM   in response to: cowwoc
  Click to reply to this thread Reply

I don't have access to a headless server to verify this, but have you tried putting your graph in a JPanel subclass which overrides isShowing()?

public boolean isShowing() { return true; }

Since a JPanel doesn't have a native peer associated with it, most tests for headless operation should be safe to circumvent.

Artem Ananiev
Re: Print a JPanel in headless mode
Posted: Apr 4, 2008 9:45 AM   in response to: tball
  Click to reply to this thread Reply

You don't have to use a real headless server to run in a headless mode -
just specify -Djava.awt.headless=true when running your application.

Artem

swing@javadesktop.org wrote:
> I don't have access to a headless server to verify this, but have you tried putting your graph in a JPanel subclass which overrides isShowing()?
>
> public boolean isShowing() { return true; }
>
> Since a JPanel doesn't have a native peer associated with it, most tests for headless operation should be safe to circumvent.
> [Message sent by forum member 'tball' (tball)]
>
> http://forums.java.net/jive/thread.jspa?messageID=267763


alexfromsun

Posts: 404
Re: Print a JPanel in headless mode
Posted: Apr 4, 2008 9:59 AM   in response to: tball
  Click to reply to this thread Reply

Hello

By the way, you can switch on the headless mode on any machine:

java -Djava.awt.headless=true AppName

Thanks
alexp

profiler

Posts: 13
Re: Print a JPanel in headless mode
Posted: Apr 6, 2008 6:09 AM   in response to: josandres
  Click to reply to this thread Reply

Yes you can. We do it all the time, while we thought there was no solution like some others, the following code solved it:

public static BufferedImage createImage(JComponent component){
return createImage(component, BufferedImage.TYPE_INT_RGB);
}

public static BufferedImage createImage(JComponent component, int imageType){
Dimension componentSize = component.getPreferredSize();
component.setDoubleBuffered(false);
component.setSize(componentSize);
component.addNotify();
component.validate();

BufferedImage img = new BufferedImage(componentSize.width,
componentSize.height, imageType);
Graphics2D grap = img.createGraphics();
grap.setColor(Color.WHITE);
grap.fillRect(0,0,img.getWidth(),img.getHeight());
component.print(grap);
grap.dispose();
return img;
}

This works in headless mode with no painting artifacts. Note we are using component.print rather than component.paint - component.paint introduces "screen artifacts" e.g. grey rect issues and doesn't do a good job overall.

Hope this helps!
Joe

cowwoc

Posts: 1,055
Re: Print a JPanel in headless mode
Posted: Apr 6, 2008 7:25 AM   in response to: profiler
  Click to reply to this thread Reply

Sounds to me like whatever technique works should be wrapped up in a core API method. Users shouldn't have to guess what loops they have to jump through to get a component to print, and these hoops might change from one release to another anyway so we're better off abstracting it.

christiaan_se

Posts: 31
Re: Print a JPanel in headless mode
Posted: Apr 7, 2008 3:11 AM   in response to: profiler
  Click to reply to this thread Reply

Thanks for the tip regarding the "screen artifacts"! I thought it has something to do with doing the paint not on the EDT.

Btw, a similar discussion can be found http://today.java.net/pub/a/today/2006/04/20/bringing-swing-to-the-web.html
I agree with cowwoc that this should be part of the core Api.

Another btw, you may want to J2PrinterWorks, a nice package to print swing components which handles the described issue as well:
http://www.wildcrest.com/Software/J2PrinterWorks/J2PrinterWorksREADME.html

kind regards,
Christiaan

josandres

Posts: 3
Re: Print a JPanel in headless mode
Posted: Apr 7, 2008 11:55 PM   in response to: profiler
  Click to reply to this thread Reply

Thanks everyone for our answers...
In the end I found the solution based on profiler's reply.
I just needed to invoke addNotify method to paint my JPanel correctly!
Thank you very much!

profiler

Posts: 13
Re: Print a JPanel in headless mode
Posted: Apr 21, 2008 5:37 PM   in response to: josandres
  Click to reply to this thread Reply

I agree it is insane. The above code is the result of much hair pulling. It would be great to have it wrapped into:

BufferedImage image = SwingUtilities.saveImage(ofComponent);

cowwoc

Posts: 1,055
Re: Print a JPanel in headless mode
Posted: Jun 25, 2008 7:06 PM   in response to: profiler
  Click to reply to this thread Reply

I filed a RFE with review-id 1279729: "Ability to layout invisible components"

shikida

Posts: 20
Re: Print a JPanel in headless mode
Posted: Jul 31, 2009 1:39 PM   in response to: profiler
  Click to reply to this thread Reply

it helped me

a lot

thanks!

Kenji




 XML java.net RSS