title: How to change image in JPanel without using a new JFrame (Repaint() doesn't work! )
body: <p>I've been working on a drinking game program for school.</p>
  <p>//this is the game //http://sotallytober.com/games/verbal/mexican/</p>
  <p>Anyway, I painted a image in an JPanel using the following code (it's an class that extends JPanel)</p>
  <pre><code>public class iconPanel extends JPanel {
  
  ImageIcon image;
  Image pic;
  public iconPanel(String startScreenImage) {
        image = new ImageIcon(startScreenImage);
        pic = image.getImage();
        this.setBackground(new Color(0, true));
  }
  
  @Override
  
  public void paintComponent(Graphics g) {
      //Paint background first
      g.drawImage (pic, 0, 0, getWidth (), getHeight (), this);
  }
  </code></pre>
  <p>Now in my other class, where I have the layout and all the components I declare on top my JPanels like this :</p>
  <pre><code>private JPanel pnDrinkPlayerBW;
  </code></pre>
  <p>Then in a method in the same class named MakeComponents I set the JPanel to :</p>
  <pre><code>pnDrinkPlayerBW = new iconPanel(&quot;img/glass.jpg&quot;);
  pnDrinkPlayerBW.setPreferredSize(new Dimension(183,61));
  </code></pre>
  <p>Afterwards I add it to the Panel where it has to come, and that panel onto the frame in the method makeLayout() (I don't think that it's useful code, so if you want to see it, ask me)</p>
  <p>Then if a button gets pressed, I want to change the glass.jpg image to another image, for example beerGlass0.png, so in the actionlistener in another method actionEvents() I do this:</p>
  <pre><code>pnDrinkPlayerBW = new iconPanel(&quot;img/beerGlass.png&quot;);
  pnDrinkPlayerBW.setPreferredSize(new Dimension(183,61));
  pnDrinkPlayerBW.repaint();
  </code></pre>
  <p>I'll put the constructor of this class also here, just if people need it :</p>
  <pre><code>public SpelScreen(){
      makeComponents();
      makeLayout();
      actionEvents();
  } // note : this is'nt the full constructor, just the call for the methods I talked          about, SpelScreen extends JFrame..
  </code></pre>
  <p>So what I want to do, is to set in the class SpelScreen a new image for the iconPanel and repaint it using the same instance of the spelscreen.</p>
  <p>I am quite new to Java, so don't expect me to rapidly understand complicated code :)</p>
  <p>Thanks!</p>