Java Applet to BufferedImage [duplicate]

*爱你&永不变心* 提交于 2019-12-11 11:24:13

问题


I created a JFrame that loads an external applet for a game like so:

//Setup everything for the Stub.. Below adds the stub to the applet and creates it.
DownloadFile(new URL(World + "/" + Archive), "./gamepack.jar");
CAppletStub Stub = new CAppletStub(new URL(World), new URL(World), this.Parameters);
applet = (Applet) new URLClassLoader(new URL[] {new URL(World.toString() + "/" + Archive)}).loadClass("Rs2Applet").newInstance();

applet.setStub(Stub);
applet.init();
applet.start();
applet.setPreferredSize(new Dimension(Width, Height));
Frame.getContentPane().add(applet);

I'm trying to screenshot this applet or get it to draw its surface in a BufferedImage. I tried subclassing "Applet" and casting my URLClassLoader to that class but it can't cast which makes sense.

How can I capture everything the applet is rendering into an Image?


回答1:


This works for any Component, not only Applets:

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



回答2:


I use Screen Image for normal Swing Applications. Don't know if loading an applet into a JFrame causes any problems.



来源:https://stackoverflow.com/questions/16002629/java-applet-to-bufferedimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!