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.
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.
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.
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.
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.
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.
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.
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.
The project is now complete, to test that the JFrame has been setup correctly I made a small code amendment.
The parameter passed here sets the title for the JFrame, if the JFrame executes successfully then the title of the JFrame will be displayed.
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