[英]Getting coordinates of mouse click on grid easy java StdDraw
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. 好吧,所以我试图在单击了鼠标的正方形的网格上获取坐标(为什么我将其强制转换为int),这使我像鼠标的当前位置一样,但是,我想要单击后的位置并它徘徊时什么也不会发生。
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? 有人可以给我举个例子,先获取x和y坐标然后打印出来吗? I want the mouseX and mouseY to the be coordinates of the mouseclick.
我希望mouseX和mouseY成为mouseclick的坐标。 I've looked online I don't understand any of the other questions, I assume it has something to do with mouseevent?
我在网上看过,但我不明白其他任何问题,我认为这与mouseevent有关?
StdDraw implements a MouseListener. StdDraw实现一个MouseListener。 Overload the mouseReleased method to set your mouseX and mouseY variables.
重载mouseReleased方法以设置mouseX和mouseY变量。
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
. 因此,例如,mouseX和mouseY的都是0我点击鼠标在
5, 6
,拖动它,并释放鼠标在120, 50
。 mouseReleased is called and changes mouseX to 120, and mouseY to 50. Meanwhile, game.update(0,0) has been happening. mouseReleased会被调用,并将mouseX更改为120,将mouseY更改为50。同时,game.update(0,0)一直在发生。 That now changes into game.update(120, 50), and will remain that way until I release the mouse button again.
现在将其更改为game.update(120,50),并且将保持这种状态,直到我再次释放鼠标按钮。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.