Getting Started With Java3D

Getting Started: 3D Objects and Light


In this tutorial we will be adding a 3D Sphere object and a light source to illuminate and give color to this 3D object. You will need to have completed the previous tutorial The JFrame in order to complete this tutorial.

What You Will Need to Start

In the previous tutorial we set up a JFrame for the Java3D canvas. We will be continuing on with that exact same code. I recommend taking a copy of the code rather than overwriting it as the blank JFrame code will be used again in the future.

Create a new project (call it whatever you want), import all of the necessary libraries as we did in the very first tutorial and create a class in the default package called “Main”You can then copy the code from the previous tutorial into this new class.


Now it’s time to code. To start I would like to give you all the imports that you will need for this tutorial. They have been condensed, you can delete the imports from your current class and replace them with the ones seen below.


Creating the Sphere

Let’s create the object, in this tutorial we will be working in the constructor method “public Main()”. To create the sphere we use “Sphere sphere = new Sphere(size);”. Where the size parameter is I used the value “0.25f”.

Now we need to add the sphere into the scene, simply use “group.addChild(sphere)”.  This adds the sphere object into the BranchGroup which we have already created. Your constructor method will look similar to the image below.


When you run this program, all you will see is a black screen. This is because there is no lighting within the scene. So lets implement the lighting.

Creating the Light

First of all we need to set a few properties for our light. This is the color of the light along with the direction of the light. To create the color of the light we will create a new Color3f object as done below. Feel free to change the values for the color.


Now we can set the direction of the light, this has been done below. You can change the values for the direction of the light and experiment with what happens when you do that.


Now we can set the BoundingSphere for the object, this will define the position and the distance that the light shines within our scene.


Finally we can create the actual light within the scene and set the color and direction which we already created. Then set the InfluenciningBounds property of the light to the BoundingSphere which we have already created. Finally, we can add the light into the BranchGroup.


That is it, when you run your program you should have a sphere of the color you decided. As I small exercise, change the values of the light direction and the color and see what happens!


Here is the code for the entire constructor method.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s