Forum Stats

  • 3,873,375 Users
  • 2,266,539 Discussions
  • 7,911,516 Comments

Discussions

How to take a snap shot of a GUI in java

user8557977
user8557977 Member Posts: 10
edited Jun 26, 2013 3:22AM in Swing

Hi

   I know how to use Robot class to take a screen shot. But, I would like to take a snap shot of a java Swing GUI.  How can I do that....How can I know the locations of Java GUI in the screen.

Please let me know

Thanks

Prashanth

Tagged:

Answers

  • mKorbel
    mKorbel Member Posts: 306
    edited Jun 26, 2013 3:23AM

    there no needed any active Screens Capture

    any JComponents including Containers can be printed to Image, BufferedImage

            Dimension size = container.getSize();

            BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);

            Graphics2D g2 = image.createGraphics();

            panel.paint(g2);

            try {

                ImageIO.write(image, "jpg", new File("snapshot.jpg"));

            } catch (Exception e) {

                // exceptions handling

            }

This discussion has been closed.