1

okay so I am trying to get the coordinates on a grid of which square a mouse was clicked on (why i casted into int) and this is giving me like the current position of the mouse,however, I want the position after a click and for nothing to happen while it hovers.

what do i need to do?

while (gameOver==false){
    mouseX= (int) StdDraw.mouseX();
    mouseY=(int) StdDraw.mouseY();
    game.update(mouseX, mouseY);
}

now i have

    public void mouseReleased(MouseEvent e){
       int mouseX = e.getX();
       int mouseY = e.getY();
               synchronized (mouseLock) {
    mousePressed = false;
}
}
public void run(){
    print();
    boolean gameOver=false;
    int mouseX,mouseY;
    StdDraw.setCanvasSize(500, 500);
    StdDraw.setXscale(0, game.gettheWidth());
    StdDraw.setYscale(0, game.gettheHeight());
    game.update(-1,-1);
    while (gameOver==false){

            mouseReleased(????)
            game.update(mouseX, mouseY);
        }       

}

still not working

None of this is making sense still,

Could someone give me an example of it getting x and y coordinates then printing them? I want the mouseX and mouseY to the be coordinates of the mouseclick. I've looked online I don't understand any of the other questions, I assume it has something to do with mouseevent?

user3075559
  • 23
  • 1
  • 6

1 Answers1

0

StdDraw implements a MouseListener. Overload the mouseReleased method to set your mouseX and mouseY variables.

To overload, your rewrite the method the way you need it to run:

int mouseX = 0;
int mouseY = 0;
public static void main(String[] args) {
    //do stuff
    //...
    while (gameOver == false) {
        //because mouseX and mouseY only change when the mouse button is released
        //they will remain the same until the user clicks and releases the mouse button
        game.update(mouseX, mouseY);
    }
}

//mouseReleased happens whenever the user lets go of the mouse button
@Override
public void mouseReleased(MouseEvent e) {
    //when the user lets go of the button, send the mouse coordinates to the variables.
    mouseX = e.getX();
    mouseY = e.getY();
    synchronized (mouseLock) {
        mousePressed = false;
    }
}

So for example, mouseX and mouseY are both 0. I click the mouse at 5, 6, drag it, and release the mouse at 120, 50. mouseReleased is called and changes mouseX to 120, and mouseY to 50. Meanwhile, game.update(0,0) has been happening. That now changes into game.update(120, 50), and will remain that way until I release the mouse button again.

To print the mouse coordinates:

@Override
public void mouseReleased(MouseEvent e) {
    //when the user lets go of the button, send the mouse coordinates to the variables.
    System.out.println("mouse x coord = " + e.getX());
    System.out.println("mouse y coord = " + e.getY());
    synchronized (mouseLock) {
        mousePressed = false;
    }
}
Aarowaim
  • 801
  • 4
  • 10
  • what does "overload the mousereleased method" mean? – user3075559 May 05 '14 at 02:23
  • Your write a new version of it. I've edited my answer to include an example. – Aarowaim May 05 '14 at 02:27
  • I can't see it, assume your still typing it? thanks a ton appreciate it – user3075559 May 05 '14 at 02:28
  • while (gameOver==false){ @Override public void mouseReleased(MouseEvent e){ mouseX = e.getX(); mouseY = e.getY(); } game.update(mouseX, mouseY); } – user3075559 May 05 '14 at 02:31
  • No, you don't need to put the method in your game loop; the `mouseReleased` method is called whenever the user lets go of the mouse button. Your game loop would be exactly the same as in your question, minus the lines to set `mouseX` and `mouseY` – Aarowaim May 05 '14 at 02:33
  • edited my answer still trying to understand your helping – user3075559 May 05 '14 at 02:33
  • okay so now how do i acess the x and y coordinates i will edit again with what I am trying to do. Sorry ughh so confusing! not your answer. – user3075559 May 05 '14 at 02:35
  • check out what i have now maybe you can give me further direction – user3075559 May 05 '14 at 02:37
  • okay so none of this is making any sense where is the mouseclick taking place can you add comments or something. and how do i access e.getY and e.getY in the main? – user3075559 May 05 '14 at 02:44
  • all i want is an example of it getting x and y coordinates and printing them could you do that? – user3075559 May 05 '14 at 02:47