Nicolas Mommaerts
Nicolas Mommaerts

Reputation: 3263

how to get the coordinates of a transformed shape in javafx2?

I have 2 ellipses on a pane, one has a rotation transformation applied to it (the rotation point not being the ellipse itself obviously :)), the other doesn't. Now I need to draw a line from the center of the transformed ellipse to the center of the untransformed ellipse. So I need the coordinates of the transformed ellipse, is there a way to retrieve those? (I need them for other calculations besides the line drawing too)

Upvotes: 1

Views: 1513

Answers (2)

Sergey Grinev
Sergey Grinev

Reputation: 34498

Use localToParent method. Example:

    @Override
    public void start(Stage stage) {
        stage.setTitle(VersionInfo.getRuntimeVersion());
        Group root = new Group();

        // ellypsis with center in 100,100
        Arc ellypsis = ArcBuilder.create().centerX(100).centerY(100).length(360).radiusX(100).radiusY(50).fill(Color.TRANSPARENT).stroke(Color.RED).build();
        // rotate
        ellypsis.getTransforms().add(new Rotate(50, 50, 45));

        // find out where is 100,100 in rotated ellypsis
        Point2D localToParent = ellypsis.localToParent(100,100);

        // draw line from that point
        Line line = new Line(localToParent.getX(), localToParent.getY(), 200, 200);
        root.getChildren().addAll(ellypsis, line);
        stage.setScene(new Scene(root, 300, 250));
        stage.show();
    }

Upvotes: 4

GETah
GETah

Reputation: 21409

Given the lack of code provided in the question, I will give you a mathematical answer :)

Say you have ellipse1 which center is X1 and elipse2 which center is X2. The transformation gives you the following

elipse1 --> f(elipse1) = elipse1'

if you want to know the transformed ellipse ellipse1' center coordinates (X1') just do the following:

X1 --> f(X1) = X1` 

So, in a nutshell, just apply the same transformation to your orginal point X1 and you will get the transformed coordinates X1'. Now all what you have to do is draw a line from X1' to X2

Upvotes: 0

Related Questions