JavaFX imageview real coordinates translateX and translateY


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 ?

    Group root = new Group();
    Scene scene = new Scene(root, 800, 600, Color.WHITE);

   // border.prefHeightProperty().bind(scene.heightProperty());
    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);

    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);
    DoubleProperty xValue = new SimpleDoubleProperty();
    xValue.addListener(new ChangeListener() {

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

        public void changed(ObservableValue ov, Object t, Object t1) {
            System.out.println((double) t1);
5/6/2014 12:19:55 PM

Accepted Answer

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

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.

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.

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>() {
    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...

Read the Javadocs for Node for more details.

5/6/2014 3:42:34 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow