title: Change Image in JFrame?
body: <p>I've been working through the Oracle tutorial for images this one here:</p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.awt.image.*;
  import java.io.*;
  import javax.imageio.*;
  import javax.swing.*;
  
  public class LoadImageApp extends Component {
  
      BufferedImage img;
  
      public void paint(Graphics g) {
          g.drawImage(img, 0, 0, null);
      }
  
      public LoadImageApp() {
         try {
             img = ImageIO.read(new File("strawberry.jpg"));
         } catch (IOException e) {
         }
  
      }
  
      public Dimension getPreferredSize() {
          if (img == null) {
               return new Dimension(100,100);
          } else {
             return new Dimension(img.getWidth(null), img.getHeight(null));
         }
      }
  
      public static void main(String[] args) {
  
          JFrame f = new JFrame("Load Image Sample");
  
          f.addWindowListener(new WindowAdapter(){
                  public void windowClosing(WindowEvent e) {
                      System.exit(0);
                  }
              });
  
          f.add(new LoadImageApp());
          f.pack();
          f.setVisible(true);
      }
  }
  </code></pre>
  
  <p>I want to change the image that appears in the JFrame. If I say:</p>
  
  <pre><code>f.add(new LoadImageApp());      
  f.revalidate();
  f.repaint();
  </code></pre>
  
  <p>The new image will appear, behind the current one. What I'd like to do is remove the previous image and replace, but I can't work out how I'd go about doing this with this snippet of code? </p>