【问题标题】:Java Applet to BufferedImage [duplicate]Java Applet 到 BufferedImage [重复]
【发布时间】:2013-04-06 19:50:16
【问题描述】:

我创建了一个 JFrame,它为这样的游戏加载外部小程序:

//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 转换为该类,但它无法转换,这是有道理的。

如何将小程序渲染到图像中的所有内容捕获?

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java image applet awt bufferedimage


【解决方案1】:

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

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

【讨论】:

  • 您应该在完成绘图后处理Graphics 对象:g.dispose()
  • 与 Applet 一起使用时会创建一个黑色图像:S
  • @gcvt:不,Graphics.dispose() 的 Javadoc 说“作为参数提供给组件的绘制和更新方法的图形对象在这些方法返回时由系统自动释放”。
  • @CantChooseUsernames:我刚刚用一个 Applet 和一个由 appletviewer 运行的 JApplet 对其进行了测试:它可以工作,再次检查或发布您的代码。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多