获取 JPanel 的图像
我在从屏幕外 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绘制 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
尝试调用:
而不是:
Try calling:
instead of: