Scene Graph Creation


Integrating Java 3D and Swing

Checkers3D is a JFrame where GUI controls, such as Swing text fields and buttons, would be placed if necessary. In this example, it creates an instance of WrapCheckers3D (a JPanel) and places it in the center of a BorderLayout:

     c.setLayout( new BorderLayout( ) );     WrapCheckers3D w3d = new WrapCheckers3D( );   // panel for 3D canvas     c.add(w3d, BorderLayout.CENTER);

The Canvas3D view onto the scene is created inside WrapCheckers3D:

     public WrapCheckers3D( )     {       setLayout( new BorderLayout( ) );       // other initialization code       GraphicsConfiguration config =                  SimpleUniverse.getPreferredConfiguration( );       Canvas3D canvas3D = new Canvas3D(config);       add("Center", canvas3D);       // other initialization code}

Some care must be taken when using Canvas3D since it's a heavyweight GUI element (a thin layer over an OS-generated window). Heavyweight components aren't easily combined with Swing controls, which are lightweight; the controls are mostly generated by Java. Problems are avoided if the Canvas3D object is embedded in a JPanel; then the panel can be safely integrated with the rest of the Swing-built application.

There's a detailed discussion of the issues related to combining Canvas3D and Swing at j3d.org (http://www.j3d.org/tutorials/quick_fix/swing.html).


Compared to applications in earlier chapters, there's no update/draw animation loop. This is unnecessary because Java 3D contains its own mechanism for monitoring changes in the scene and initiating rendering. Here is the algorithm in pseudocode form:

     while(true) {       process user input;       if (exit request) break;       perform behaviors;       if (scene graph has changed)         traverse scene graph and render;     }

Behaviors are scene graph nodes containing code that can influence other parts of the graph, such as moving shapes or changing the lighting. They may be used for monitoring the graph, passing details to the non-3D parts of the application.

The details are more complicated than this pseudocode suggests for example, Java 3D uses multithreading to carry out parallel traversal and rendering. However, having a general idea of the process will help you work through the code in the rest of this chapter.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net