title: Java - Change BufferedImage File
body: <p>I have a Java class that extends JPanel and implements MouseListener, and trying to duplicate some basic functionality of JButton but with some loaded images to make things more pretty. Here's some slices of my code.</p>
  
  <p>A class field:</p>
  
  <pre><code>private BufferedImage image;
  </code></pre>
  
  <p>In the constructor I have:</p>
  
  <pre><code>try {
      image = ImageIO.read(new File("image/firstImage.png"));
  } catch (IOException ex) {
  }
  </code></pre>
  
  <p>I then override paintComponent to draw the image:</p>
  
  <pre><code>public void paintComponent(Graphics g) {
      g.drawImage(image, 0, 0, null);
  }
  </code></pre>
  
  <p>And that works great! Hooray. But I want to also be able to change the image that is being drawn to the screen. This doesn't work:</p>
  
  <pre><code>public void mousePressed(MouseEvent arg0) {
          try {
              image = ImageIO.read(new File("image/newImage.png"));
          } catch (IOException ex) {
          }
  }
  </code></pre>
  
  <p>mousePressed() definitely fires successfully (tried a System.out.println() debug statement) so what do I have to do to successfully change the image being drawn?</p>