Getting Started With Java3D

Getting Started: The JFrame

Introduction

By default Java3D uses its own default canvas to run programs on. However, in my own personal opinion I much prefer to use the Swing UI JFrame. The JFrame uses one of Java’s inbuilt libraries to create 2D interfaces.

Implementation

Let’s get started, if you haven’t already you need to setup your environment with an empty class at the ready. Make sure you import all of the required libraries, if you’re not sure how to do that take a look at the first tutorial on how to setup your environment for Java3D here.

To stay consistent please call your Java class “Main”. You are able to use the default package for this tutorial as only one class is needed.

Setting up the Class

First of all we need to create the necessary imports to access Java Swing, we can then inherit the JPanel class using the keyword extends.

998cbc899ad4099768d133025196997d

Adding Some Necessities

There is a small issue with Java3D in its current state. When creating new applications either in the standard canvas or in a JFrame the screen flickers when being resized/moved, it can also cause your screen to appear white. Here is a minor fix for that problem.

We the “sun.awt.noerasebackground” property to true and disable the ToolTipManager and JPopupMenu

9da061a1872b23808f19c585cd5a9c2e

Your main method should now look like the code above, this will also require you to make two new imports. If you haven’t already implement the imports as shown below.

5a50101650358d6035a4377fc96aa7ba

Create the JFrame and Set its Properties

We are now at the point were we can create the JFrame. First we need to create a new Instance of the class, this is used to call the constructor method “public Main()”, that we made earlier.

After that we can create a new instance of the JFrame and then set its properties. For this example we only need to set three different properties, its size, its contents and whether or not it should be visible by default. The contents of the JFrame will be the “main” itself, this is because we want the JFrame to hold the Java3D canvas rather than running the canvas as a standalone application.

All of this code is added into the main method as shown below.

07a1e7e94e32fe967193620f959ed58b

You will also need to add one new import for this new code, if you haven’t already you can implement it as shown below.

15a7e17fb170f6bd8bb4ad8e37912190

Setting up the 3D Canvas and Creating the Universe

Now we need to create the 3D Canvas. Moving into the constructor method (“public Main()”). Firstly we need to set the layout, this is a very simple and standard procedure “setLayout(new BorderLayout());” will do the trick.

Then a few new objects need to be made, the GraphicsConfiguration, the Canvas3D, the SimpleUniverse and the BranchGroup. When the canvas is created we pass the Graphics configuration so that the canvas is created with those configurations and the simple universe passes the canvas.

Adding the canvas to the screen simply requires “add(“Center”, canvas”);” this add the canvas into the center of the screen.

We can then set up some of the properties for the Universe, the “setNominalViewingTransform();” is used to move the viewing platform so that the objects in the scene can be viewed. “setBackClipDistance(x);” is used to set the render distance, were “x” is the render distance. Finally we add the branch into the Universe.

cb9c676eb019833444c4f7c5b527bf33

For this code you will need to update your imports as this is the final piece of code to be implemented I will display the full list of imports below.

2f0ae70f2e37cc9445ca4de22dfc737e

The project is now complete, to test that the JFrame has been setup correctly I made a small code amendment.

c71f3487d6f9631c73a3c2508052c406

The parameter passed here sets the title for the JFrame, if the JFrame executes successfully then the title of the JFrame will be displayed.

b3cdc001e0b59e3480be22b3a00114b5

There you have it, the JFrame is the size we set it within the code and has the title which I also implemented previously.

This scene obviously does not have any content, in the next tutorial we will implement a light and a 3D object into the scene.

Make sure you save the code from this tutorial, this will be the base plate for almost every tutorial in the future.

You can also download all of the code for this project from our website in the Code Examples section.
Click here to go directly to the projects Source Code

Advertisements

One thought on “Getting Started: The JFrame

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