// // CLASS // ExSwitch - illustrate use of switches // // LESSON // Add a Switch grouping node to select among several shapes to render. // // AUTHOR // Michael J. Bailey / San Diego Supercomputer Center // import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.lang.*; import java.net.*; import java.util.*; import javax.media.j3d.*; import javax.vecmath.*; import com.sun.j3d.utils.image.*; import com.sun.j3d.utils.geometry.*; public class ExSwitch extends Example { // nodes that can be updated via a menu: Switch group = null; private int currentSwitch = 0; // Build the scene: public Group buildScene() { // Turn on the headlight setHeadlightEnable( true ); // Build the scene root group = new Switch( ); group.setCapability( Switch.ALLOW_SWITCH_WRITE ); // Create application bounds BoundingSphere worldBounds = new BoundingSphere( new Point3d( 0.0, 0.0, 0.0 ), // Center 1000.0 ); // Extent Transform3D t3d; Appearance app0 = new Appearance( ); Material mat0 = new Material(); mat0.setAmbientColor( 0.8f, 0.4f, 0.2f ); mat0.setDiffuseColor( 0.8f, 0.4f, 0.2f ); mat0.setSpecularColor( 0.0f, 0.0f, 0.f ); app0.setMaterial( mat0 ); t3d = new Transform3D( ); t3d.setTranslation( new Vector3f( -2.f, 0.f, 0.f ) ); TransformGroup tg0 = new TransformGroup( t3d ); Sphere sph0 = new Sphere( 0.5f, Primitive.GENERATE_NORMALS, 16, app0 ); tg0.addChild( sph0 ); group.addChild( tg0 ); Appearance app1 = new Appearance( ); Material mat1 = new Material(); mat1.setAmbientColor( 0.2f, 0.8f, 0.4f ); mat1.setDiffuseColor( 0.2f, 0.8f, 0.4f ); mat1.setSpecularColor( 0.0f, 0.0f, 0.f ); app1.setMaterial( mat1 ); t3d.setTranslation( new Vector3f( 0.f, 0.f, 0.f ) ); TransformGroup tg1 = new TransformGroup( t3d ); Sphere sph1 = new Sphere( 1.5f, Primitive.GENERATE_NORMALS, 16, app1 ); tg1.addChild( sph1 ); group.addChild( tg1 ); Appearance app2 = new Appearance( ); Material mat2 = new Material(); mat2.setAmbientColor( 0.4f, 0.2f, 0.8f ); mat2.setDiffuseColor( 0.4f, 0.2f, 0.8f ); mat2.setSpecularColor( 0.0f, 0.0f, 0.f ); app2.setMaterial( mat2 ); t3d.setTranslation( new Vector3f( 2.f, 0.f, 0.f ) ); TransformGroup tg2 = new TransformGroup( t3d ); Sphere sph2 = new Sphere( 1.0f, Primitive.GENERATE_NORMALS, 16, app2 ); tg2.addChild( sph2 ); group.addChild( tg2 ); group.setWhichChild( options[currentSwitch].child ); return group; } // // Main (if invoked as an application) // public static void main( String[] args ) { ExSwitch ex = new ExSwitch(); ex.initialize( args ); ex.buildUniverse(); ex.showFrame(); } private NameChildMask[] options = { new NameChildMask( "CHILD_ALL", Switch.CHILD_ALL, 0 ), new NameChildMask( "CHILD_NONE", Switch.CHILD_NONE, 0 ), new NameChildMask( "0", 0, 0 ), new NameChildMask( "1", 1, 0 ), new NameChildMask( "2", 2, 0 ), new NameChildMask( "0+1", Switch.CHILD_MASK, 3 ), new NameChildMask( "0+2", Switch.CHILD_MASK, 5 ), new NameChildMask( "1+2", Switch.CHILD_MASK, 6 ), }; private CheckboxMenuItem[] switchMenu; // // Initialize the GUI (application and applet) // public void initialize( String[] args ) { // Initialize the window, menubar, etc. super.initialize( args ); exampleFrame.setTitle( "Java 3D Switch Example" ); // Add a menu to select among switch options Menu mt = new Menu( "Switch" ); switchMenu = new CheckboxMenuItem[ options.length ]; for( int i = 0; i < options.length; i++ ) { switchMenu[i] = new CheckboxMenuItem( options[i].name ); switchMenu[i].addItemListener( this ); switchMenu[i].setState( false ); mt.add( switchMenu[i] ); } exampleMenuBar.add( mt ); currentSwitch = 0; switchMenu[currentSwitch].setState( true ); } // // Handle checkboxes // public void itemStateChanged( ItemEvent event ) { Object src = event.getSource(); // Check if it is switch choice for( int i = 0; i < switchMenu.length; i++ ) { if( src == switchMenu[i] ) { // Update the checkboxes switchMenu[currentSwitch].setState( false ); currentSwitch = i; switchMenu[currentSwitch].setState( true ); // Set the switch group.setWhichChild( options[currentSwitch].child ); group.setChildMask( options[currentSwitch].mask ); return; } } // Handle all other checkboxes super.itemStateChanged( event ); } public class NameChildMask { public String name; public int child; public BitSet mask; public NameChildMask( String n, int c, int m ) { name = n; child = c; mask = new BitSet(3); if( (m&1) != 0 ) mask.set( 0 ); if( (m&2) != 0 ) mask.set( 1 ); if( (m&4) != 0 ) mask.set( 2 ); } } }