JavaFX: Working with JavaFX Graphics

2 Shape3D

This chapter gives information about the Shape3D API that is available with the JavaFX 3D Graphics library.

There are two types of 3D shapes:

Pre-defined Shapes

The pre-defined 3D shapes are provided to make it easier for you to quickly create 3D objects out-of-the-box. These shapes include boxes, cylinders, and spheres. Sample usages are shown in Figure 2-1.

Figure 2-1 Pre-defined Shapes

Description of Figure 2-1 follows
Description of "Figure 2-1 Pre-defined Shapes"

Example 2-1 shows the Shape3D class hierarchy. It includes the MeshView class, which defines a surface with the specified 3D mesh data. Also included are the Box, Cylinder, and Sphere subclasses.

Example 2-1 Shape3D Class Hierarchy


Use the following information to create the pre-defined shapes:

  • To create a Box object, specify the dimensions of width, height, and depth.

    Box myBox = new Box(width, height, depth);
  • To create a Cylinder object, specify the radius and height.

    Cylinder myCylinder = new Cylinder(radius, height);
    Cylinder myCylinder2 = new Cylinder(radius, height, divisions);
  • To create a Sphere object, specify the radius.

    Sphere mySphere = new Sphere(radius);
    Sphere mySphere2 = new Sphere(radius, divisions);

Example 2-2 shows lines of code that demonstrate the use of the predefined 3D shapes.

Example 2-2 Sample Usage of Predefined 3D Shapes

       final PhongMaterial redMaterial = new PhongMaterial();
       final PhongMaterial blueMaterial = new PhongMaterial();

       final PhongMaterial greyMaterial = new PhongMaterial();

       final Box red = new Box(400, 400, 400);
       final Sphere blue = new Sphere(200);
       final Cylinder grey = new Cylinder(5, 100);

User-Defined Shapes

Example 2-3 shows the JavaFX Mesh class hierarchy, which contains the TriangleMesh subclass. Triangle mesh is the most typical kind of mesh used in 3D layouts.

Example 2-3 Mesh Class Hierarchy

   javafx.scene.shape.Mesh (abstract)

The TriangleMesh contains separate arrays of points, texture coordinates, and faces that describe a triangulated geometric mesh. Smoothing groups are used to group triangles that are part of the same curved surface. Triangles that are in different smoothing groups form hard edges.

Use the following steps to create a TriangleMesh instance:

  1. Create a new instance of a triangleMesh.

    mesh = new TriangleMesh();
  2. Define the set of points, which are the vertices of the mesh.

    float points[] = { … };
  3. Describe the texture coordinates for each vertex.

    float texCoords[] = { … };
  4. Using the vertices, build the Faces, which are triangles that describe the topology.

    int faces[] = { … };
  5. Define the smoothing group to which each face belongs.

    int smoothingGroups[] = { … };

    Smoothing group adjusts the normal on the vertices for the face to either be smooth or faceted. If every single face has a differing smoothing group, then the mesh will be very faceted. If every single face has the same smoothing group, then the mesh will look very smooth.

Close Window

Table of Contents

JavaFX: Working with JavaFX Graphics

Expand | Collapse