简体   繁体   中英

How to apply a transformation to a shape in JavaFX?

I want to apply a transformation to a shape and with method setRotate(Double value) I can rotate a shape. Now, how can I apply the transformation again? How can I get the coordinates of the points after the transformation?

Polygon p =  new Polygon(0,0,0,100,50,50); 
p.getPoints(); // output before : 0,0,0,100,50,50

p.setRotate(90);
p.setRotate(90); //not applied

p.getPoints(); // output after: 0,0,0,100,50,50

Triangle before transformation:

变换前的三角形

Triangle after transformation:

变形后的三角形

If you only want to do rotations, simply add the values up. rotate is the "absolute" rotation, not a relative one. If you want to concatenate different transformations, I recommend using Transform s and adding them to the transform s list of the node.

As for finding finding the coordinates: You're proably be interested in the coordinates in the parent node. You can get those from the local coordinates using Node.localToParent :

@Override
public void start(Stage stage) throws IOException {
    Polygon p =  new Polygon(0,0,0,100,50,50); 
    p.getPoints(); // output before : 0,0,0,100,50,50

    p.getPoints(); // output after: 0,0,0,100,50,50

    StackPane root = new StackPane(p);
    Scene scene = new Scene(root, 500.0, 400.0);

    scene.setOnMouseClicked(evt -> {
        // add rotation and determine resulting position of (0, 0)
        Rotate r = new Rotate(90, (0+0+50) / 3d, (0+100+50) / 3d);
        p.getTransforms().add(r);
        System.out.println("(0, 0) is now at " + p.localToParent(0, 0));
    });

    stage.setScene(scene);
    stage.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM