多读书多实践,勤思考善领悟

JavaFX Rotation Transformation(旋转变换)

本文于1664天之前发表,文中内容可能已经过时。

在旋转中,我们将物体从其原点以特定角度θ(θ)旋转。从下图中,我们可以看到点P(X,Y)位于距水平X坐标的角度φ处,距离原点的距离为r

以下是演示JavaFX中的旋转变换的程序。在这里,我们在同一位置创建2个矩形节点,具有相同的尺寸但具有不同的颜色(Blurywood和Blue)。我们还在Blurywood颜色的矩形上应用旋转变换。

将此代码保存在名为RotationExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 RotationExample extends Application {
@Override
public void start(Stage stage) {
//Drawing Rectangle1
Rectangle rectangle1 = new Rectangle(150, 75, 200, 150);
rectangle1.setFill(Color.BLUE);
rectangle1.setStroke(Color.BLACK);

//Drawing Rectangle2
Rectangle rectangle2 = new Rectangle(150, 75, 200, 150);

//Setting the color of the rectangle
rectangle2.setFill(Color.BURLYWOOD);

//Setting the stroke color of the rectangle
rectangle2.setStroke(Color.BLACK);

//creating the rotation transformation
Rotate rotate = new Rotate();

//Setting the angle for the rotation
rotate.setAngle(20);

//Setting pivot points for the rotation
rotate.setPivotX(150);
rotate.setPivotY(225);

//Adding the transformation to rectangle2
rectangle2.getTransforms().addAll(rotate);

//Creating a Group object
Group root = new Group(rectangle1, rectangle2);

//Creating a scene object
Scene scene = new Scene(root, 600, 300);

//Setting title to the Stage
stage.setTitle("Rotation transformation example");

//Adding scene to the stage
stage.setScene(scene);

//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

使用以下命令从命令提示符编译并执行保存的java文件。

1
2
javac RotationExample.java 
java RotationExample

执行时,上面的程序生成一个javaFx窗口,如下所示。