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

JavaFX 3D Shape(形状)

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

在前面的章节中,我们已经了解了如何在XY平面上绘制2D形状。除了这些2D形状,我们还可以使用JavaFX绘制其他几个3D形状。

3D形状

通常,3D形状是可以在XYZ平面上绘制的几何图形。这些包括气缸,球体盒子

上面提到的每个3D形状都由一个类表示,所有这些类都属于包javafx.scene.shape。名为Shape3D的类是JavaFX中所有三维形状的基类。

创建3D形状

要创建三维形状,您需要 -

  • 实例化所需3D形状的相应类。
  • 设置3D形状的属性。
  • 将3D形状对象添加到组中。

实例化各个类

要创建三维形状,首先需要实例化其各自的类。例如,如果要创建3D框,则需要实例化名为Box的类,如下所示 -

1
Box box = new Box();

设置形状的属性

实例化类之后,需要使用setter方法设置形状的属性。

例如,要绘制3D框,您需要传递其宽度,高度,深度。您可以使用各自的setter方法指定这些值,如下所示 -

1
2
3
4
//Setting the properties of the Box 
box.setWidth(200.0);
box.setHeight(400.0);
box.setDepth(200.0);

将形状对象添加到组

最后,您需要将形状的对象添加到组中,方法是将其作为构造函数的参数传递,如下所示。

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

下表提供了JavaFX提供的各种3D形状的列表。

序号 形状和描述
1 Box(盒子)长方体是具有长度(深度),宽度高度的三维形状。在JavaFX中,三维框由名为Box的类表示。该类属于包javafx.scene.shape。通过实例化此类,您可以在JavaFX中创建Box节点。该类有double数据类型的3个属性,即 -width - 框的宽度。height - 盒子的高度。深度 - 盒子的深度。
2 Cylinder(圆筒)圆柱体是封闭的固体,其具有通过弯曲表面连接的两个平行(大部分为圆形)基部。它由两个参数描述,即其圆形底座的半径和圆柱体的高度。在JavaFX中,圆柱体由名为Cylinder的类表示。该类属于包javafx.scene.shape。通过实例化此类,您可以在JavaFX中创建柱面节点。该类具有double数据类型的2个属性,即 -height - 气缸的高度。radius - 圆柱的半径。
3 Sphere(球)球体被定义为与3D空间中的给定点距离r相同的点集。该距离r是球体的半径,给定点是球体的中心。在JavaFX中,球体由名为Sphere的类表示。该类属于包javafx.scene.shape。通过实例化此类,您可以在JavaFX中创建一个球体节点。此类具有名为radius of double数据类型的属性。它代表球体的半径。

3D对象的属性

对于所有3维对象,您可以设置各种属性,如剔除面,绘图模式,材质。

以下部分讨论3D对象的属性。

剔脸

通常,剔除是去除形状的不正确定向部分(在视图区域中不可见)。

Cull Face属性的类型为CullFace,它表示3D形状的Cull Face。您可以使用方法setCullFace()设置形状的剔除面,如下所示 -

1
box.setCullFace(CullFace.NONE);

形状的笔划类型可以是 -

  • None 无 - 不执行剔除(CullFace.NONE)。
  • Front 正面 - 所有正面饰面都被剔除。(CullFace.FRONT)。
  • Back 返回 - 所有背面多边形都被剔除。(StrokeType.BACK)。

默认情况下,三维形状的剔除面是“后退”。

以下程序是演示球体的各种剔除面的示例。将此代码保存在名为SphereCullFace.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
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.CullFace;
import javafx.stage.Stage;
import javafx.scene.shape.Sphere;

