< Zurück | Inhalt | Weiter >

From SwitchTest.java

//create the Switch Node

Switch switchGroup = new Switch( Switch.CHILD_MASK ); switchGroup.setCapability( Switch.ALLOW_SWITCH_WRITE );

switchGroup.addChild( createLabel( "Child Node 1", labelScale ) ); switchGroup.addChild( createLabel( "Child Node 2", labelScale ) ); switchGroup.addChild( createLabel( "Child Node 3", labelScale ) ); switchGroup.addChild( createLabel( "Child Node 4", labelScale ) ); switchGroup.addChild( createLabel( "Child Node 5", labelScale ) ); switchGroup.addChild( createLabel( "Child Node 6", labelScale ) ); switchGroup.addChild( createLabel( "Child Node 7", labelScale ) );

//create the logical mask to control Node visibility java.util.BitSet visibleNodes = new java.util.BitSet( switchGroup.numChildren() );

//make the third, sixth and seventh nodes visible visibleNodes.set( 2 );

visibleNodes.set( 5 );

visibleNodes.set( 6 );

//assign the visibility mask to the Switch switchGroup.setChildMask( visibleNodes );


The output of the SwitchTest example is shown in figure 5.1.


Figure 5.1 The effect of using a BitSet mask and a Switch Node. On the left, the Switch Node has been created with the Switch.CHILD_ALL attribute. On the right, a BitSet has been created to display the third, sixth, and seventh Node through a call to setChildMask. The child elements being controlled by the Switch Node are Text2D objects

The Switch Node can be used to implement modal static scenegraphs, that is, scenegraphs that are essentially of a fixed structure but which the user can influence through adding or removing prebuilt sections.

Switch Nodes can also be used to implement simple animation using the SwitchInterpolator Behavior (figure 5.2). The SwitchInterpolator attaches to a Switch Node and cycles the active child of the Switch Node using an Alpha object. For example, a simple 3D “flip−book” style animation could be achieved by adding several versions of a 3D model to a Switch Node and triggering a SwitchInterpolator to cycle from one model to another.


Figure 5.2 Seven frames from the NodesTest example. A Switch Node is created with seven child elements, and a SwitchInterpolator is used to cycle between them. Note that the items at index 3 (row 2, column 1) and 6 (row 3, column

1) use a Link Node to share a single SharedGroup

BEWARE If you choose to use the SwitchInterpolator class, do not add the Interpolator as a child of the Switch Node itself, or the moment the Interpolator is activated it will

deactivate itself, hence stopping the Interpolator.