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

JavaFX Scaling Transformation(扩展转换)

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

要更改对象的大小,请使用缩放转换。在缩放过程中,您可以展开或压缩对象的尺寸。可以通过将对象的原始坐标与缩放因子相乘来实现缩放,以获得期望的结果。

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

将此代码保存在名为ScalingExample.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
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.transform.Scale;
import javafx.stage.Stage;

public class ScalingExample extends Application {
@Override
public void start(Stage stage) {
//Drawing Circle1
Circle circle1 = new Circle(300, 135, 50);

//Setting the color of the circle
circle1.setFill(Color.BLUE);

//Setting the stroke width of the circle
circle1.setStrokeWidth(20);

//Drawing Circle2
Circle circle2 = new Circle(300, 135, 50);

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

//Setting the stroke width of the circle
circle2.setStrokeWidth(20);

//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);

//Adding the scale transformation to circle1
circle1.getTransforms().addAll(scale);

//Creating a Group object
Group root = new Group(circle1, circle2);

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

//Setting title to the Stage
stage.setTitle("Scaling 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 ScalingExample.java 
java ScalingExample

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