JavaFX 鼠标位置

标签 java javafx mouse

我需要在我的应用程序中获取鼠标点击的 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();
    }

James_D给出的代码,有错误:

enter image description here

最佳答案

我不明白为什么要在关键帧的监听器内部设置鼠标监听器,但您需要从鼠标事件中获取坐标。

MouseEvent定义getX()getY()获取鼠标事件相对于节点本身的坐标,getSceneX() getSceneY() 获取鼠标事件相对于整个 Scene 的坐标,以及(在 Java 8 中)getScreenX()getScreenY( ) 获取鼠标事件相对于入口屏幕坐标系的坐标。

因此,如果您对鼠标相对于窗口(场景)的位置感兴趣,请执行

root.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        System.out.println(event.getSceneX());
        System.out.println(event.getSceneY());
    }
});

关于JavaFX 鼠标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27785917/

相关文章:

c++ - 在 Mac OS X 上模拟鼠标点击对某些应用程序不起作用

java - 我想在我的 jframe 计算器中进行多次计算,例如 2+2+2

java - 为什么 Platform.runLater 不检查它当前是否在 JavaFX 线程上?

.net - 检测水平鼠标滚轮移动

JavaFX - ReadOnlyProperty 不是只读的

java - SQLite驱动程序: Getting issue with getBinaryStream when Blob is null

C++ 简单 if 语句不检查?

java - ArrayList 内的 ArrayList 被覆盖

java - [Java]插入int时字符串数组中的空值

java - Struts 2 复选框 : What's the use of the value attribute when the fieldValue can only be true or false?