获取 JPanel 的图像

发布于 2024-12-19 10:38:32 字数 1190 浏览 0 评论 0原文

我在从屏幕外 JPanel 创建 BufferedImage 时遇到问题。具体来说,我正在尝试绘制 JPanel 的图像(其中包含一些 Java3D 元素)作为我正在开发的应用程序的背景图像。

我发现了几个线程描述如何通过将 JPanel 绘制到 BufferedImage 的图形上下文来获取 JPanel 的图像,但是当我绘制 BufferedImage 时,我得到的只是一个大的白色矩形。

这是我的代码:

SimpleWorld j3DPanel; // a custom JPanel that contains some simple Java3D elements

// CONSTRUCTOR

public GameBackgroundObject()
{
    super();

    // Here I set up a JPanel that contains some Java3D elements.

    j3DPanel = new SimpleWorld();
    j3DPanel.setSize(mainLevel.SCREENW, mainLevel.SCREENH);
    j3DPanel.setBounds(0,0,mainLevel.SCREENW, mainLevel.SCREENH);
    j3DPanel.doLayout();
    j3DPanel.validate();
} 


protected void draw(Graphics2D parentComponentGraphics, ...)
{
    super.draw(parentComponentGraphics, ...);

    int w = j3DPanel.getWidth();
    int h = j3DPanel.getHeight();

    BufferedImage j3DImg = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = j3DImg.createGraphics();
    j3DPanel.paint(g);

    parentComponentGraphics.drawImage(j3DImg, null, null);

}

另外,我尝试将 JPanel 添加到 JFrame 的内容中。它在那里工作并正确显示渲染的 Java3D 元素。然而,每当我尝试获取 JPanel 的 BufferedImage 并绘制该图像时,我都会得到这个大的白色矩形。

I'm having problems creating a BufferedImage from an offscreen JPanel. Specifically, I'm trying a draw the image of a JPanel (which contains some Java3D elements) as a background image for an application I'm working on.

I've found several threads describing how to do get a JPanel's Image by painting the JPanel to a BufferedImage's graphics context, but when I draw the BufferedImage, all I get is a big white rectangle.

Here's my code:

SimpleWorld j3DPanel; // a custom JPanel that contains some simple Java3D elements

// CONSTRUCTOR

public GameBackgroundObject()
{
    super();

    // Here I set up a JPanel that contains some Java3D elements.

    j3DPanel = new SimpleWorld();
    j3DPanel.setSize(mainLevel.SCREENW, mainLevel.SCREENH);
    j3DPanel.setBounds(0,0,mainLevel.SCREENW, mainLevel.SCREENH);
    j3DPanel.doLayout();
    j3DPanel.validate();
} 


protected void draw(Graphics2D parentComponentGraphics, ...)
{
    super.draw(parentComponentGraphics, ...);

    int w = j3DPanel.getWidth();
    int h = j3DPanel.getHeight();

    BufferedImage j3DImg = new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = j3DImg.createGraphics();
    j3DPanel.paint(g);

    parentComponentGraphics.drawImage(j3DImg, null, null);

}

Also, I've tried adding my JPanel to the contents of a JFrame. It works there and shows the rendered Java3D elements correctly. However, I just get this big white rectangle whenever I try to get the BufferedImage of the JPanel and draw that image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-12-26 10:38:32

绘制 Java 3D 场景可能会失败,因为它的 Canvas3D 组件是重量级的。使用轻量级面板“com.sun.j3d.exp.swing.JCanvas3D”可能会解决此问题。它的使用有点棘手。请参阅相应的 Java 3D 示例“JCanvas3DExample”或尝试此处的派生作品之一 http:// /www.interactivemesh.org/testspace/j3dmeetsswing.html#leightweight

Drawing a Java 3D scene probably fails because its Canvas3D component is heavyweight. Using the lightweight panel 'com.sun.j3d.exp.swing.JCanvas3D' might solve this problem. Its use is a bit tricky. See the corresponding Java 3D sample 'JCanvas3DExample' or try one of the derived works from here http://www.interactivemesh.org/testspace/j3dmeetsswing.html#leightweight

等往事风中吹 2024-12-26 10:38:32

尝试调用:

j3DPanel.paintComponent(g);

而不是:

j3DPanel.paint(g);

Try calling:

j3DPanel.paintComponent(g);

instead of:

j3DPanel.paint(g);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文