title: Drawing multiple images using paintComponent()
body: <p>I would like to display more than one image on the screen in the same <code>JPanel</code>.</p>
  
  <p>A for loop iterates over each element in the array and displays their corresponding image, but only seems to keep the last image.</p>
  
  <p>The code:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.io.*;
  import javax.imageio.ImageIO;
  
  public class GameGUI extends JFrame{
  JPanel mainPanel = new JPanel();
  JPanel buttonPanel = new JPanel();
  int arrayLength;
  
  public GameGUI() {
      super("Gameplay");
  
      //Set size of the frame.
      setSize(650, 580);
      //Location inside frame.
      setLocation(10, 8);
  
      SwingUtilities.isEventDispatchThread();
  </code></pre>
  
  <p>The methods that contain each individual panel:</p>
  
  <pre><code>    createMainPanel();
      createCentrePanel();
      createNorthPanel();
      createSouthPanel();
      createWestPanel();
      createEastPanel();
      setVisible(true);
  
  }
  //creating panels
  
  public void createMainPanel() {
      //here is the main panel which the others will be nested in.
      mainPanel.setLayout(new BorderLayout());
      mainPanel.setBackground(Color.red);
      add(mainPanel);
  }
  
  public boolean createCentrePanel() {
      JPanel CENTRE = new JPanel(new BorderLayout());
      CENTRE.setBackground(Color.WHITE);
      CENTRE.setBorder(BorderFactory.createLineBorder(Color.black));
      mainPanel.add(CENTRE, BorderLayout.CENTER);
  
      return true;
  }
  </code></pre>
  
  <p>This is the panel which i am using to print multiple images to the panel.
  As you can see I have a for loop which is going through each item in the array and passing the value to the draw component. However it only seems to keep the last image on the screen eventhough each item in the array is being passed to it.</p>
  
  <p>I have tried using <code>repaint</code> but it doesn't seem to work:</p>
  
  <pre><code>public boolean createNorthPanel() {
      int[] array = {1, 8, 9, 10};
      arrayLength = array.length;
      int size = 0;
      JPanel NORTH = new JPanel(new BorderLayout());
      NORTH.setPreferredSize(new Dimension(100, 100));
      NORTH.setBorder(BorderFactory.createLineBorder(Color.black));
      mainPanel.add(NORTH, BorderLayout.NORTH);
      for (int i = 0; i &lt; arrayLength; i++) {
          NORTH.add(new drawPanel(array[i], size, arrayLength));
          size = size + 30;
          //repaint();
      }
      return true;
  }
  
  public boolean createSouthPanel() {
      JPanel SOUTH = new JPanel(new BorderLayout());
      SOUTH.setPreferredSize(new Dimension(100, 100));
      // SOUTH.add(new drawPanel(2, 0));
      // SOUTH.add(new drawPanel(5, 30));
      SOUTH.setBorder(BorderFactory.createLineBorder(Color.black));
      mainPanel.add(SOUTH, BorderLayout.SOUTH);
      SOUTH.add(buttonPanel, BorderLayout.EAST);
  
      return true;
  }
  
  public boolean createWestPanel() {
      JPanel WEST = new JPanel(new BorderLayout());
      WEST.setPreferredSize(new Dimension(150, 100));
      //WEST.add(new drawPanel(8, 0));
      WEST.setBorder(BorderFactory.createLineBorder(Color.black));
      mainPanel.add(WEST, BorderLayout.WEST);
  
      return true;
  }
  
  public boolean createEastPanel() {
      JPanel EAST = new JPanel(new BorderLayout());
      EAST.setPreferredSize(new Dimension(150, 100));
      EAST.setBorder(BorderFactory.createLineBorder(Color.black));
      mainPanel.add(EAST, BorderLayout.EAST);
      //EAST.add(new drawPanel(2, 0));
      //EAST.add(new drawPanel(7, 60));
  
      return true;
  }
  
  public static void main(String args[]){
      new GameGUI();
  }
  }
  </code></pre>
  
  <p>Here is my class which draws the images on the screen</p>
  
  <pre><code>class drawPanel extends JPanel {
  
  Image image = null;
  int xPos;
  
  public drawPanel(int x, int y, int length) {
  
      xPos = y;
      try {
          File location = new File("src/Card_images/" + x + ".png");
          image = ImageIO.read(location);
  
      } catch (IOException e) {
          System.out.println("Error: " + e);
      }
  }
  
  /*public Dimension getPreferredSize() {
   return new Dimension(71, 96);
   }*/
  protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      //draws image to screen at positions displayed
      g.drawImage(image, xPos, 0, this);
  }
  }
  </code></pre>