⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java3dworld1.java

📁 java 编程源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// Class Java3DWorld1 is a Java 3D game.
// The goal is to navigate the small red ball in the bottom right 
// corner to the screen's top-left corner without colliding with 
// any of the flying objects. The user can specify the number of 
// flying objects to make the game more difficult.
package com.deitel.advjhtp1.java3dgame;

// Java core packages
import java.awt.*;
import java.net.*;
import java.util.BitSet;

// Java extension packages
import javax.media.j3d.*;
import javax.vecmath.*;

// Java3D utility packages
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.universe.*;

public class Java3DWorld1 extends Canvas3D {
    
   // container dimensions
   private static final int CONTAINER_WIDTH = 600;
   private static final int CONTAINER_HEIGHT = 600;
      
   // constants that specify number of shapes
   private static final int NUMBER_OF_SHAPES = 20;
   private static final int NUMBER_OF_PRIMITIVES = 4;
   
   // initial scene to display in Switch
   private static final int DEFAULT_SCENE = 0;
   
   // constants for animating rotation
   private static final int MAX_ROTATION_SPEED = 25000;
   private static final int MIN_ROTATION_SPEED = 20000;
   private static final float MIN_ROTATION_ANGLE = 0.0f;
   private static final float MAX_ROTATION_ANGLE = 
      ( float ) Math.PI * 8.0f;
   
   // constants for animating translation
   private static final int MAX_TRANSLATION_SPEED = 7500; 
   private static final int MIN_TRANSLATION_SPEED = 2500;
   
   // maximum time until animations begins
   public static final int MAX_PHASE_DELAY = 20000;
   
   // 3D shape information
   private static final float MAX_RADIUS = 0.15f;
   private static final float MAX_LENGTH = 0.2f;
   private static final float MAX_SHININESS = 128.0f;
   private static final float SPHERE_RADIUS = 0.15f;
   private static final float BOUNDING_RADIUS = 100.0f;
   
   private Switch shapeSwitch; // contains flying shapes
   private BoundingSphere bounds; // bounds for nodes and groups
   
   private SimpleUniverse simpleUniverse; // 3D environment
     
   private String imageName; // texture image file name
   
   // Java3DWorld1 constructor
   public Java3DWorld1( String imageFileName ) {
             
      super( SimpleUniverse.getPreferredConfiguration() );
      
      imageName = imageFileName;
      
      // create SimpleUniverse ( 3D Graphics environment )
      simpleUniverse = new SimpleUniverse( this );
      
      // set viewing distance for 3D scene
      ViewingPlatform viewPlatform = 
         simpleUniverse.getViewingPlatform();
         
      viewPlatform.setNominalViewingTransform();
      
      // create 3D scene 
      BranchGroup branchGroup = createScene();
      
      // attach BranchGroup to SimpleUniverse
      simpleUniverse.addBranchGraph( branchGroup );
   
   } // end Java3DWorld1 constructor    

