0

So I'm attempting to create a simple application in which I can click a button and allow the user to display an image of their choice using a jFIleChooser from the menu and display that image into a label. I wrote code that works for that, but I'm supposed to create and use a method called "BufferedImage getImage()" using a class "ImageManipulator" to actually run that code, so that I can call on that method rather than running all the code in the main class. Here is what I have: (My ImageManipulator class)

public class ImageManipulator extends MainWindow {
BufferedImage image;

public ImageManipulator() {
    image = null;
}

public ImageManipulator(BufferedImage image){
    this.image = image;
}

public BufferedImage getImage() {
    FileChooser.showOpenDialog(null);
    BufferedImage image = null;
       try{
           File myFile = FileChooser.getSelectedFile();

           image = ImageIO.read(myFile);

           labelImage.setIcon(new ImageIcon(image));
       }
       catch(IOException e){

    }
    return image;

}

and my main window for the GUI (the code that actually goes under buttonClicked):

private void buttonChooseActionPerformed(java.awt.event.ActionEvent evt) {                                             


        ImageManipulator myManipulator = new ImageManipulator(image);
        myManipulator.getImage();


}   

So it presents an error because the image variable within myManipulator has not been declared or initialized. I realize that I need to initialize the image variable above in my main window and "connect" it to the image variable that is being returned via getImage() in some way, I'm just not sure about how to do so or what to set image equal to so that it actually runs the code in the getImage() method. I'm new to using classes and OO programming so hopefully someone can give me some direction here.

hnoel13
  • 58
  • 12
  • You should remove the `BufferedImage image` in your `getImage()` method and use `this.image = ImageIO.read(myFile);`, `this.image` refers to the class instance of `image` rather than a local instance of `image`. You should also return `this.image`. On a side note, your method names should reflect what they do, a name like `getImageFromFile()` better reflects what the method is actually doing. – Jonny Henly Sep 27 '17 at 19:13
  • @JonnyHenly thank you for the help with my method and the tip! I've changed the method the way you indicated, but an issue I'm having is that when I declare and initialize `ImageManipulator myManipulator = new ImageManipulator(image);` the image variable that I insert is showing a "cannot find symbol" error. I realize that I need to declare and initialize an image variable within the main window, I just don't know what to set image equal to. – hnoel13 Sep 27 '17 at 19:27
  • Remove your second constructor; there is no benefit to trying to pass an existing image to a class the supplies a brand new image. Then make use of the value *returned* by `myManipulator.getImage()`. – VGR Sep 27 '17 at 21:28
  • @VGR Thank you! This and a little tweaking of my code got it working the way that I wanted. I was under the impression that an argument had to be passed to it, which wasn't the case. – hnoel13 Sep 27 '17 at 23:01

1 Answers1

0

In order to get the method to work properly I deleted the second constructor

public ImageManipulator(BufferedImage image){ this.image = image; }

and used the first constructor with some small changes made

public ImageManipulator() { this.image = image; }

when creating ImageManipulator myManipulator = new ImageManipulator();because there's actually no need to pass myManipulator an argument in this case.

hnoel13
  • 58
  • 12