knorv
knorv

Reputation: 50117

Obtaining a screen shot of an Applet?

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 = ...;

Upvotes: 0

Views: 354

Answers (4)

Nate
Nate

Reputation: 16888

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

Upvotes: 1

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147124

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.)

Upvotes: -1

camickr
camickr

Reputation: 324088

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

Upvotes: 0

jarnbjo
jarnbjo

Reputation: 34313

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.

Upvotes: 2

Related Questions