Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use translate and when relocate - What is the difference between translate and layout coordinates?

When to use translate and when relocate in order to move a node? In the end of the day it seems they do the same thing (visually); move the node; the first by doing a translation on the origin (the x, y stays the same), the second by changing the x, y coords. So suppose i want to move a node in a specific point in the screen.. should i use node.relocate(x,y) or node.setTranslateX(x), node.setTranslateY(y)?

To demostrate what I mean I have made a sample program you can play with: A rectangle on the screen, whose position is determined by 4 sliders (2 of them controlling the layout x, y the other two controlling the translate x, y).

/* imports are missing */  
public class TransReloc extends Application{
    @Override
    public void start(Stage primaryStage) throws Exception {
        Group root = new Group();
        Rectangle rect = new Rectangle(100, 50, Color.BLUE);
        root.getChildren().add(rect);
        VBox controlGroup = new VBox();
        Slider relocX = new Slider(-100, 100, 0 );
        Slider relocY = new Slider(-100, 100, 0 );
        Slider transX = new Slider(-100, 100, 0 );
        Slider transY = new Slider(-100, 100, 0 );
        rect.layoutXProperty().bind(relocX.valueProperty());
        rect.layoutYProperty().bind(relocY.valueProperty());
        rect.translateXProperty().bind(transX.valueProperty());
        rect.translateYProperty().bind(transY.valueProperty());
        controlGroup.getChildren().addAll(relocX, relocY, transX, transY);
        root.getChildren().add(controlGroup);
        controlGroup.relocate(0, 300);
        Scene scene  = new Scene(root, 300, 400, Color.ALICEBLUE);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}
like image 502
C.L.S Avatar asked Mar 03 '15 15:03

C.L.S


2 Answers

layout coordinates are used by Layout Managers like StackPane or VBox to control their children location. Group (and Pane) leaves children layout to developer thus there is no difference from translate functionality.

So generally you should only change translate coordinates for fine location tuning and leave layout to layout managers (actually you can't change layoutX, layoutY for nodes inside non Group/Pane layout managers)

As an example try to run next code and resize window to see how StackPane recalculates layoutBounds

public void start(Stage primaryStage) throws Exception {
    StackPane root = new StackPane();
    Rectangle rect = new Rectangle(100, 50, Color.BLUE);
    root.getChildren().add(rect);
    Scene scene  = new Scene(root, 300, 300, Color.ALICEBLUE);

    rect.layoutXProperty().addListener( (e) -> {
        System.out.println(rect.getLayoutX() + ":" + rect.getLayoutY());
            });

    primaryStage.setScene(scene);
    primaryStage.show();

}
like image 66
Sergey Grinev Avatar answered Nov 16 '22 07:11

Sergey Grinev


Another difference is that when you call Node.getBoundsInLocal(), will calculate the LayoutX, LayoutY and all the applied Effects. But, Node.getBoundsInParent() will get calculated with LayoutX, LayoutY, all applied Effects plus all transformations (rotation, translation and scaling). So you can use LayoutX/Y properties as a main position and use translateX/Y as a second or an alternative way to move the node. And the other difference is discussed above, I mean not to copy from Sergey Grinev.

like image 28
Hasan Shadi Avatar answered Nov 16 '22 08:11

Hasan Shadi