Java screenshots: How to take a screen capture with Java and Swing

I was just digging around through an old Java/Swing application and found this method that takes a snapshot of the current screen, and returns that screen shot as an BufferedImage, so I thought I would share it here. I left a comment in the code where I used to create an ImageIcon from the BufferedImage, but in the end the code is a little more flexible when I return the BufferedImage:

/*
 * Take a snapshot of the screen using the Java Robot class.
 * Return the screen shot as an Image (BufferedImage).
 *
 */
public BufferedImage getBackgroundImage()
{
  try {
    Robot rbt = new Robot();
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension dim = tk.getScreenSize();
    BufferedImage background = rbt.createScreenCapture(new Rectangle(0, yOffset - 1, (int) dim.getWidth(), (int) dim.getHeight()));
    return background;
    //return new ImageIcon(background);
  }
  catch (Exception ex) {
    ex.printStackTrace();
  }
}

For what it's worth, I actually take this screenshot when my application isn't visible. I would hide my main JFrame, take this picture of the screen, and then re-display my JFrame. That doesn't really matter -- the Robot class doesn't care much what's displayed on your screen -- but I thought I'd mention it.

One other note: Looking at that method, it would be even better if I return the screen shot as an Image instead of a BufferedImage, but I pretty much just use BufferedImage's, so I'll leave it at that for now.