   // create 3D scene
   public BranchGroup createScene() 
   {
      BranchGroup sceneBranchGroup = new BranchGroup();
      
      // create scene Switch group 
      Switch sceneSwitch = initializeSwitch( DEFAULT_SCENE );
      
      // create Switch group containing shapes
      shapeSwitch = initializeSwitch( DEFAULT_SCENE );
      
      // initialize BranchGroup that contains only elements
      // in game scene 
      BranchGroup gameBranchGroup = new BranchGroup();
      
      // add shapeSwitch to gameBranchGroup
      gameBranchGroup.addChild( shapeSwitch ); 
      
      // add gameBranchGroup to sceneSwitch
      sceneSwitch.addChild( gameBranchGroup );
      
      // add sceneSwitch to sceneBranchGroup
      sceneBranchGroup.addChild( sceneSwitch );
      
      // create BoundingSphere for 3D objects and behaviors
      bounds = new BoundingSphere(
         new Point3d( 0.0f, 0.0f, 0.0f ), BOUNDING_RADIUS );
               
      // create rotation TransformGroup array
      TransformGroup[] spinTransform = 
         createTransformGroupArray( NUMBER_OF_SHAPES );
      
      // create translation TransformGroup array
      TransformGroup[] pathTransform = 
         createTransformGroupArray( NUMBER_OF_SHAPES );
      
      // create RotationInterpolators
      createRotationInterpolators( spinTransform, 
         NUMBER_OF_SHAPES );
      
      // create PositonInterpolators
      createPositionInterpolators( pathTransform, 
         NUMBER_OF_SHAPES );
                
      // create Appearance objects for Primitives
      Appearance[] shapeAppearance = 
         createAppearance( NUMBER_OF_SHAPES );
      
      // create shapes
      Primitive[] shapes = 
         createShapes( shapeAppearance, NUMBER_OF_SHAPES ); 
      
      // add shapes to scene structure
      for ( int x = 0; x < NUMBER_OF_SHAPES; x++ ) {
         
         // add primitive to spinTransform group
         spinTransform[ x ].addChild( shapes[ x ] );
         
         // add spinTransform group to pathTransform group
         pathTransform[ x ].addChild( spinTransform[ x ] );
      
         // add pathTransform group to shapeSwitch group
         shapeSwitch.addChild( pathTransform[ x ] );
      }
            
      // create and set scene lighting
      setLighting( sceneBranchGroup, bounds );  
      
      // create scene to display if user loses
      TransformGroup loserTransformGroup = 
         createEndScene( "You Lose!" );
      
      // add loser scene to sceneSwitch
      sceneSwitch.addChild( loserTransformGroup );  
      
      // create scene to display if user winss
      TransformGroup winnerTransformGroup = 
         createEndScene( "You Win!" );
      
      // add winner scene to sceneSwitch
      sceneSwitch.addChild( winnerTransformGroup ); 
      
      // create shiny red Appearance for navigating shape
      Appearance flyingAppearance = createAppearance( 
         new Color3f( 1.0f, 0.0f, 0.0f ) );
      
      // initialize navigable sphere 
      Primitive flyingBall = new Sphere( 
         0.03f,  Sphere.GENERATE_NORMALS, flyingAppearance );
      
      // set capability bits to enable collision detection and 
      // allow for read/write of bounds
      flyingBall.setCollidable( true );
        flyingBall.setCapability( Node.ENABLE_COLLISION_REPORTING );
      flyingBall.setCapability( Node.ALLOW_BOUNDS_READ );
      flyingBall.setCapability( Node.ALLOW_BOUNDS_WRITE );
      
      // create TransformGroup to translate shape position
      TransformGroup startTransform = createTransform( 
         new Vector3f( 0.9f, -0.9f, 0.0f ) );
           
      startTransform.addChild( flyingBall );
      gameBranchGroup.addChild( startTransform );
       
      // create Material for Appearance for target sphere
      Appearance targetAppearance = createAppearance(
         new Color3f( 0.0f, 1.0f, 0.0f ) );
      
      // obtain textured image for target sphere
      String rgb = new String( "RGB" );
      TextureLoader textureLoader = new TextureLoader( 
         Java3DWorld1.class.getResource( imageName ), rgb, this );
      textureLoader.getTexture().setEnable( true );
      targetAppearance.setTexture( textureLoader.getTexture() );
      
      // initialize target sphere
      Primitive targetSphere = new Sphere( SPHERE_RADIUS, 
         Sphere.GENERATE_TEXTURE_COORDS | Sphere.GENERATE_NORMALS, 
         targetAppearance );
      
      // disable collision detection for sphere
      targetSphere.setCollidable( false );
           
      // create vector to target point
      Vector3f target = new Vector3f( -1.0f, 1.0f, -1.0f );
      
      // create TransformGroup that translates sphere position
      TransformGroup targetTransform = createTransform( target );
      targetTransform.addChild( targetSphere );
      gameBranchGroup.addChild( targetTransform );
            
      // create Navigator behavior
      Navigator navigator = new Navigator( startTransform );
      navigator.setSchedulingBounds( bounds );
      
      // create Collide behavior
      Collide collider = new Collide(  
         simpleUniverse, flyingBall, sceneSwitch );
      collider.setSchedulingBounds( bounds );
      
      // create GoalDetector behavior
      GoalDetector goalDetector = new GoalDetector( 
         simpleUniverse, startTransform, sceneSwitch, 
         target, SPHERE_RADIUS );
      goalDetector.setSchedulingBounds( bounds );
      
      // add Behaviors to scene
      sceneBranchGroup.addChild( goalDetector );
      sceneBranchGroup.addChild( collider );
      sceneBranchGroup.addChild( navigator );
      
      // create Background for scene
      Background background = new Background();
      background.setColor( 0.65f, 0.75f, 1.0f );
      background.setApplicationBounds( bounds );
      sceneBranchGroup.addChild( background );
      
      sceneBranchGroup.compile(); 
      
      return sceneBranchGroup;
   
   } // end method createScene
   
