siccome era un argomento mezzo interessante, ho fatto una ricerchina ed è venuto fuori questo thread su StackOverflow:
Thread @ StackOverflow.com
ho messo un po' mano al codice originale per trasformarlo in JApplet:
codice:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.*;
import javax.swing.border.TitledBorder;
/**
*
* @author Andrea
*/
public class ScreenshotJApplet extends JApplet {
static final String HELP =
"Type Ctrl-0 to get a screenshot of the current GUI.\n" +
"The screenshot will be saved to the current " +
"directory as 'screenshot.png'.";
public static BufferedImage getScreenShot(Component component) {
BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB);
// call the Component's paint method, using
// the Graphics object of the image.
component.paint(image.getGraphics());
return image;
}
private class MyActionListener implements ActionListener {
private ScreenshotJApplet sja;
public MyActionListener (ScreenshotJApplet sja) {
this.sja = sja;
}
public void actionPerformed (ActionEvent ae) {
BufferedImage img = sja.getScreenShot(sja.getContentPane());
JOptionPane.showMessageDialog(null,
new JLabel(new ImageIcon(img.getScaledInstance(img.getWidth(null)/2, img.getHeight(null)/2, Image.SCALE_SMOOTH)
)));
}
}
public void init() {
JMenuItem screenshot = new JMenuItem("Screenshot");
screenshot.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0, InputEvent.CTRL_DOWN_MASK));
screenshot.addActionListener(new MyActionListener(this));
JMenu menu = new JMenu("Other");
menu.add(screenshot);
JMenuBar mb = new JMenuBar();
mb.add(menu);
this.setJMenuBar(mb);
JPanel p = new JPanel(new BorderLayout(5,5));
p.setBorder( new TitledBorder("Main GUI"));
p.add(new JScrollPane(new JTree()), BorderLayout.WEST);
p.add( new JScrollPane( new JTextArea(HELP,10,30)), BorderLayout.CENTER);
this.setContentPane(p);
this.setVisible(true);
}
}
e come puoi vedere, mostra il JOptionPane con l'immagine, scalata. Ho eliminato tutta la gestione del salvataggio su file perché comporta la firma dell'applet etc etc, magari la finisci tu.