JavaFX 鼠标位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27785917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX MousePosition
提问by Makaveli
I need to get x and y coordinates of a mouseclick in my application. I partially solved it in the code below by creating a point but I keep getting different coordinates depending on where I move a window of my application on the screen. I would need something constant to identify certain obejcts later. Thank you for your help!
我需要在我的应用程序中获取鼠标单击的 x 和 y 坐标。我通过创建一个点在下面的代码中部分解决了它,但是根据我在屏幕上移动应用程序窗口的位置,我不断获得不同的坐标。我需要一些常量来稍后识别某些对象。感谢您的帮助!
@Override
public void start(Stage stage) throws Exception {
final Pane root = new Pane();
setWidth(1400);
setHeight(1000);
Canvas background = new Canvas(getWidth(), getHeight());
final GraphicsContext context = background.getGraphicsContext2D();
File f = new File("background.png");
final Image image = new Image(new FileInputStream(f));
root.getChildren().add(background);
root.getChildren().add(b1);
b1.setLayoutX(1300);
b1.setLayoutY(10);
final Canvas animation = new Canvas(getWidth(), getHeight());
final Canvas animation2 = new Canvas(getWidth(), getHeight());
animation.setMouseTransparent(true);
animation2.setMouseTransparent(true);
final GraphicsContext context2 = animation.getGraphicsContext2D();
final GraphicsContext context3 = animation2.getGraphicsContext2D();
root.getChildren().add(animation);
root.getChildren().add(animation2);
Scene scene = new Scene(root, getWidth(), getHeight());
stage.setTitle("Old Gotham");
stage.setScene(scene);
stage.show();
final Duration oneFrameAmt = Duration.millis(1000 / 60);
final KeyFrame oneFrame;
oneFrame = new KeyFrame(oneFrameAmt,
new EventHandler() {
@Override
public void handle(Event event) {
context2.drawImage(image, 0, 0);
int offset = 700;
final Point p = MouseInfo.getPointerInfo().getLocation();
root.setOnMouseClicked(new EventHandler<Event>() {
@Override
public void handle(Event event) {
System.out.println(p.getX());
System.out.println(p.getY());
}
});
}
});
final Timeline tl = new Timeline(oneFrame);
tl.setCycleCount(Animation.INDEFINITE);
tl.play();
}
For the code presented by James_D, there is an error:
对于 James_D 给出的代码,有一个错误:
采纳答案by James_D
I don't understand why you are setting the mouse listener inside the listener for a key frame, but you need to get the coordinates from the mouse event.
我不明白为什么要在关键帧的侦听器内设置鼠标侦听器,但是您需要从鼠标事件中获取坐标。
MouseEvent
defines getX()
and getY()
to get the coordinates of the mouse event relative to the node itself, getSceneX()
and getSceneY()
to get the coordinates of the mouse event relative to the whole Scene
, and (in Java 8) getScreenX()
and getScreenY()
to get the coordinates of the mouse event relative to the entrie screen coordinate system.
MouseEvent
定义getX()
并getY()
获取鼠标事件相对于节点本身的坐标,getSceneX()
以及getSceneY()
获取鼠标事件相对于整体的坐标Scene
,以及(在 Java 8 中)getScreenX()
并getScreenY()
获取鼠标事件相对于入口屏幕的坐标坐标系。
So, if you're interested in the location of the mouse relative to the window (scene), do
因此,如果您对鼠标相对于窗口(场景)的位置感兴趣,请执行
root.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println(event.getSceneX());
System.out.println(event.getSceneY());
}
});
回答by Sidath Bhanuka Randeniya
This would provide more accurate coordinates of your mouse point..
这将提供更准确的鼠标点坐标..
root.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent event) {
System.out.println(event.getScreenX());
System.out.println(event.getScreenY());
}
});