views:

87

answers:

4

Given an Applet object, is it possible to programatically obtain a "screen shot" of the applet window (represented as say a BufferedImage)?

 JApplet applet = this;
 // ... code here ...
 BufferedImage screenshotOfApplet = ...;
+1  A: 

You could use Robot.createScreenCapture(Rectangle bounds) - however, the applet would have to be signed to allow this to work once deployed.

After comments -

If you just want the applet component -

You can create a BufferedImage and paint to it - something like this:

public static BufferedImage imageFor(Component component) {
    BufferedImage image = new BufferedImage(component.getWidth(), 
            component.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    component.paint(g);
    return image;
}

I'm not sure if this would require the applet to be signed or not...

Nate
createScreenCapture(..) gives me the entire screen including the browser window, etc. I'd like the "applet window" only.
knorv
+2  A: 

At least if you're only using Swing components, I suppose it would be possible to create a BufferedImage of the same size as the applet and call the applet's paint method with the Graphics object you can get from BufferedImage#getGraphics(). I have no template code here to test if it actually works, but I guess it's worth a try.

jarnbjo
A: 

Screen Image. Must admit I've never tried it on a JApplet before, but it works fine on JFrames and JDialogs.

camickr
A: 

I think you want java.awt.image.PixelGrabber. (IIRC, that was behind the massive slowdown is Java graphics performance from 1.1 to 1.2, although I might be wrong.)

Tom Hawtin - tackline