Javafx 简明教程
JavaFX - 3D Shapes
在前面的章节中,我们已经了解如何在 JavaFX 应用程序中在 XY 平面中绘制二维图形。除了这些二维图形外,我们还可以使用 JavaFX 绘制其他几个三维图形。
In the earlier chapters, we have seen how to draw 2D shapes on an XY plane in a JavaFX application. In addition to these 2D shapes, we can draw several other 3D shapes as well using JavaFX.
3D Shape
总的来说,三维图形是在 XYZ 平面中可以绘制的几何图形。它们由两个或更多个维度定义,通常为长度、宽度和深度。JavaFX 支持的三维图形包括 Cylinder, Sphere 和 Box 。
In general, a 3D shape is a geometrical figure that can be drawn on the XYZ plane. They are defined by two or more dimensions, commonly length, width and depth. 3D shapes supported by JavaFX include a Cylinder, Sphere and a Box.
以上提到的每个 3D 形状都由一个类表示,且所有这些类都属于包 javafx.scene.shape 。名为 Shape3D 的类是 JavaFX 中所有 3D 形状的基类。
Each of the above mentioned 3D shape is represented by a class and all these classes belong to the package javafx.scene.shape. The class named Shape3D is the base class of all the 3-Dimensional shapes in JavaFX.
Creating a 3D Shape
要创建一个 3D 形状,你需要:
To create a 3-Dimensional shape, you need to −
-
Instantiate the respective class of the required 3D shape.
-
Set the properties of the 3D shape.
-
Add the 3D shape object to the group.
Instantiating the Respective Class
要创建一个 3D 形状,首先需要实例化其相应类。例如,如果要创建一个 3D 盒子,你需要实例化名为 Box 的类,如下所示:
To create a 3-Dimensional shape, first of all you need to instantiate its respective class. For example, if you want to create a 3D box, you need to instantiate the class named Box as follows −
Box box = new Box();
Setting the Properties of the Shape
实例化该类后,你需要使用 setter 方法为该形状设置属性。
After instantiating the class, you need to set the properties for the shape using the setter methods.
例如,要绘制一个 3D 盒子,你需要传递其宽度、高度和深度。你可以使用其各自的 setter 方法指定这些值,如下所示:
For example, to draw a 3D box you need to pass its Width, Height, Depth. You can specify these values using their respective setter methods as follows −
//Setting the properties of the Box
box.setWidth(200.0);
box.setHeight(400.0);
box.setDepth(200.0);
Adding the Shape Object to the Group
最后,你需要将形状的对象添加到组中,方法是将其作为构造函数的参数进行传递,如下所示。
Finally, you need to add the object of the shape to the group by passing it as a parameter of the constructor as shown below.
//Creating a Group object
Group root = new Group(box);
下表提供了 JavaFX 提供的各种 3D 形状的列表。
The following table gives you the list of various 3D shapes provided by JavaFX.
S.No |
Shape & Description |
1 |
BoxA cuboid is a three-dimensional shape with a length (depth), width, and a height. In JavaFX a three-dimensional box is represented by a class named Box. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a Box node in JavaFX. This class has 3 properties of the double datatype namely − width − The width of the box. height − The height of the box. depth − The depth of the box. |
2 |
CylinderA cylinder is a closed solid that has two parallel (mostly circular) bases connected by a curved surface. It is described by two parameters, namely, the radius of its circular base and the height of the cylinder. In JavaFX, a cylinder is represented by a class named Cylinder. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a cylinder node in JavaFX. This class has 2 properties of the double datatype namely − height − The height of the Cylinder. radius − The radius of the Cylinder. |
3 |
SphereA sphere is defined as the set of points that are all at the same distance r from a given point in a 3D space. This distance r is the radius of the sphere and the given point is the centre of the sphere. In JavaFX, a sphere is represented by a class named Sphere. This class belongs to the package javafx.scene.shape. By instantiating this class, you can create a sphere node in JavaFX. This class has a property named radius of double datatype. It represents the radius of a Sphere. |