views:

361

answers:

4

Hi!

I have a desktop application having heavyweight components (JxBrowser) in a JFrame. How can I make a snapshot from the GUI and save it to for example a png file?

Note: The method using Graphics2d and Component.paint()/paintAll()/print/printAll works only for lightweight components.

Any answers appreciated!

EDIT

I have already tried this:

robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

But I want the inner component to be captured...

EDIT

The situation seems to converge to this solution: if I have a bigger heavyweight component in my JFrame, so it is rendered on a JScrollPane then there exist no other method to get a snapshot of it programatically then to scroll it/screenshot it with screencapture?

+1  A: 

You mean programmatically?

What about

Point p = yourAwtComponent.getLocationOnScreen();
int w   = yourAwtComponent.getWidth();
int h   = yourAwtComponent.getHeight();

Rectangle rectangle = new Rectangle( p.x, p.y, w, h );

Image image = robot.createScreenCapture(rectangle);

And then something like this:

ImageIO.write( image, "png", file );
OscarRyz
@Pato: I've change the answer and placed your comments into your question.
OscarRyz
OK! That's what I like in SO! :)
Balint Pato
A: 

You can try also to create a screenshot by implementing this:

    int width = frameContainer.getWidth();
    int height = frameContainer.getHeight();

    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();

    frameContainer.paint(g2);

    return image;
Markus Lausberg
+1  A: 

Hi Balint, My name is Roman and I'm developer at TeamDev.

JxBrowser component it's a heavyweight component that embedds a native mozilla window to display web pages. To get screenshot of a full web page from JxBrowser component you can really use the Java Robot functionality with web page scrolling. For small web pages this solution is ok. But receiving screenshot of a large web page will take a lot of time. Also please note that Java Robot allows capturing only screen. So if some window will appears over the JxBroser component, then it will be captured too.

We have already started working on this functionality in terms of JxBrowser project. The solution that will allow capturing a full web page including its invisible part is based on Mozilla internal stuff. You will be able to capture a full web page and save it as image very easy.

We are going to add this functionality in one of the next release of JxBrowser library, but the release date is not defined yet. If you wish you can subscribe to TeamDev's RSS feed: http://support.teamdev.com/blogs/feeds/tags/company_news

Or just let me know and I will inform you when this functionality will be available. Please let me know if you have any further questions. I will be happy to help.

Regards, Roman

Roman
A: 

The JxBrowser can make the page screen shout them self since 1.3 version. Look at sample code http://support.teamdev.com/docs/DOC-1127