Java Applet to BufferedImage



我创建了一个JFrame来加载一个游戏的外部applet,如下所示:

//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);

我正在尝试截图这个小程序或让它在buffereimage中绘制其表面。我尝试子类化"Applet",并将我的URLClassLoader转换为该类,但它不能转换,这是有意义的。

如何捕获applet渲染成图像的所有内容?

这适用于任何组件,而不仅仅是applet:

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;
}

对于普通的Swing应用程序,我使用Screen Image。不知道将applet加载到JFrame中是否会导致任何问题。

最新更新