Im using this;
Code:
public void takeScreenshot(boolean flag)
{
BufferedImage bufferedimage;
try
{
Robot robot = new Robot();
Point point = getLocationOnScreen();
Rectangle rectangle = new Rectangle(point.x, point.y, getWidth(), getHeight());
bufferedimage = robot.createScreenCapture(rectangle);
}
catch(Throwable throwable)
{
JOptionPane.showMessageDialog(frame, "An error occured while trying to create a screenshot!", "Screenshot Error", 0);
return;
}
String s = null;
try
{
s = getNearestScreenshotFilename();
}
catch(IOException ioexception)
{
if(flag)
{
JOptionPane.showMessageDialog(frame, "A screenshot directory does not exist, and could not be created!", "No Screenshot Directory", 0);
return;
}
}
if(s == null && flag)
{
JOptionPane.showMessageDialog(frame, "There are too many screenshots in the screenshot directory!\n"+"Delete some screen\n" +"shots and try again." , "Screenshot Directory Full", 0);
return;
}
if(!flag)
{
final JFileChooser fileChooser = new JFileChooser();
final JDialog fileDialog = createFileChooserDialog(fileChooser, "Save Screenshot", this);
final BufferedImage si = bufferedimage;
JFileChooser _tmp = fileChooser;
fileChooser.setFileSelectionMode(0);
fileChooser.addChoosableFileFilter(new imageFileFilter());
fileChooser.setCurrentDirectory(new File("C:/.GabbesClient/Scre/"));
fileChooser.setSelectedFile(new File(s));
JFileChooser _tmp1 = fileChooser;
fileChooser.setDialogType(1);
fileChooser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
String s1 = actionevent.getActionCommand();
if(s1.equals("ApproveSelection"))
{
File file = fileChooser.getSelectedFile();
if(file != null && file.isFile())
{
int i = JOptionPane.showConfirmDialog(frame, (new StringBuilder()).append(file.getAbsolutePath()).append(" already exists.\n"+"Do you want to replace it?").toString(), "Save Screenshot", 2);
if(i != 0)
{
return;
}
}
try
{
ImageIO.write(si, "png", file);
//client.pushMessage("Screenshot taken.", 3, "@cr2@ Client");
//JOptionPane.showMessageDialog(frame,"Screenshot Taken");
}
catch(IOException ioexception2)
{
JOptionPane.showMessageDialog(frame, "An error occured while trying to save the screenshot!\n"+"Please make sure you have\n"+" write access to the screenshot directory." , "Screenshot Error", 0);
}
fileDialog.dispose();
} else
if(s1.equals("CancelSelection"))
{
fileDialog.dispose();
}
}
{
}
});
fileDialog.setVisible(true);
} else
{
try
{
ImageIO.write(bufferedimage, "png", new File((new StringBuilder()).append("C:/.GabbesClient/Scre/").append(s).toString()));
JOptionPane.showMessageDialog(frame, "Image has been saved to C:/.GabbesClient/Scre. You can view your images by pushing File>View Images in the menu dropdowns." , "Screenshot manager", JOptionPane.INFORMATION_MESSAGE);
//client.pushMessage("Screenshot taken.", 3, "@cr2@ Client");
//JOptionPane.showMessageDialog(frame,"Screenshot taken.");
}
catch(IOException ioexception1)
{
JOptionPane.showMessageDialog(frame, "An error occured while trying to save the screenshot!\n"+"Please make sure you have\n"+" write access to the screenshot directory." , "Screenshot Error", 0);
}
}
}
And;
Code:
public static String getNearestScreenshotFilename()
throws IOException
{
File file = new File("C:/.ImBaws/Screenshots/");
int i = 0;
do
{
String s = " .png";
if(i < 10)
{
s = s.replaceFirst(" ", (new StringBuilder()).append("000").append(i).toString());
} else
if(i < 100)
{
s = s.replaceFirst(" ", (new StringBuilder()).append("00").append(i).toString());
} else
if(i < 1000)
{
s = s.replaceFirst(" ", (new StringBuilder()).append("0").append(i).toString());
} else
if(i < 10000)
{
s = s.replaceFirst(" ", (new StringBuilder()).append(" ").append(i).toString());
}
if((new File(file, s)).isFile())
{
i++;
} else
{
return s;
}
} while(i < 10000);
return null;
}
class imageFileFilter extends FileFilter
{
imageFileFilter()
{
}
public boolean accept(File file)
{
String s = file.getName();
return file.isDirectory() || s.toLowerCase().endsWith(".png") && s.indexOf("$") == -1;
}
public String getDescription()
{
return "PNG (*.png)";
}
}
There you go.[/QUOTE]