【发布时间】:2012-09-19 00:06:06
【问题描述】:
我正在尝试将 JFrame 渲染到图像而不显示 JFrame 本身(类似于this 的问题)。我试过使用这段代码:
private static BufferedImage getScreenShot(Component component)
{
BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
// call the Component's paint method, using
// the Graphics object of the image.
component.paint(image.getGraphics());
return image;
}
但是,这仅在设置了JFrame 的setVisible(true) 时有效。这将导致图像显示在屏幕上,这不是我想要的。我也尝试过创建类似的东西:
public class MyFrame extends JFrame
{
private BufferedImage bi;
public MyFrame(String name, BufferedImage bi)
{
this.bi = bi;
super(name);
}
@Override
public void paint(Graphics g)
{
g.drawImage(this.bufferedImage, 0, 0, null);
}
}
然而,这会显示黑色图像(如上面的代码)。我很确定我所追求的是可能的,问题是我真的找不到方法。我在自定义 Swing 组件方面的经验非常有限,因此我们将不胜感激。
谢谢。
【问题讨论】:
-
@mKorbel:我发布的代码正是我想要做的。我曾尝试使用该类,但它也呈现黑色图像。
-
JComponents,然后 JFrames RootPane 可以返回其坐标或图形,对于已经可见的容器,在调用 pack() 之后,ContentPane 不是 Graphics(2d) 的正确容器,使用 JComponents / JPanel通过覆盖 get/setPreferredSize 或使用图像的调用实例(不确定有关其他方法的正确方法,图像,图形不是我的 Java 杯)
标签: java swing jframe bufferedimage