ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Killer Game Programming in Java: A 3D Checkerboard, Part 1
Pages: 1, 2, 3, 4

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.

TIP: 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.

Scene Graph Creation

The scene graph is created by the constructor for WrapCheckers3D:



    public WrapCheckers3D( )
    {
      // initialization code

      GraphicsConfiguration config =
                 SimpleUniverse.getPreferredConfiguration( );
      Canvas3D canvas3D = new Canvas3D(config);
      add("Center", canvas3D);
      canvas3D.setFocusable(true);     // give focus to the canvas
      canvas3D.requestFocus( );

      su = new SimpleUniverse(canvas3D);

    
      createSceneGraph( );
      initUserPosition( );        // set user's viewpoint
      orbitControls(canvas3D);   // controls for moving the viewpoint

      su.addBranchGraph( sceneBG );
    }

The Canvas3D object is initialized with a configuration obtained from getPreferredConfiguration( ); this method queries the hardware for rendering information. Some older Java 3D programs don't bother initializing a GraphicsConfiguration object, using null as the argument to the Canvas3D constructor instead. This is bad programming practice.

canvas3D is given focus so keyboard events will be sent to behaviors in the scene graph. Behaviors are often triggered by key presses and releases, but they may be triggered by timers, frame changes, and events generated by Java 3D internally. There aren't any behaviors in Checkers3D, so it's not necessary to set the focus. I've left these lines in since they're needed in almost every other program we'll consider.

The su SimpleUniverse object creates a standard view branch graph and the VirtualUniverse and Locale nodes of the scene graph. createSceneGraph( ) sets up the lighting, the sky background, the floor, and floating sphere; initUserPosition( ) and orbitControls( ) handle viewer issues. The resulting BranchGroup is added to the scene graph at the end of the method:


    private void createSceneGraph( )
    {
      sceneBG = new BranchGroup( );
      bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE);


      lightScene( );         // add the lights
      addBackground( );      // add the sky
      sceneBG.addChild( new CheckerFloor( ).getBG( ) );  // add floor

      floatingSphere( );     // add the floating sphere

      sceneBG.compile( );   // fix the scene
    } // end of createSceneGraph( )

Various methods add subgraphs to sceneBG to build the content branch graph. sceneBG is compiled once the graph has been finalized to allow Java 3D to optimize it. The optimizations may involve reordering the graph and regrouping and combining nodes. For example, a chain of TransformGroup nodes containing different translations may be combined into a single node. Another possibility is to group all the shapes with the same appearance properties together, so they can be rendered more quickly.

bounds is a global BoundingSphere used to specify the influence of environment nodes for lighting, background, and the OrbitBehavior object. The bounding sphere is placed at the center of the scene and affects everything within a BOUNDSIZE units radius. Bounding boxes and polytopes are available in Java 3D.

The scene graph by the end of WrapCheckers3D( ) is shown in Figure 15-3.

The "Floor Branch" node is my invention to hide some details until later. Missing from Figure 15-3 is the view branch part of the scene graph.

Pages: 1, 2, 3, 4

Next Pagearrow