   // create Appearance object for Primitive in scene
   private Appearance createAppearance( Color3f diffuseColor )
   {
      Appearance appearance = new Appearance();
      Material material = new Material();
      material.setShininess( MAX_SHININESS );
      material.setDiffuseColor( diffuseColor );
      material.setAmbientColor( 0.0f, 0.0f, 0.0f );
      appearance.setMaterial( material );
      return appearance;
   
   } // end method createAppearance
      
   
   // create TransformGroup for placing an object in scene
   private TransformGroup createTransform( 
      Vector3f positionVector )
   {
      // initialize a TransformGroup and set capability bits
      TransformGroup transformGroup = new TransformGroup();
      transformGroup.setCapability( 
         TransformGroup.ALLOW_TRANSFORM_READ );
      transformGroup.setCapability( 
         TransformGroup.ALLOW_TRANSFORM_WRITE );
      
      // translate starting position to bottom right of scene
      Transform3D location = new Transform3D();
      location.setTranslation( positionVector );
      transformGroup.setTransform( location );
   
      return transformGroup;
      
   } // end method createTransform
   
   // initialize Switch group and set capability bits
   private Switch initializeSwitch( int sceneNumber )
   {
      Switch switchGroup = new Switch( sceneNumber );
      switchGroup.setCollidable( true );
      switchGroup.setCapability( Switch.ALLOW_SWITCH_WRITE );
      switchGroup.setCapability( Switch.ALLOW_SWITCH_READ );
      switchGroup.setCapability( Group.ALLOW_CHILDREN_WRITE );
      switchGroup.setCapability( Group.ALLOW_CHILDREN_READ );
      switchGroup.setCapability( 
         Group.ENABLE_COLLISION_REPORTING );
      return switchGroup;
   
   } // end method initializeSwitch
   
   private TransformGroup[] createTransformGroupArray( 
      int size )
   {
      TransformGroup[] transformGroup = 
         new TransformGroup[ size ];
      
      // set TransformGroup's WRITE and READ permissions
      // and enable collision reporting
      for ( int i = 0; i < size; i++ ) {
         
         // create TransformGroups
         transformGroup[ i ] = new TransformGroup();
        
         // enable collision reporting
         transformGroup[ i ].setCapability(
            Group.ENABLE_COLLISION_REPORTING );
         
         // enable WRITE permission
         transformGroup[ i ].setCapability(
            TransformGroup.ALLOW_TRANSFORM_WRITE );
         
         // enable READ permission
         transformGroup[ i ].setCapability( 
            TransformGroup.ALLOW_TRANSFORM_READ );
      }
      
      return transformGroup;
      
   } // end method createTransformGroupArray

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -