Java小程序设置为BufferedImage [英] Java Applet to BufferedImage
本文介绍了Java小程序设置为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);
我想这个截图applet或得到它要绘制它的表面的BufferedImage。我试着子类小程序和铸造我的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小程序设置为BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文