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

JavaFX Translation Transformation(剪切转换)

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

倾斜对象形状的变换称为剪切变换。有两种剪切变换X-ShearY-Shear。一个移动X坐标值,另一个移动Y坐标值。但是,在这两种情况下,只有一个坐标改变其坐标,另一个坐标保留其值。剪毛也被称为歪斜

以下是在JavaFX中演示剪切的程序。在这里,我们在同一位置创建2个多边形(节点),具有相同的尺寸,但具有不同的颜色(蓝色和透明)。我们还在透明多边形上应用剪切。

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

public class ShearingExample extends Application {
@Override
public void start(Stage stage) {
Polygon hexagon1 = new Polygon();

//Adding coordinates to the hexagon
hexagon1.getPoints().addAll(new Double[]{
200.0, 50.0,
400.0, 50.0,
450.0, 150.0,
400.0, 250.0,
200.0, 250.0,
150.0, 150.0,
});
//Setting the fill color for the hexagon
hexagon1.setFill(Color.BLUE);

hexagon1.setStroke(Color.BLACK);
Polygon hexagon2 = new Polygon();

//Adding coordinates to the hexagon
hexagon2.getPoints().addAll(new Double[]{
200.0, 50.0,
400.0, 50.0,
450.0, 150.0,
400.0, 250.0,
200.0, 250.0,
150.0, 150.0,
});
//Setting the fill color for the hexagon
hexagon2.setFill(Color.TRANSPARENT);
hexagon2.setStroke(Color.BLACK);

//Creating shear transformation
Shear shear = new Shear();

//Setting the pivot points
shear.setPivotX(200);
shear.setPivotY(250);

//Setting the dimensions for the shear
shear.setX(0.5);
shear.setY(0.0);

//Adding the transformation to the polygon
hexagon2.getTransforms().addAll(shear);

//Creating a Group object
Group root = new Group(hexagon1, hexagon2);

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

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

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