好吧所以我试图在一个网格上找到一个鼠标被点击的网格上的坐标(为什么我进入了int)这就像给我一样的鼠标的当前位置,但是,我希望之后的位置点击,徘徊时没有任何事情发生。
我需要做什么?
while (gameOver==false){
mouseX= (int) StdDraw.mouseX();
mouseY=(int) StdDraw.mouseY();
game.update(mouseX, mouseY);
}
现在我有
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);
}
}
仍然无法正常工作
这一切都没有意义,
有人能给我一个例子,它会得到x和y坐标然后打印出来吗? 我希望mouseX和mouseY成为鼠标点击的坐标。我已经在线查看了我不了解任何其他问题,我认为它与mouseevent有关吗?
答案 0 :(得分:0)
StdDraw实现了MouseListener。重载mouseReleased方法以设置mouseX和mouseY变量。
要重载,您需要按照运行方式重写方法:
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;
}
}
因此,例如,mouseX和mouseY均为0.我在5, 6
单击鼠标,拖动它,然后在120, 50
处释放鼠标。调用mouseReleased并将mouseX更改为120,将mouseY更改为50.同时,game.update(0,0)已经发生。现在变为game.update(120,50),并且将一直保持这种状态,直到我再次释放鼠标按钮。
要打印鼠标坐标:
@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;
}
}