Wednesday, June 29, 2016

87. Rotate

We create a red and green rectangle at same location and with same size. However the green one is rotated by 45 degrees.


The pivot point (50,75 in this example) is the center of rotation, and the point which not change position during rotation. This point corresponds to center of left side.


package ex87;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class Ex87 extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        Rectangle rect1 = new Rectangle(50, 50, 100, 50);
        rect1.setFill(Color.rgb(255, 0, 0, 0.5));

        Rectangle rect2 = new Rectangle(50, 50, 100, 50);
        rect2.setFill(Color.rgb(0, 255, 0, 0.5));
        
        Rotate rotate = new Rotate(45, 50, 75);
        rect2.getTransforms().add(rotate);
        
        Group root = new Group(rect1, rect2);
        Scene scene = new Scene(root,300,200);
        
        stage.setScene(scene);
        stage.setTitle("Example 87. Rotate");
        stage.show();
    }
}

This is the output:


No comments:

Post a Comment