I have a node which is a child of a "local" group which is a child of a "global" group. I want to translate the child in the coordinates of the global group but TranslateTransition
moves it in the local group coordinates.
In this example I have this parent hierarchy:
parent group
|- red circle
|- child group
|- blue circle
I want to get the blue circle on top of the red circle. If I translate it to the coordinates of the red circle it moves away from it because it's in its own group. If i translate the whole group it works.
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class AnimTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Circle c = new Circle(5, Color.BLUE);
Group group = new Group(c);
group.setTranslateX(40);
group.setTranslateY(50);
Circle target = new Circle(10, Color.RED);
target.setTranslateX(20);
target.setTranslateY(20);
Group parent = new Group(target, group);
TranslateTransition t1 = new TranslateTransition(Duration.seconds(1), c); // 'group' works
t1.setToX(target.getTranslateX());
t1.setToY(target.getTranslateY());
Button next = new Button("Play");
next.setOnAction(e -> t1.play());
Pane p = new Pane(parent);
p.setPrefSize(200, 200);
VBox root = new VBox(p, next);
stage.setScene(new Scene(root));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
How do I make the translation of the blue circle in the coordinates of the parent of the red circle?
Please understand that this is a simple example. In truth the hierarchy is bigger and looks something like
parent group
|- target node
|- group1
|- group2
|- group3
|- moving node
And also i can use the bounds of the target and not its translate properties to find the final destination but translate were enough for the example.