I cant seem to save my buffered image as a jpeg in a certain folder, please help, here is some of the code.
public void actionPerformed(ActionEvent e) {
img = null;
JMenuItem source = (JMenuItem)(e.getSource());
//if(source.getText()== "New"){
// img = null;
// }
if (source.getText()== "Exit"){
System.exit(0);
}
if (source.getText()== "Open"){
JOptionPane.showMessageDialog(
null,"Please select an image.");
JFileChooser imgChooser =
new JFileChooser();
while (img == null) {
try {
if (imgChooser.showDialog(null, "Open") !=
JFileChooser.APPROVE_OPTION) {
System.exit(0);
}
img = javax.imageio.ImageIO.read(imgChooser.getSelectedFile());
if (img.getType() == BufferedImage.TYPE_CUSTOM) {
BufferedImage tmp =
new BufferedImage(
img.getWidth(), img.getHeight(),
BufferedImage.
TYPE_3BYTE_BGR);
ColorConvertOp op =
new ColorConvertOp(null);
op.filter(
img, tmp);
img = tmp;
}
}
catch (NullPointerException r) {
JOptionPane.showMessageDialog(
null, "That was not an image " +
"format I recognized! Please try again.");
}
catch (java.io.IOException r) {
JOptionPane.showMessageDialog(
null, "I was not able to read " +
"that image! Please try again.");
}
}
}
//************************************************************************************
if (source.getText().equalsIgnoreCase("Save")){
System.out.println("1");
JFileChooser chooser = new JFileChooser();
//FileFilter filter = new FileNameExtensionFilter("JPG Files", "JPG");
//chooser.addChoosableFileFilter(filter);
System.out.println("2");
int ret = chooser.showDialog(null, "Save Image");
System.out.println("3");
File file = chooser.getSelectedFile();
String location = file.getPath();
File saveFile = new File(""+img);
int slash = file.getPath().lastIndexOf("\");
String save = file.getPath().substring(slash+1);
chooser.setSelectedFile(saveFile);
int rval = chooser.showSaveDialog(source);
if (rval == JFileChooser.APPROVE_OPTION) {
saveFile = chooser.getSelectedFile()
public void actionPerformed(ActionEvent e) {
img = null;
JMenuItem source = (JMenuItem)(e.getSource());
//if(source.getText()== "New"){
// img = null;
// }
if (source.getText()== "Exit"){
System.exit(0);
}
if (source.getText()== "Open"){
JOptionPane.showMessageDialog(
null,"Please select an image.");
JFileChooser imgChooser =
new JFileChooser();
while (img == null) {
try {
if (imgChooser.showDialog(null, "Open") !=
JFileChooser.APPROVE_OPTION) {
System.exit(0);
}
img = javax.imageio.ImageIO.read(imgChooser.getSelectedFile());
if (img.getType() == BufferedImage.TYPE_CUSTOM) {
BufferedImage tmp =
new BufferedImage(
img.getWidth(), img.getHeight(),
BufferedImage.
TYPE_3BYTE_BGR);
ColorConvertOp op =
new ColorConvertOp(null);
op.filter(
img, tmp);
img = tmp;
}
}
catch (NullPointerException r) {
JOptionPane.showMessageDialog(
null, "That was not an image " +
"format I recognized! Please try again.");
}
catch (java.io.IOException r) {
JOptionPane.showMessageDialog(
null, "I was not able to read " +
"that image! Please try again.");
}
}
}
//************************************************************************************
if (source.getText().equalsIgnoreCase("Save")){
System.out.println("1");
JFileChooser chooser = new JFileChooser();
//FileFilter filter = new FileNameExtensionFilter("JPG Files", "JPG");
//chooser.addChoosableFileFilter(filter);
System.out.println("2");
int ret = chooser.showDialog(null, "Save Image");
System.out.println("3");
File file = chooser.getSelectedFile();
String location = file.getPath();
File saveFile = new File(""+img);
int slash = file.getPath().lastIndexOf("\");
String save = file.getPath().substring(slash+1);
chooser.setSelectedFile(saveFile);
int rval = chooser.showSaveDialog(source);
if (rval == JFileChooser.APPROVE_OPTION) {
saveFile = chooser.getSelectedFile()