0

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.

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;
    } 
} 

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,

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Have you tried revalidating panel as shown in https://stackoverflow.com/questions/4392722/how-to-repaint-a-jpanel-after-have-drawn-on-it – Karol Dowbecki Jul 24 '19 at 22:12
  • 1
    Possible duplicates [Java: Move image towards mouse position](https://stackoverflow.com/questions/26784303/java-move-image-towards-mouse-position/26791886#26791886) and [Moving an image to the position of the cursor where is clicked on a JFrame ](https://stackoverflow.com/questions/43603615/moving-an-image-to-the-position-of-the-cursor-where-is-clicked-on-a-jframe/43604224#43604224) – MadProgrammer Jul 24 '19 at 22:35
  • Possibly helpful answers [make image point toward specific location in java](https://stackoverflow.com/questions/50986670/make-image-point-toward-specific-location-in-java/50987225#50987225); [Java 2d rotation in direction mouse point](https://stackoverflow.com/questions/11911316/java-2d-rotation-in-direction-mouse-point/11911470#11911470); [How to animate from one x,y coordinate to another? (Java/Processing)](https://stackoverflow.com/questions/23209060/how-to-animate-from-one-x-y-coordinate-to-another-java-processing/23210015#23210015) – MadProgrammer Jul 24 '19 at 22:36
  • [mouse motion listener only in one direction](https://stackoverflow.com/questions/20129281/mouse-motion-listener-only-in-one-direction/20131064#20131064); [Java basic 2d game animation stutter](https://stackoverflow.com/questions/35979697/java-basic-2d-game-animation-stutter/35979771#35979771) – MadProgrammer Jul 24 '19 at 22:36

2 Answers2

1

Add a p.repaint(); to your mousePressed method. This causes the Panel to be redrawn.

However doing only this will just add another Image to the graphics component.

If you do not want to maintain the image at the previous position,
add a super.paintComponent(a); to the beginning of your paintComponent method
to have the panel in its blank state.

JPanel p = new JPanel() {

    protected void paintComponent(Graphics a){ 

        super.paintComponent(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);
        p.repaint();
    }
}); 
second
  • 4,069
  • 2
  • 9
  • 24
-2

Changed this:

 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);
    }
    }); 

To This:

 p.addMouseListener(new MouseAdapter() {
    @Override 
    public void mousePressed(MouseEvent e) {
        int xMouse = e.getX();
        int yMouse = e.getY(); 
        obj.changeX(xMouse);
        obj.changeY(yMouse); 
        p.repaint(); 
        System.out.println(xMouse+" "+yMouse);
    }
    }); 
  • if there is already an answer that covers your solution you shouldn't add another one saying the same. So you`re still missing the super.paintComponent ;) (Its likekly that this question will be closed as duplicate anyway). – second Jul 24 '19 at 22:56