How to inject Java applet's getGraphics() method?
First what I want to say is that this is my favorite help site for Java. (and other languages)
Now my question is, I want to take a screenshot of a Java applet. I have load the applet via the java.net.URLClassLoader and added it to my JFrame. I开发者_运维百科 can see it in my JFrame but I want to render it to a screenshot. The solution for NORMAL applets is mentioned here: Taking a "screenshot" of a java applet But this Applet doesn't use the paint nor update method, instead it uses the getGraphics() method. I have already injected the AppletStub so the applets thinks he is on his own website. But how can I inject the applet its getGraphics() method to render it to a screenshot?BTW, this is my code:
@Override
public void actionPerformed(final ActionEvent e) {
final String s = e.getActionCommand();
if (s.equals("Screenshot")) {
final BufferedImage offScreen = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
Loader.getApplet().update(offScreen.createGraphics());
try {
ImageIO.write(offScreen, "PNG", new File("C:/Users/Mitchell/Pictures/screenshot.png"));
} catch (Exception ex) { }
}
}
All it creates is a black 800x600 png image (the empty BufferedImage).
If you can get away with it, create a subclass of the Applet
in question, and override getGraphics()
such that it will return your Graphics
object, then use the subclass instead of the original.
精彩评论