In this tutorial we will be implementing a rotating cube using the Java3D API. 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 to start, for this tutorial all of the code will be written within the main constructor method. “public Main()”. Starting under the line “BranchGroup group = new BranchGroup();”
Let’s start by adding in the required imports for this project, all of the imports you need are seen below.
Creating the TransformGroup
Now, we can start programming. First of all we need to create a TransformGroup and set the capabilities of that TransformGroup. The TransformGroup can then be added into the BranchGroup which has already been created. So, here is how we do that.
Great, we’ve now created a TransformGroup which we can manipulate. This allows us to Translate/Rotate and do a whole bunch of things to the objects within this TransformGroup.
Creating the Cube
Implementing the Rotation
Now we need to set the rotation for the cube, to do that we will be using a RotationInterpolator which takes in an Alpha value and the TransformGroup to manipulate. We also need to set the SchedulingBounds which sets the Behavior’s scheduling region to the specified bounds.
That is all! When you run the project you will see a rotating 3D cube with a black background.
You can find all of the source code in the Code Examples section of our website.
Click here to view the entire project code