title: Adding Images on Mouse Click to JPanel
body: <p>I am working with Swing for the first time and am having a bit of an issue. What I have is a JFrame split into four JPanels. There is a MouseListener on the JFrame that acts like this</p>
  
  <p>On a click, if the click is inside of the left hand bar, determine which of the 13 icons is being selected. If the click is inside the right hand "game pane" &amp; an icon has been selected, place it at the location clicked.</p>
  
  <p>This is done here</p>
  
  <pre><code>@Override
  public void mouseClicked(MouseEvent event) {
      // TODO Auto-generated method stub
      xPos = event.getX();
      yPos = event.getY()-25;
      //If click is inside tool bar
      if(xPos&lt;=75){
          if(yPos&gt;-1 &amp;&amp; yPos&lt;48)
              //First tool image
                image = new ImageIcon(getClass().getResource(_image path_)).getImage();
          else if(yPos&gt;=48 &amp;&amp; yPos&lt;96)
              //Second tool image
                image = new ImageIcon(getClass().getResource(_image path_)).getImage();
          else if(yPos&gt;=96 &amp;&amp; yPos&lt;144)
              //Third tool image
                image = new ImageIcon(getClass().getResource(_image path_)).getImage();
          else if(yPos&gt;=144 &amp;&amp; yPos&lt;192)
              //Fourth tool image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=192 &amp;&amp; yPos&lt;240)
              //Fifth tool image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=240 &amp;&amp; yPos&lt;288)
              //Sixth tool image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=288 &amp;&amp; yPos&lt;336)
              //Seventh tool image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=336 &amp;&amp; yPos&lt;384)
              //First NPC image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=384 &amp;&amp; yPos&lt;432)
              //second NPC image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=432 &amp;&amp; yPos&lt;480)
              //Third NPC image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=480 &amp;&amp; yPos&lt;528)
              //First Decoration image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=528 &amp;&amp; yPos&lt;576)
              //Second Decoration image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
          else if(yPos&gt;=576 &amp;&amp; yPos&lt;=625)
              //Third Decoration image
                image = new ImageIcon(getClass().getResource(imagepath)).getImage();
      }
      //If click is within Game Pane
      else if (xPos&gt;75 &amp;&amp; yPos&lt;625){
          //A tool has been selected
          if(image!=null){
              placedTool = this.image;
              this.image = null;
              placeable = true;
          }
      }
      //An image and location on the game pane has been selected
      if(placeable &amp;&amp; this.image==null){
          ImageInfo newImg = new ImageInfo(xPos, yPos, image);
          gamePane.additions.add(newImg);
          gamePane.repaint();
          System.out.println("IMAGE PLACED @ " + xPos + ", " + yPos);
          placeable = false;
      }
  
      System.out.println("CLICK: (" + xPos + "," + yPos +")");
  }
  </code></pre>
  
  <p>Where imagepath is the path of a 50x50 icon. This portion works correctly with no errors. However gamePane doesn't get repainted properly.</p>
  
  <p><strong>gamePane simply has a background image for now. As components are added, they're supposed to be painted on top. All that gets painted is the background image though. Is there any way to specify the Z component using Graphics.drawImage(); Here's what I have for the paintComponent function of gamePane</strong> (bolded because this is the main issue)</p>
  
  <pre><code>@Override
  protected void paintComponent(Graphics g) {
      g.drawImage(backgroundImg, 0, 0, null);
      for(ImageInfo add : additions){
          g.drawImage(add.getImage(), add.getX(), add.getY(), null);
      }
  }
  </code></pre>
  
  <p>Where additions is defined like this</p>
  
  <p>List additions = new ArrayList();</p>
  
  <p>And the ImageInfo class just contains an image, an x coordinate, and a y coordinate</p>
  
  <pre><code>public class ImageInfo {
  private int x;
  private int y;
  private Image image;
  
  public int getX() {
      return x;
  }
  
  public int getY() {
      return y;
  }
  
  public Image getImage() {
      return image;
  }
  
  public ImageInfo(int x, int y, Image image) {
      super();
      this.x = x;
      this.y = y;
      this.image = image;
  }
  
  }
  </code></pre>
  
  <p>FIXED:</p>
  
  <p>Thank you mKorbel. By defining all the images outside of the mouseClicked method</p>
  
  <pre><code>Image tool1 = new ImageIcon(getClass().getResource(toolBar.TOOL1)).getImage();
  Image tool2 = new ImageIcon(getClass().getResource(toolBar.TOOL2)).getImage();
  Image tool3 = new ImageIcon(getClass().getResource(toolBar.TOOL3)).getImage();
  Image tool4 = new ImageIcon(getClass().getResource(toolBar.TOOL4)).getImage();
  Image tool5 = new ImageIcon(getClass().getResource(toolBar.TOOL5)).getImage();
  Image tool6 = new ImageIcon(getClass().getResource(toolBar.TOOL6)).getImage();
  Image tool7 = new ImageIcon(getClass().getResource(toolBar.TOOL7)).getImage();
  Image npc1 = new ImageIcon(getClass().getResource(toolBar.NPC1)).getImage();
  Image npc2 = new ImageIcon(getClass().getResource(toolBar.NPC2)).getImage();
  Image npc3 = new ImageIcon(getClass().getResource(toolBar.NPC3)).getImage();
  Image decor1 = new ImageIcon(getClass().getResource(toolBar.DECOR1)).getImage();
  Image decor2 = new ImageIcon(getClass().getResource(toolBar.DECOR2)).getImage();
  Image decor3 = new ImageIcon(getClass().getResource(toolBar.DECOR3)).getImage();
  </code></pre>
  
  <p>and the executing the mouseClicked function like</p>
  
  <pre><code>@Override
  public void mouseClicked(MouseEvent event) {
      // TODO Auto-generated method stub
      xPos = event.getX();
      yPos = event.getY()-25;
      //If click is inside tool bar
      if(xPos&lt;=75){
          if(yPos&gt;-1 &amp;&amp; yPos&lt;48)
              //First tool image
                image = tool1;
          else if(yPos&gt;=48 &amp;&amp; yPos&lt;96)
              //Second tool image
                image = tool2;
          else if(yPos&gt;=96 &amp;&amp; yPos&lt;144)
              //Third tool image
                image = tool3;
          else if(yPos&gt;=144 &amp;&amp; yPos&lt;192)
              //Fourth tool image
                image = tool4;
          else if(yPos&gt;=192 &amp;&amp; yPos&lt;240)
              //Fifth tool image
                image = tool5;
          else if(yPos&gt;=240 &amp;&amp; yPos&lt;288)
              //Sixth tool image
                image = tool6;
          else if(yPos&gt;=288 &amp;&amp; yPos&lt;336)
              //Seventh tool image
                image = tool7;
          else if(yPos&gt;=336 &amp;&amp; yPos&lt;384)
              //First NPC image
                image = npc1;
          else if(yPos&gt;=384 &amp;&amp; yPos&lt;432)
              //second NPC image
                image = npc2;
          else if(yPos&gt;=432 &amp;&amp; yPos&lt;480)
              //Third NPC image
                image = npc3;
          else if(yPos&gt;=480 &amp;&amp; yPos&lt;528)
              //First Yard Decoration image
                image = decor1;
          else if(yPos&gt;=528 &amp;&amp; yPos&lt;576)
              //Second Yard Decoration image
                image = decor2;
          else if(yPos&gt;=576 &amp;&amp; yPos&lt;=625)
              //Third Yard Decoration image
                image = decor3;
      }
      //If click is within Game Pane
      else if (xPos&gt;75 &amp;&amp; yPos&lt;625){
          //A tool has been selected
          if(image!=null){
              placedTool = this.image;
              this.image = null;
              placeable = true;
          }
      }
  
      if(placeable &amp;&amp; this.image==null){
          GamePiece newImg = new GamePiece(placedTool, xPos, yPos);
          gamePane.additions.add(newImg);
          gamePane.repaint();
          System.out.println("IMAGE PLACED @ " + xPos + ", " + yPos);
          placeable = false;
      }
  
      System.out.println("CLICK: (" + xPos + "," + yPos +")");
  }
  </code></pre>
  
  <p>The images were added over top of the background image, by the previously given paintComponent method. They are a little off position, but still visible.</p>