Manuel Jain
Manuel Jain

Reputation: 751

is it possible to set a javaFX Pane origin to bottom-left?

Is it possible so set the origin of a Pane to the bottom-left corner (instead of top-left)? I'm adding many shapes to my canvas which are defined within the "mathematical coordinate system".

I thought there is perhaps an easier way than always substract the height from the y-coordinate. Another advantage would be that I don't have to take care of resizing the pane.

Upvotes: 3

Views: 3242

Answers (1)

James_D
James_D

Reputation: 209225

If all you are doing is using shapes, you can apply a reflection to the pane. You can represent a reflection as a Scale with x=1 and y=-1. The only tricky part is that you must keep the pivot of the scale at the vertical center, which needs a binding in case the pane changes size.

If you're putting controls, or text, in the pane, then those will also be reflected, so they won't look correct. But this will work if all you are doing is using shapes.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

public class ReflectedPaneTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        Scale scale = new Scale();
        scale.setX(1);
        scale.setY(-1);

        scale.pivotYProperty().bind(Bindings.createDoubleBinding(() -> 
            pane.getBoundsInLocal().getMinY() + pane.getBoundsInLocal().getHeight() /2, 
            pane.boundsInLocalProperty()));
        pane.getTransforms().add(scale);

        pane.setOnMouseClicked(e -> 
            System.out.printf("Mouse clicked at [%.1f, %.1f]%n", e.getX(), e.getY()));

        primaryStage.setScene(new Scene(pane, 600, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Upvotes: 5

Related Questions