1

In my program (using awt.Graphics), I'm rendering images on a JPanel, just a super basic game in Java. I have a feature to take a screen shot using this

public static void getScreenShot(String name) {
    file3 = new File("\\saves\\" + name);
    //first things first, take that screenshot!
    try {
        BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(image, "png", new File(file3 + ".png"));
    } catch(Exception e) {}
}

But this captures my entire monitor size, I want just whats inside the JFrame I'm using. Any suggestions?

Bryce Hahn
  • 1,585
  • 4
  • 16
  • 26
  • One thing you can do is setBounds on the JFrame, then use those bounds to call createScreenCapture – Sully May 05 '14 at 21:38
  • 2
    @HithamS.AlQadheeb: that's unnecessary. Simply create a BufferedImage, and draw the JPanel with a Graphics object obtained from the image. Edit: as well described in Andrew's excellent answer, one that I up-voted ages ago. – Hovercraft Full Of Eels May 05 '14 at 21:40

0 Answers0