Using a Behavior Class

Recipe for Using a Behavior Class

  1. prepare the scene graph (by adding a TransformGroup or other necessary objects)

  2. insert behavior object in the scene graph, referencing the object of change

  3. specify a scheduling bounds (or SchedulingBoundingLeaf)

  4. set write (and read) capabilities for the target object (as appropriate)

Example of use of the last example

public BranchGroup createSceneGraph() {
	// 1. Create the root of the branch graph
	BranchGroup objRoot = new BranchGroup();

	TransformGroup objRotate = new TransformGroup();
	// 4. 
	objRotate.addChild(new ColorCube(0.4));

	// 2.
	SimpleBehavior myRotationBehavior = new SimpleBehavior(objRotate);
	// 3.
	myRotationBehavior.setSchedulingBounds(new BoundingSphere());
	// 2.
	// Let Java 3D perform optimizations on this scene graph.

	return objRoot;
} // end of CreateSceneGraph method of SimpleBehaviorApp

Finding or writing the appropriate behavior class for your application is the beginning of writing an interactive Java 3D program. The first step in adding a behavior involves making sure the scene graph makes provisions for the behavior. For example, to use the SimpleBehavior class from the previous section there must be a TransformGroup in the scene graph above the object(s) to be rotated.
Many behaviors need only a single TransformGroup object; however, scene graph requirements for a behavior is application and behavior dependent and may be more complex.

Having established the support for a behavior, an instance of the class must be added to the scene graph. Without being a part of a live scene graph, there is no way a behavior can be initialized. In fact, a behavior object that is not part of the scene graph will become garbage and be eliminated on the next garbage collection.

The last step for adding a behavior is to provide a scheduling bounds for the behavior. To improve efficiency, Java 3D uses the scheduling bounds to perform execution culling. Behavior is only active when its scheduling bounds intersects a ViewPlatform's activation volume. Only active behaviors are eligible to receive stimuli. In this way, stimuli can be ignored for some behaviors. The programmer has control over the execution culling through the selection of the scheduling bounds of the behavior.