Java Applet 到 BufferedImage [英] Java Applet to BufferedImage

查看:30
本文介绍了Java Applet 到 BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 JFrame,它为游戏加载外部小程序,如下所示:

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

我正在尝试截取此小程序的屏幕截图或让它在 BufferedImage 中绘制其表面.我尝试将Applet"子类化并将我的 URLClassLoader 转换为该类,但它无法转换,这是有道理的.

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?

推荐答案

这适用于任何组件,而不仅仅是小程序:

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

这篇关于Java Applet 到 BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