0

How do i capture a portion of the GUI displayed by a Java program. My program generates certain graphs based on user input, which need to be captured. I need to provide a button on the GUI to save the graph as an image file.

user2318314
  • 113
  • 2
  • 12
  • This worked well for me http://stackoverflow.com/questions/4490454/how-to-take-a-screenshot-in-java – BAR Jul 21 '13 at 02:05

4 Answers4

3

Check out Screen Image to create an image of any component.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

You can use the BufferedImage class to capture it. Just use the paint method to write the content of the graph component to the Bufferedmage object. Then write the image to a file using ImageIO.write() method. Here's a link to its tutorial.

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

Rishabh
  • 199
  • 3
  • 14
0

If you are using a JFrame there is a createImage method you may want to look into. The method is also compatible with JPanel.

-1
Robot robot = new Robot();

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

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

Where guiObject is the component you want and getBounds() returns a Rectangle.

EDIT:

getBounds() must return the on screen coords. Or you do a little math to make it so.

BAR
  • 15,909
  • 27
  • 97
  • 185
  • -1, the answers that were given 3 hours earlier both show how to use the Robot class. No need to clutter the forum with duplicate suggestions. – camickr Jul 21 '13 at 02:23
  • You posted a link and called it done. I worked it out. – BAR Jul 21 '13 at 02:26