I've got an issue with saving a BufferedImage using a simple paint program. When I save the image from the paint such as a picture of a snake that I drew earlier, it saves the image just fine, but it doesn't save it in the way you might think. Instead of saving the image to a C:\ drive (or whatever drive a user may be using) it saves the image to the eclipse workspace. This of course is unacceptable, as this needs to be given directly to a user's main place of access is. Here is the method that is used for saving the bufferedimage.
static void saveImage() {
try {
ImageIO.write(background, "png",
new File(fileName + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
So here, background is obviously the image being saved, png is the extension, and fileName is a string that is saved earlier using a prior method that isn't important here. This method saves the image to the eclipse workspace. This is bad. We need this to save to the default drive. How do I accomplish this? Let me know if you need anything else to aid you with your answer.
EDIT: So, as requested, here is the code that changes the fileName. It is in a different class with a differnt UI completely, and because I'm not sure of how much to post, I'll post the actionListener and the getName()
method. What happens here is that there is a JTextField
that, once a JButton
is pressed, has the string extracted from it and uses it as the fileName
. Here's the code:
`finishButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ProgramUI.fileName = getName();
ProgramUI.fileHasName = true;
ProgramUI.saveImage();
frame.dispose();
}
});
}
public String getName() {
return nameField.getText();
}
`