?? java3dworld1.java
字號:
// create RotationInterpolators for scene
private void createRotationInterpolators(
TransformGroup[] transformGroup, int size )
{
// declare structures for creating RotationInterpolators
Alpha[] alphaSpin = new Alpha[ size ];
Transform3D[] spinAxis =
new Transform3D[ size ];
RotationInterpolator[] spinner =
new RotationInterpolator[ size ];
// create RotationInterpolator for each shape
for ( int x = 0; x < size; x++ ) {
// initialize Alpha
alphaSpin[ x ] = new Alpha();
// set increasing time for Alpha to random number
alphaSpin[ x ].setIncreasingAlphaDuration(
MIN_ROTATION_SPEED + ( ( int ) ( Math.random() *
MAX_ROTATION_SPEED ) ) );
// initialize RotationInterpolator using appropriate
// Alpha and TransformGroup
spinner[ x ] = new RotationInterpolator(
alphaSpin[ x ], transformGroup[ x ] );
spinAxis[ x ] = new Transform3D();
// set random X-axis rotation
spinAxis[ x ].rotX(
( float ) ( Math.PI * ( Math.random() * 2 ) ) );
spinner[ x ].setAxisOfRotation( spinAxis[ x ] );
// set minimum and maximum rotation angles
spinner[ x ].setMinimumAngle( MIN_ROTATION_ANGLE );
spinner[ x ].setMaximumAngle( MAX_ROTATION_ANGLE );
spinner[ x ].setSchedulingBounds( bounds );
// add RotationInterpolator to appropriate TransformGroup
transformGroup[ x ].addChild( spinner[ x ] );
}
} // end method createRotationInterpolators
// create PositionInterpolators
private void createPositionInterpolators(
TransformGroup[] transformGroup, int size )
{
// create structures for PositionInterpolators
Alpha[] alphaPath = new Alpha[ size ];
PositionInterpolator[] mover =
new PositionInterpolator[ size ];
Transform3D[] pathAxis =
new Transform3D[ size ];
// create PositionInterpolator for each shape
for ( int x = 0; x < size; x++ ) {
// initialize Alpha
alphaPath[ x ] = new Alpha();
// set mode to increase and decrease interpolation
alphaPath[ x ].setMode(
Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE );
// set random phase delay
alphaPath[ x ].setPhaseDelayDuration(
( ( int ) ( Math.random() * MAX_PHASE_DELAY ) ) );
// randomize translation speed
int speed = MIN_TRANSLATION_SPEED +
( int ) ( Math.random() * MAX_TRANSLATION_SPEED );
// set increasing and decreasing durations
alphaPath[ x ].setIncreasingAlphaDuration( speed );
alphaPath[ x ].setDecreasingAlphaDuration( speed );
// randomize translation axis
pathAxis[ x ] = new Transform3D();
pathAxis[ x ].rotX(
( float ) ( Math.PI * ( Math.random() * 2 ) ) );
pathAxis[ x ].rotY(
( float ) ( Math.PI * ( Math.random() * 2 ) ) );
pathAxis[ x ].rotZ(
( float ) ( Math.PI * ( Math.random() * 2 ) ) );
// initialize PositionInterpolator
mover[ x ] = new PositionInterpolator( alphaPath[ x ],
transformGroup[ x ], pathAxis[ x ], 1.0f, -1.0f );
mover[ x ].setSchedulingBounds( bounds );
// add PostionInterpolator to appropriate TransformGroup
transformGroup[ x ].addChild( mover[ x ] );
}
} // end method createPositionInterpolators
// create appearance and material arrays for Primitives
private Appearance[] createAppearance( int size )
{
// create Appearance objects for each shape
Appearance[] appearance =
new Appearance[ size ];
Material[] material = new Material[ size ];
// set material and appearance properties for each shape
for( int i = 0; i < size; i++ ) {
appearance[ i ] = new Appearance();
material[ i ] = new Material();
// set material ambient color
material[ i ].setAmbientColor(
new Color3f( 0.0f, 0.0f, 0.0f ) );
// set material Diffuse color
material[ i ].setDiffuseColor( new Color3f(
( float ) Math.random(), ( float ) Math.random()*0.5f,
( float ) Math.random() ) );
// set Material for appropriate Appearance object
appearance[ i ].setMaterial( material[ i ] );
}
return appearance;
} // end method createAppearance
// create Primitives shapes
private Primitive[] createShapes( Appearance[] appearance,
int size )
{
Primitive[] shapes = new Primitive[ size ];
// random loop to get index
for ( int x = 0; x < size; x++ ) {
// generate random shape index
int index = ( int ) ( Math.random() * NUMBER_OF_PRIMITIVES );
// create shape based on random index
switch( index ) {
case 0: // create Box
shapes[ x ] = new Box(
( ( float ) Math.random() * MAX_LENGTH ),
( ( float ) Math.random() * MAX_LENGTH ),
( ( float ) Math.random() * MAX_LENGTH ),
Box.GENERATE_NORMALS, appearance[ x ] );
break;
case 1: // create Cone
shapes[ x ] = new Cone(
( ( float ) Math.random() * MAX_RADIUS ),
( ( float ) Math.random() * MAX_LENGTH ),
Cone.GENERATE_NORMALS, appearance[ x ] );
break;
case 2: // create Cylinder
shapes[ x ] = new Cylinder(
( ( float ) Math.random() * MAX_RADIUS ),
( ( float ) Math.random() * MAX_LENGTH ),
Cylinder.GENERATE_NORMALS, appearance[ x ] );
break;
case 3: // create Sphere
shapes[ x ] = new Sphere(
( ( float ) Math.random() * MAX_RADIUS ),
Sphere.GENERATE_NORMALS, appearance[ x ] );
break;
} // end switch statement
// set capability bits to enable collisions and to set
// read/write permissions of bounds
shapes[ x ].setCapability(
Node.ENABLE_COLLISION_REPORTING );
shapes[ x ].setCapability(
Node.ALLOW_BOUNDS_READ );
shapes[ x ].setCapability(
Node.ALLOW_BOUNDS_WRITE );
shapes[ x ].setCollidable( true );
}
return shapes;
} // end method createShapes
// initialize ambient and directional lighting
private void setLighting( BranchGroup scene,
BoundingSphere bounds )
{
// initialize ambient lighting
AmbientLight ambientLight = new AmbientLight();
ambientLight.setInfluencingBounds( bounds );
// initialize directional lighting
DirectionalLight directionalLight = new DirectionalLight();
directionalLight.setColor(
new Color3f( 1.0f, 1.0f, 1.0f ) );
directionalLight.setInfluencingBounds( bounds );
// add lights to scene
scene.addChild( ambientLight );
scene.addChild( directionalLight );
} // end method setLighting
// update scene by rendering different shapes in shapeSwitch
public void switchScene( int numberChildren, int size )
{
// create a new BitSet of size NUMBER_OF_SHAPES
BitSet bitSet = new BitSet( size );
// set BitSet values
for ( int i = 0; i < numberChildren; i++ )
bitSet.set( i );
// instruct switchShape to render Mask of objects
shapeSwitch.setWhichChild( Switch.CHILD_MASK );
shapeSwitch.setChildMask( bitSet );
} // end method switchScene
// create end scene when user wins or loses
private TransformGroup createEndScene( String text )
{
TransformGroup transformGroup = new TransformGroup();
transformGroup.setCapability(
TransformGroup.ALLOW_TRANSFORM_WRITE );
// disable scene collision detection
transformGroup.setCollidable( false );
// create Alpha object
Alpha alpha = new Alpha();
alpha.setIncreasingAlphaDuration( MAX_ROTATION_SPEED );
// create RotationInterpolator for scene
RotationInterpolator rotation =
new RotationInterpolator( alpha, transformGroup );
// set axis of rotation
Transform3D axis = new Transform3D();
axis.rotY( ( float ) ( Math.PI / 2.0 ) );
rotation.setAxisOfRotation( axis );
// set minimum and maximum rotation angles
rotation.setMinimumAngle( 0.0f );
rotation.setMaximumAngle( ( float ) ( Math.PI * 8.0 ) );
rotation.setSchedulingBounds( bounds );
transformGroup.addChild( rotation );
// create scene geometry
Appearance appearance = new Appearance();
Material material = new Material();
appearance.setMaterial( material );
// set diffuse color of material
material.setDiffuseColor(
new Color3f( 0.0f, 0.8f, 1.0f ) );
// create Font3D object
Font3D font3d = new Font3D(
new Font( "Helvetica", Font.ITALIC, 1 ),
new FontExtrusion() );
// create Text3D object from Font3D object
Text3D text3d = new Text3D( font3d, text,
new Point3f( -2.0f, 0.0f, 0.0f ) );
// create Shape3D object from Text3D object
Shape3D textShape = new Shape3D( text3d );
textShape.setAppearance( appearance );
// disable collision detection
textShape.setCollidable( false );
transformGroup.addChild( textShape );
return transformGroup;
} // end method createEndScene
// return preferred dimensions of Container
public Dimension getPreferredSize()
{
return new Dimension( CONTAINER_WIDTH, CONTAINER_HEIGHT );
}
// return minimum size of Container
public Dimension getMinimumSize()
{
return getPreferredSize();
}
}
/***************************************************************
* (C) Copyright 2002 by Deitel & Associates, Inc. and *
* Prentice Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have *
* used their best efforts in preparing the book. These *
* efforts include the development, research, and testing of *
* the theories and programs to determine their effectiveness. *
* The authors and publisher make no warranty of any kind, *
* expressed or implied, with regard to these programs or to *
* the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for *
* incidental or consequential damages in connection with, or *
* arising out of, the furnishing, performance, or use of *
* these programs. *
***************************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -