title: Is there a way to execute the code in paintComponent() more than once?
body: <p>I want to move the image around the screen in correlation to the mouse's x and y position. Every time the mouse is clicked, the image should move to that position.</p>
  
  <pre><code>public class testSquare {
    public static void main(String[] args) throws Exception {
      //...
      //object that stores x and y of mouse click
      testSquareInfo obj = new testSquareInfo(0, 0);
      //...
      //panel that draws image, seems to only execute once
      JPanel p = new JPanel ()
      {
          protected void paintComponent(Graphics a){ 
              int x = obj.getXPos();
              int y = obj.getYPos(); 
              a.drawImage(img, x, y, 50, 50, null);
          }        
        };
      //listens for mouse click and sends x and y to object
      p.addMouseListener(new MouseAdapter() {
      @Override 
      public void mousePressed(MouseEvent e) {
          int xMouse = e.getX();
          int yMouse = e.getY(); 
          obj.changeX(xMouse);
          obj.changeY(yMouse); 
  
          System.out.println(xMouse+" "+yMouse);
      }
      }); 
  
      window.add(p);
      window.setVisible(true); 
    }  
   } 
  
  //Second Class
  
  public class testSquareInfo 
  {
      private int x, y; 
      public testSquareInfo(int x, int y)
      {
         this.x = x;
         this.y = y;
      }
      public void changeX(int xNew)
      {
          x = xNew;
      }
      public void changeY(int yNew)
      {
          y = yNew;
      }
      public int getXPos()
      {
          return x;
      }
      public int getYPos()
      {
          return y;
      } 
  } 
  </code></pre>
  
  <p>Upon running the code, the image is drawn on the window at the 0, 0, coordinate, since the x and y are initialized to these values. Clicking around the screen does not move the image, but does properly update the x and y stored in the object. At one point during testing, the image did move to a different position on the screen. However, this happened when I was not clicking on the window and I have not been able to replicate it since. I am unsure as to when or how it moved. Thanks,</p>