Getting Started With Java3D

Getting Started: Rotating Cube

Introduction

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.

Implementation

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.

8c0e529df39794a8f271e253f8d597cc

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.

6221b84f24cd413cd36a36e01bac5a26

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

We need an object to be able to rotate, so we will create a standard ColorCube and add it to the TransformGroup.

2f7b3dd39ba84f09707005498ad67cb5

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.

aef0f4a5287f2f70b5dc48167309f94b

That is all! When you run the project you will see a rotating 3D cube with a black background.

2a24180c7f8943763beeb984ca7a971e

You can find all of the source code in the Code Examples section of our website.
Click here to view the entire project code

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s