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

JavaFX Transformation(转换)

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

转换意味着通过应用规则将某些图形转换为其他图形。我们可以进行各种类型的转换,例如平移,向上或向下缩放,旋转,剪切等。

使用JavaFX,您可以在节点上应用转换,例如旋转,缩放和转换。所有这些转换都由各种类表示,这些类属于包javafx.scene.transform

序号 转型与描述
1 Rotation(旋转)在旋转中,我们将物体从其原点以特定角度θ(θ)旋转。
2 Scaling(缩放)要更改对象的大小,请使用缩放转换。
3 Translation(平移)将对象移动到屏幕上的其他位置。
4 Shearing(剪切)倾斜对象形状的变换称为剪切变换。

多重转换

您还可以在JavaFX中的节点上应用多个转换。以下程序是同时对矩形执行旋转,缩放平移转换的示例。

将此代码保存在名称为的文件中 -

MultipleTransformationsExample.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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.scene.transform.Scale;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class MultipleTransformationsExample extends Application {
@Override
public void start(Stage stage) {
//Drawing a Rectangle
Rectangle rectangle = new Rectangle(50, 50, 100, 75);

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

//Setting the stroke color of the rectangle
rectangle.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);

//Creating the scale transformation
Scale scale = new Scale();

//Setting the dimensions for the transformation
scale.setX(1.5);
scale.setY(1.5);

//Setting the pivot point for the transformation
scale.setPivotX(300);
scale.setPivotY(135);

//Creating the translation transformation
Translate translate = new Translate();

//Setting the X,Y,Z coordinates to apply the translation
translate.setX(250);
translate.setY(0);
translate.setZ(0);

//Adding all the transformations to the rectangle
rectangle.getTransforms().addAll(rotate, scale, translate);

//Creating a Group object
Group root = new Group(rectangle);

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

//Setting title to the Stage
stage.setTitle("Multiple transformations");

//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 MultipleTransformationsExample.java 
java MultipleTransformationsExample

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

3D对象的转换

您还可以在3D对象上应用变换。以下是旋转和平移三维框的示例。

将此代码保存在名为RotationExample3D.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
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class RotationExample3D extends Application {
@Override
public void start(Stage stage) {
//Drawing a Box
Box box = new Box();

//Setting the properties of the Box
box.setWidth(150.0);
box.setHeight(150.0);
box.setDepth(150.0);

//Creating the translation transformation
Translate translate = new Translate();
translate.setX(400);
translate.setY(150);
translate.setZ(25);

Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
rxBox.setAngle(30);
ryBox.setAngle(50);
rzBox.setAngle(30);
box.getTransforms().addAll(translate,rxBox, ryBox, rzBox);

//Creating a Group object
Group root = new Group(box);

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

//Setting title to the Stage
stage.setTitle("Drawing a cylinder");

//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 RotationExample3D.java 
java RotationExample3D

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