Save a Screen Capture of a Java Frame

Submitted by:David Villa

Date added:19 January, 2013

Category:Java

Essentially the trick is to draw the panel/frame/etc.

Tags: screen capture

Code Snippet:

BufferedImage image = ...
System.out.println("image " + image);
File f = ...
String fileType = f.getName().substring(f.getName().lastIndexOf('.') + 1);
try {
ImageIO.write(image,fileType,f);
} catch (IOException ioe) {
ioe.printStackTrace();
JOptionPane.showMessageDialog(null,ioe,"Error",JOptionPane.ERROR_MESSAGE);
}
 
 

Comments