-1

Im implementing a screenshot function and currently it just saves to the project file but it would be nice if it saved to a specific file or location such as the desktop.

Currently:

try {
   imageId = random.nextInt(9999);
   ImageIO.write(MainGame.image, "png" , new File("Sinecure_" + imageId + ".png/"));
   System.out.println("Image Saved as Sinecure_" + imageId);
} catch (IOException e) {
   e.printStackTrace();
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Bevilacqua
  • 465
  • 3
  • 8
  • 19

1 Answers1

3

you can find the user's home directory using this code snippet:

String userHome = System.getProperty( "user.home" );

then you could (depending on the operating system) construct the file path like so:

String fullPath = userHome+File.seperator+"Desktop"+File.seperator+"Sinecure_" + imageId + ".png";
ImageIO.write(MainGame.image, "png" , new File(fullPath));

the above is for windows 7. you will need to adjust for other operating systems, of course. the easiest way to detect what OS youre running under would be another system property:

String OS = System.getProperty("os.name")
radai
  • 23,949
  • 10
  • 71
  • 115
  • @Jigar Joshi - thats because i forgot to add the "Desktop" part :-) thank you for making me notice it. – radai Jan 13 '13 at 05:01