”;
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
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.
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
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
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
After instantiating the class, you need to set the properties for the shape using the setter methods.
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);
The following table gives you the list of various 3D shapes provided by JavaFX.
S.No | Shape & Description |
---|---|
1 |
Box
A 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 −
|
2 | Cylinder
A 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 −
|
3 | Sphere
A 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. |
Properties of 3D Objects
For all the 3 Dimensional objects, you can set various properties in JavaFX. They are listed below −
We will discuss the properties of 3D objects in further chapters of this tutorial.
”;