【问题标题】:Paint java GUI component to image file将 java GUI 组件绘制到图像文件
【发布时间】:2010-06-09 11:15:37
【问题描述】:

假设我有

JButton test = new JButton("Test Button");

我想将按钮绘制到图像对象中并将其保存到文件中。

我试过了:

BufferedImage b = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
test.paint(b.createGraphics());

File output = new File("C:\\screenie.png");

try
{
    ImageIO.write(b, "png", output);
}
catch (IOException e)
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

此代码生成了一个空的 500x500 PNG 文件。有谁知道如何将 GUI 组件绘制到图像文件中?

【问题讨论】:

  • 请提供一个实际编译的小型独立示例。在您的 sn-p 中,使用了“offScreen”但未声明。

标签: java paint image-file


【解决方案1】:

图像不是空的,它在 0,0 处包含一个大小为 0x0 的按钮。

解决方案:您必须手动添加布局或设置按钮的大小。

注意:要测试它,首先在 JFrame 上渲染组件。这使您可以快速了解会发生什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多