Java Programming: Add, Animate, and Light Up Objects in 3D

By Doug Lowe

Creating a virtual 3D world is easier than ever in JavaFX. In this example, you learn to add a sphere to your JavaFX program, animate it, and add a light source to it.

Step one: Add a sphere

First, add a sphere, represented by the Sphere class. The Sphere constructor accepts just a single parameter, which specifies the radius of the sphere. For example, these lines create a sphere whose radius is 100, and then translates it to move it off the center point of your virtual world:

Sphere sphere = new Sphere(100);
sphere.setTranslateX(-180);
sphere.setTranslateY(-100);
sphere.setTranslateZ(100);
root.getChildren().add(sphere);

Rather than apply the same blue Phong material to the sphere, you can do something more interesting: apply a Phong material constructed from an image of a cylindrical projection of the earth using this code:

Image earthImage = new Image("file:earth.jpg");
PhongMaterial earthPhong = new PhongMaterial();
earthPhong.setDiffuseMap(earthImage);
sphere.setMaterial(earthPhong); 

This figure shows the resulting sphere.

A sphere with a cylindrical projection of the earth applied as the Phong material.

A sphere with a cylindrical projection of the earth applied as the Phong material.

You can wrap any image around a sphere (or any other 3D object, for that matter) using this technique. You can also obtain the image for this program from Wikipedia. Just search for Behrmann Projection and then download the file. (Use Windows Paint to crop the edges of the image a bit because the image available on Wikipedia has a small border around the edges.)

Step two: Animate the objects

Whew! Your 3D virtual world now has an object: a sphere that looks like the earth. You can also add other objects at this time.

In this step, add an animation to all objects to get them spinning. Each object gets a simple RotationTransition animation. First, the sphere:

        RotateTransition rt4 = new RotateTransition();
        rt4.setNode(sphere);
        rt4.setDuration(Duration.millis(9000));
        rt4.setAxis(Rotate.Y_AXIS);
        rt4.setByAngle(360);
        rt4.setCycleCount(Animation.INDEFINITE);
        rt4.setInterpolator(Interpolator.LINEAR);
        rt4.play();

After the play method is called, the sphere starts spinning, making one complete turn around its z-axis every three seconds.

The sphere rotates around the y-axis, creating the impression that the world is revolving. For the sphere, the speed is set to one revolution every 10 seconds.

Step three: Add a light source

The last step into this foray into the world of 3D programming is to add a light source. The light source will change the whole look of the scene, as shown here.

To add the light source, use the following code:

Your 3D world with a light source.

Your 3D world with a light source.
PointLight light = new PointLight(Color.WHITE);
light.setTranslateX(-1000);
light.setTranslateY(100);
light.setTranslateZ(-1000);
root.getChildren().add(light);

The PointLight class defines a light source that originates from a specific point in the scene and projects light of the given color (in this case, good old white). To create the lighting effect, relocate the light by translating its coordinates 1,000 to the left, 100 down, and 1,000 units toward the user. The result casts nice shadows on the backsides of the spinning objects.