JMenuItem screenshot = new JMenuItem("Screenshot (png)");
screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK));
screenshot.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
BufferedImage img = getScreenShot(frame.getMainPanel());
try {
// write the image as a PNG
ImageIO.write(img, "png", file);