< Zurück | Inhalt | Weiter >

17.1 Building the Java 3D Swing application

Rather than embarking on a theoretical HelloUniverse−type example using Swing, we will dive straight in and start building a much more realistic Java 3D application. The application (SwingTest) has the following features:


It uses Swing components, including a JmenuBar−based menu system, for the UI.

The Java 3D scene is modified at runtime through the UI.

The Java 3D scenegraph, used for rendering, is built from scratch rather than using the

SimpleUniverse utility class.

It uses Java 3D 1.2 off−screen rendering support to render frames into an image.

It saves the captured images to disk in JPEG format.

It supports running as an applet.


The Java 3D demo applications from Sun used the SimpleUniverse utility class to build the scenegraph for the applications. SwingTest will build the whole scenegraph from scratch, so you can relate the various classes to each other and how they are combined to produce screen output (figure 17.1). The SimpleUniverse class is useful for quick Java 3D prototypes; however, it hides many of the interesting and powerful features of Java 3D’s view model.


The code in this example is all in one class, SwingTest, which is actually a Swing JPanel. This is the approach that has been taken for much of the example code, but you should not emulate it for your applications. You should aim to break your application’s functionality into far more discrete pieces, pushing the functionality down the class hierarchy as much as possible. Other examples use utility classes defined in the org.selman.java3d.book package, which you should be able to reuse in building your applications. This example does not rely on any external library code, so you can see the entire application and how all the pieces relate to one another.

image


Figure 17.1The SwingTest example running as an application. Combines Swing, Java 3D, dynamic scenegraph modifications, and off−screen rendering


 

From SwingTest.java