Java3D Tutorials

StdDraw3D: 3D Animation

Introduction

In this tutorial we will be making a sphere move up and down on the screen using the StdDraw3D library.

What You Will Need to Start

For this tutorial you will need to have completed the basic setup for a Java3D application, you will need to import all of the libraries that Java3D applications require. If you haven’t done that and don’t know how to do that, click here to find out how. You will also need to have imported the StdDraw3D class into your project, click here to find out how.

Implementation

Create a new class in your project, I called my class ‘Main’. Then create a main method as the code below shows.

A sphere has been created in the centre of the window however, notice how ‘StdDraw3D.finished()’ has not been used in this instance. This is because we can now animate the sphere, calling ‘finished()’prevents redrawing the screen.

Now we need a while loop to continually redraw the frames. Creating a call to ‘StdDraw3D.clear()’ will clear all of the objects from the current scene and then calling ‘StdDraw3D.show()’ will redraw everything back to the screen.

Finally, the sphere can be moved around the screen. This program will simply demonstrate the sphere moving upwards on the Y-Axis. Create a variable which will be considered as the Y-Axis position of the sphere, create this outside of the loop and set it equal to 0. Then inside the loop increment the value by 0.001, this will increment every frame.

That is it, you are done. The sphere should now slowly move up and off of the screen. You can of course move the camera which is inbuilt into StdDraw3D.