捕获Java程序的布局

时间:2013-07-20 23:15:23

标签: java

如何捕获Java程序显示的GUI的一部分。我的程序根据用户输入生成某些图表,需要捕获这些图表。我需要在GUI上提供一个按钮,将图形保存为图像文件。

4 个答案:

答案 0 :(得分:3)

查看Screen Image以创建任何组件的图像。

答案 1 :(得分:2)

您可以使用BufferedImage类来捕获它。只需使用paint方法将图形组件的内容写入Bufferedmage对象即可。然后使用ImageIO.write()方法将图像写入文件。这是教程的链接。

http://sleepingthreads.blogspot.in/2013/07/capture-screenshotpart-of-java-gui.html

答案 2 :(得分:0)

如果您使用的是JFrame,则可能需要查看createImage方法。该方法也与JPanel兼容。

答案 3 :(得分:-1)

Robot robot = new Robot();

BufferedImage screenShot = robot.createScreenCapture( guiObject.getBounds() );

ImageIO.write(screenShot, "JPG", new File("myScreenShot.jpg"));

其中guiObject是你想要的组件,getBounds()返回一个Rectangle。

编辑:

getBounds()必须返回屏幕上的坐标。或者你做一些数学计算就可以了。