将Java组件呈现到图像文件

时间:2011-07-18 16:41:03

标签: java image swing user-interface awt

  

可能重复:
  Java Swing : Obtain Image of JFrame

如何保存Java GUI组件显示的图像文件?

E.g。将JPanel渲染为PNG。

2 个答案:

答案 0 :(得分:5)

JPanel panel = ...
...
...
File yourFileHere = ...
...
...
BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
panel.paint(g);
g.dispose();

try{
    ImageIO.write(img, "png", yourFileHere);
}catch(IOException e){
    e.printStackTrace();
}

答案 1 :(得分:1)

将由@Andrew Thompson描述并提供优秀示例here,但您可以通过阅读2D Graphics以及here

的示例来了解更多内容