JavaFX MousePosition

时间:2015-01-05 18:51:08

标签: 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

2 个答案:

答案 0 :(得分:12)

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

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

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

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

答案 1 :(得分:2)

这将提供更准确的鼠标点坐标..

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