pensavo che bastasse fare una cosa del genere:
codice:
public class FCCaptureAuto extends JFrame {
public static void salva() throws AWTException, IOException {
JFileChooser fc = new JFileChooser();
File file = null;
FileFilter jpegFilter = new FileNameExtensionFilter("File JPG/JPEG", "jpeg");
FileFilter gifFilter = new FileNameExtensionFilter("File GIF", "gif");
FileFilter pngFilter = new FileNameExtensionFilter("File PNG", "png");
fc.addChoosableFileFilter(jpegFilter);
fc.addChoosableFileFilter(gifFilter);
fc.addChoosableFileFilter(pngFilter);
int returnVal = fc.showSaveDialog(fc);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
FileFilter selectedFilter = fc.getFileFilter();
if (file.getName().indexOf('.') == -1) {
if (selectedFilter == jpegFilter) {
file = new File(file.getPath() + ".jpeg");
} else if (selectedFilter == gifFilter) {
file = new File(file.getPath() + ".gif");
} else if (selectedFilter == pngFilter) {
file = new File(file.getPath() + ".png");
}
}
if (file.exists()) {
String msg = MessageFormat.format("The entry ''{0}'' already exists.\nDo you want to replace it?", new Object[]{file});
int r = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION);
if (r == JOptionPane.NO_OPTION) {
return;
}
}
Robot robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage buff = robot.createScreenCapture(area);
ImageIO.write(buff, recupero formato, new File(file.toString()));
JOptionPane.showMessageDialog(null, "Immagine salvata" + file);
}
}
public static void main(String[] args) {
try {
salva();
} catch (AWTException ex) {
Logger.getLogger(FCCaptureAuto.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FCCaptureAuto.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
ma come recupero il formato/estensione scelta??