|
Replies:
13
-
Last Post:
Jul 31, 2009 1:39 PM
by: shikida
|
Threads:
[
Previous
|
Next
]
|
|
|
|
|
|
Print a JPanel in headless mode
Posted:
Apr 3, 2008 6:34 AM
|
|
|
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.
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 4, 2008 8:19 AM
in response to: josandres
|
|
|
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!
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 4, 2008 9:12 AM
in response to: josandres
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 4, 2008 9:26 AM
in response to: gipak
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 4, 2008 9:45 AM
in response to: cowwoc
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 4, 2008 9:45 AM
in response to: tball
|
|
|
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
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 4, 2008 9:59 AM
in response to: tball
|
|
|
Hello
By the way, you can switch on the headless mode on any machine:
java -Djava.awt.headless=true AppName
Thanks alexp
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 6, 2008 6:09 AM
in response to: josandres
|
|
|
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
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 6, 2008 7:25 AM
in response to: profiler
|
|
|
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.
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 7, 2008 11:55 PM
in response to: profiler
|
|
|
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!
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Apr 21, 2008 5:37 PM
in response to: josandres
|
|
|
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);
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Jun 25, 2008 7:06 PM
in response to: profiler
|
|
|
I filed a RFE with review-id 1279729: "Ability to layout invisible components"
|
|
|
|
|
|
|
|
Re: Print a JPanel in headless mode
Posted:
Jul 31, 2009 1:39 PM
in response to: profiler
|
|
|
it helped me
a lot
thanks!
Kenji
|
|
|
|
|