I will assume that you have Java3D downloaded and installed and that you can successfully run all the demo applets. All the code I have been working with is based on Java 1.4.1 as well so I can’t guarantee any of this will work on an older VM. I'm not going to entirely guarantee that it works anyway, but that is neither here nor there. As the ancient proverb states - wise is he who uses the Javadocs”–you will find the platform documentation is an invaluable resource in all your attempts to work out what on earth the different parts of the API work.
The approach Java3D takes to creating 3 dimensional worlds is to create a Universe that contains everything you might want to see, hear, think about, and so on. This universe is sort of like a tree, but a tree whose leaves are elements of a 3d world. In this sense it can be compared with the World Tree of Viking mythology, but that is not relevant so I'm not going to go there. Instead we will look at the basics that you will need to deal with for this tutorial.
The fundamental universe begins as a completely empty space, containing neither a viewer nor anything for them to view. When starting out, it is useful for us to use a utility class that gives us both universe and viewer set up and ready to go. This is the aptly named SimpleUniverse (com.sun.j3d.utils.universe.SimpleUniverse) and although you will almost certainly want to customise your universe eventually, for now it is fine.
If we look at almost any basic Java3D program we are likely to find something like this in it’s initialisation:
GraphicsConfiguration
config = SimpleUniverse.getPreferredConfiguration();
Canvas3D
c = new Canvas3D(config);
add("Center",
c);
u
= new SimpleUniverse(c);
//BranchGroup
scene = createSceneGraph();
//u.addBranchGraph(scene);
//System.out.println("Should
be visible now.");
}
}
You probably know what all this does anyway but lets go through it again: You set the layout as for the Applet as a whole- this is not so relevant to J3D so Iets jump on. Next up we create a new GraphicsConfiguration by asking for the static SimpleUniverse method getPreferredConfiguration –this basically takes a look at the user’s graphics system and tells Java3D what type of configuration it thinks will work best with their hardware. Next up we create a new Canvas3D that matches that configuration- a Canvas3D is the component that the Java3D api uses to display it’s output. It is like an AWT canvas and it is heavyweight. That means that it is tied to the native platform and if you use any Swing components around it they will potentially fall behind it. There are numerous tutorials around the place on how to avoid this, but people are always asking about this in various places and a reminder can’t hurt. Of course if you want to maximise your performance you will need to run the whole thing in fullscreen and write your own overlays for interface stuff, but that is a long long way beyond the scope of this tutorial so don’t get your hopes up.
Next up we add the Canvas3D to the applet frame. Then we create a new SimpleUniverse around the Canvas3D. That is all there is to it. The commented lines afterwards create the scene and add it to the universe. As we haven’t written the createSceneGraph() method yet it would be a little pre-emptive to include it in the code.
Back to the Index | Next: Creating The Scene