public class SphereCullFace extends Application {
@Override
public void start(Stage stage) {
//Drawing Sphere1
Sphere sphere1 = new Sphere();

//Setting the radius of the Sphere
sphere1.setRadius(50.0);

//Setting the position of the sphere
sphere1.setTranslateX(100);
sphere1.setTranslateY(150);

//setting the cull face of the sphere to front
sphere1.setCullFace(CullFace.FRONT);

//Drawing Sphere2
Sphere sphere2 = new Sphere();

//Setting the radius of the Sphere
sphere2.setRadius(50.0);

//Setting the position of the sphere
sphere2.setTranslateX(300);
sphere2.setTranslateY(150);

//Setting the cull face of the sphere to back
sphere2.setCullFace(CullFace.BACK);

//Drawing Sphere3
Sphere sphere3 = new Sphere();

//Setting the radius of the Sphere
sphere3.setRadius(50.0);

//Setting the position of the sphere
sphere3.setTranslateX(500);
sphere3.setTranslateY(150);

//Setting the cull face of the sphere to none
sphere2.setCullFace(CullFace.NONE);

//Creating a Group object
Group root = new Group(sphere1, sphere2, sphere3);

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

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

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

执行时,上述程序生成一个JavaFX窗口,显示三个球体,其剔除面值分别为FRONT,BACKNONE,如下所示 -

剔脸

绘图模式

属性是DrawMode类型,它表示用于绘制当前3D形状的绘制模式。您可以使用setDrawMode()方法选择绘制模式来绘制3D形状,如下所示 -

1
box.setDrawMode(DrawMode.FILL);

在JavaFX中,您可以选择两种绘制模式来绘制3D形状,它们是 -

  • Fill 填充 - 此模式绘制并填充2D形状(DrawMode.FILL)。
  • Line 线 - 此模式使用线条绘制3D形状(DrawMode.LINE)。

默认情况下,3D维形状的绘制模式为填充。

以下程序是演示3D盒子的各种绘制模式的示例。将此代码保存在名为BoxDrawMode.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
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.stage.Stage;

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

//Setting the properties of the Box
box1.setWidth(100.0);
box1.setHeight(100.0);
box1.setDepth(100.0);

//Setting the position of the box
box1.setTranslateX(200);
box1.setTranslateY(150);
box1.setTranslateZ(0);

//Setting the drawing mode of the box
box1.setDrawMode(DrawMode.LINE);

//Drawing a Box
Box box2 = new Box();

//Setting the properties of the Box
box2.setWidth(100.0);
box2.setHeight(100.0);
box2.setDepth(100.0);

//Setting the position of the box
box2.setTranslateX(450); //450
box2.setTranslateY(150);//150
box2.setTranslateZ(300);

//Setting the drawing mode of the box
box2.setDrawMode(DrawMode.FILL);

//Creating a Group object
Group root = new Group(box1, box2);

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

//Setting camera
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(0);
camera.setTranslateY(0);
camera.setTranslateZ(0);
scene.setCamera(camera);

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

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

执行时,上面的程序生成一个JavaFX窗口,显示两个分别为绘图模式值LINE和FILL的框,如下所示 -

绘图模式

材料

剔除Face属性属于Material类型,用于选择3D形状材质的表面。您可以使用setCullFace()方法设置3D形状的材质,如下所示 -

1
cylinder.setMaterial(material);

如上所述,此方法需要传递Material类型的对象。包javafx.scene.paintPhongMaterial类是此类的子类,它提供了7个表示Phong着色材质的属性。您可以使用这些属性的setter方法将所有这些类型的材质应用于3D形状的表面。

以下是JavaFX中可用的材料类型 -

  • bumpMap - 这表示存储为RGB图像的法线贴图。
  • diffuseMap - 这表示漫反射贴图。
  • selfIlluminationMap - 这表示此PhongMaterial的自发光地图。
  • specularMap - 这表示此PhongMaterial的高光贴图。
  • diffuseColor - 这表示此PhongMaterial的漫反射颜色。
  • specularColor - 这表示此PhongMaterial的镜面反射颜色。
  • specularPower - 这代表了这个PhongMaterial的镜面反射力。

默认情况下,三维形状的材质是PhongMaterial,具有浅灰色的漫反射颜色。

以下是在圆筒上显示各种材料的示例。将此代码保存在名为CylinderMaterials.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.stage.Stage;

public class CylinderMaterials extends Application {
@Override
public void start(Stage stage) {
//Drawing Cylinder1
Cylinder cylinder1 = new Cylinder();

//Setting the properties of the Cylinder
cylinder1.setHeight(130.0f);
cylinder1.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder1.setTranslateX(100);
cylinder1.setTranslateY(75);

//Preparing the phong material of type bump map
PhongMaterial material1 = new PhongMaterial();
material1.setBumpMap(new Image
("https://www.ljjyy.com/img/tp-logo.gif"));

//Setting the bump map material to Cylinder1
cylinder1.setMaterial(material1);

//Drawing Cylinder2
Cylinder cylinder2 = new Cylinder();

//Setting the properties of the Cylinder
cylinder2.setHeight(130.0f);
cylinder2.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder2.setTranslateX(200);
cylinder2.setTranslateY(75);

//Preparing the phong material of type diffuse map
PhongMaterial material2 = new PhongMaterial();
material2.setDiffuseMap(new Image
("https://www.ljjyy.com/img/tp-logo.gif"));

//Setting the diffuse map material to Cylinder2
cylinder2.setMaterial(material2);

//Drawing Cylinder3
Cylinder cylinder3 = new Cylinder();

//Setting the properties of the Cylinder
cylinder3.setHeight(130.0f);
cylinder3.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder3.setTranslateX(300);
cylinder3.setTranslateY(75);

//Preparing the phong material of type Self Illumination Map
PhongMaterial material3 = new PhongMaterial();
material3.setSelfIlluminationMap(new Image
("https://www.ljjyy.com/img/tp-logo.gif"));

//Setting the Self Illumination Map material to Cylinder3
cylinder3.setMaterial(material3);

//Drawing Cylinder4
Cylinder cylinder4 = new Cylinder();

//Setting the properties of the Cylinder
cylinder4.setHeight(130.0f);
cylinder4.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder4.setTranslateX(400);
cylinder4.setTranslateY(75);

//Preparing the phong material of type Specular Map
PhongMaterial material4 = new PhongMaterial();
material4.setSpecularMap(new Image
("https://www.ljjyy.com/img/tp-logo.gif"));

//Setting the Specular Map material to Cylinder4
cylinder4.setMaterial(material4);

//Drawing Cylinder5
Cylinder cylinder5 = new Cylinder();

//Setting the properties of the Cylinder
cylinder5.setHeight(130.0f);
cylinder5.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder5.setTranslateX(100);
cylinder5.setTranslateY(300);

//Preparing the phong material of type diffuse color
PhongMaterial material5 = new PhongMaterial();
material5.setDiffuseColor(Color.BLANCHEDALMOND);

//Setting the diffuse color material to Cylinder5
cylinder5.setMaterial(material5);

//Drawing Cylinder6
Cylinder cylinder6 = new Cylinder();

//Setting the properties of the Cylinder
cylinder6.setHeight(130.0f);
cylinder6.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder6.setTranslateX(200);
cylinder6.setTranslateY(300);

//Preparing the phong material of type specular color
PhongMaterial material6 = new PhongMaterial();

//setting the specular color map to the material
material6.setSpecularColor(Color.BLANCHEDALMOND);

//Setting the specular color material to Cylinder6
cylinder6.setMaterial(material6);

//Drawing Cylinder7
Cylinder cylinder7 = new Cylinder();

//Setting the properties of the Cylinder
cylinder7.setHeight(130.0f);
cylinder7.setRadius(30.0f);

//Setting the position of the Cylinder
cylinder7.setTranslateX(300);
cylinder7.setTranslateY(300);

//Preparing the phong material of type Specular Power
PhongMaterial material7 = new PhongMaterial();
material7.setSpecularPower(0.1);

//Setting the Specular Power material to the Cylinder
cylinder7.setMaterial(material7);

//Creating a Group object
Group root = new Group(cylinder1 ,cylinder2, cylinder3,
cylinder4, cylinder5, cylinder6, cylinder7);

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

//Setting camera
PerspectiveCamera camera = new PerspectiveCamera(false);
camera.setTranslateX(0);
camera.setTranslateY(0);
camera.setTranslateZ(-10);
scene.setCamera(camera);

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

执行时,上面的程序生成一个JavaFX窗口,分别显示7个圆柱体,包括材质,凹凸贴图,漫反射贴图,自发光贴图,镜面贴图,漫反射颜色,镜面反射颜色,(BLANCHEDALMOND)镜面反射功率,如下面的屏幕截图所示 -

气缸材料