JavaFX imageview 实坐标 translateX 和 translateY [英] JavaFX imageview real coordinates translateX and translateY

查看:19
本文介绍了JavaFX imageview 实坐标 translateX 和 translateY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 translateX 和 translateY 在我的场景中位置不好?我从坐标 x = 100 y = 200 开始,我的真实坐标 y = -24.8 ... 为什么?我的 ImageView 什么时候需要真正的坐标?

And why the translateX and translateY is bad position on my scene ? I start on coordinate x = 100 y = 200 in real my real coordinate y = -24.8 ... why ? I need real coordinates when is my ImageView is ?

    primaryStage.setTitle("Title");
    Group root = new Group();
    Scene scene = new Scene(root, 800, 600, Color.WHITE);

   // border.prefHeightProperty().bind(scene.heightProperty());
    //border.prefWidthProperty().bind(scene.widthProperty());
    Image im = new Image("Images/universe.jpg", 800, 600, true, true);
    ImageView iv = new ImageView(im);

    Image iv1 = new Image("Images/Asteroid.png", 60, 50, false, false);
    ImageView iv2 = new ImageView(iv1);
    iv2.setX(100);
    iv2.setY(200);

    Path p = new Path();
    p.getElements().add(new MoveTo(100, 200));
    p.getElements().add(new LineTo(200, 400));
    PathTransition pt = new PathTransition(Duration.millis(10000), p);
    pt.setNode(iv2);
    root.getChildren().add(iv2);   
    DoubleProperty xValue = new SimpleDoubleProperty();
    xValue.bind(iv2.translateXProperty());
    xValue.addListener(new ChangeListener() {

        @Override
        public void changed(ObservableValue ov, Object t, Object t1) {
         //   System.out.println((double) t1);
        }
    });
       DoubleProperty yValue = new SimpleDoubleProperty();
    yValue.bind(iv2.translateYProperty());
    yValue.addListener(new ChangeListener() {

        @Override
        public void changed(ObservableValue ov, Object t, Object t1) {
            System.out.println((double) t1);
        }
    });
    pt.play();
    primaryStage.setScene(scene);
    primaryStage.show();
}

推荐答案

不知道真实坐标"是什么意思:你要哪个坐标系(屏幕、场景、父级、节点...)?

I don't know what "real coordinate" means: which coordinate system are you wanting (screen, scene, parent, node...)?

p>

您显示的值是 translateY 属性;它是节点垂直平移的量.所以 -24.8 意味着它从原来的位置向上移动了 24.8 个单位.

The value you are displaying is the translateY property; it's the amount by which the node is translated vertically. So -24.8 means it's moved 24.8 units up from its original position.

您可以查看 boundsInParent 属性,它告诉您节点在父坐标系中的边界,包括任何变换(例如平移).boundsInLocal 属性是节点在其自身坐标系中的边界,不包括任何变换.

You can look at the boundsInParent property, which tells you the bounds of the node in the parent's coordinate system, including any transforms (such as the translation). The boundsInLocal property is the bounds of the node in its own coordinate system, and doesn't include any transforms.

如果你想在场景或屏幕坐标中获取节点的边界,你可以使用许多 localToScene(...)localToScreen(...) 转换方法.

If you want to get the bounds of the node in scene or screen coordinates, you can use one of the many localToScene(...) or localToScreen(...) methods to convert.

更新:例如,要在场景坐标中跟踪图像的边界,您可以执行以下操作:

Update: For example, to track the bounds of the image in scene coordinates, you can do something like:

ChangeListener<Number> listener = new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov, Number oldValue, Number newValue) {
        Bounds boundsInScene = iv2.localToScene(iv2.getBoundsInLocal());
        double xInScene = boundsInScene.getMinX();
        double yInScene = boundsInScene.getMinY();
        // do something with values...
    }
});
iv2.translateXProperty().addListener(listener);
iv2.translateYProperty().addListener(listener);

阅读 Javadocs for Node 了解更多详情.

Read the Javadocs for Node for more details.

这篇关于JavaFX imageview 实坐标 translateX 和 translateY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