📄 exhenge.java
字号:
groundApp.setTextureAttributes( groundTexAtt );
if ( groundTex != null )
groundApp.setTexture( groundTex );
ElevationGrid ground = new ElevationGrid(
11, // X dimension
11, // Z dimension
2.0f, // X spacing
2.0f, // Z spacing
// Automatically use zero heights
groundApp ); // Appearance
//
// Build the scene using the shapes above. Place everything
// withing a TransformGroup.
//
// Build the scene root
TransformGroup scene = new TransformGroup( );
tr = new Transform3D( );
tr.setTranslation( new Vector3f( 0.0f, -1.6f, 0.0f ) );
scene.setTransform( tr );
// Create influencing bounds
BoundingSphere worldBounds = new BoundingSphere(
new Point3d( 0.0, 0.0, 0.0 ), // Center
1000.0 ); // Extent
// General Ambient light
ambient = new AmbientLight( );
ambient.setEnable( ambientOnOff );
ambient.setColor( new Color3f( 0.3f, 0.3f, 0.3f ) );
ambient.setCapability( AmbientLight.ALLOW_STATE_WRITE );
ambient.setInfluencingBounds( worldBounds );
scene.addChild( ambient );
// Bright Ambient light
brightAmbient = new AmbientLight( );
brightAmbient.setEnable( brightAmbientOnOff );
brightAmbient.setColor( new Color3f( 1.0f, 1.0f, 1.0f ) );
brightAmbient.setCapability( AmbientLight.ALLOW_STATE_WRITE );
brightAmbient.setInfluencingBounds( worldBounds );
scene.addChild( brightAmbient );
// Red directional light
redDirectional = new DirectionalLight( );
redDirectional.setEnable( redDirectionalOnOff );
redDirectional.setColor( new Color3f( 1.0f, 0.0f, 0.0f ) );
redDirectional.setDirection( new Vector3f( 1.0f, -0.5f, -0.5f ) );
redDirectional.setCapability( AmbientLight.ALLOW_STATE_WRITE );
redDirectional.setInfluencingBounds( worldBounds );
scene.addChild( redDirectional );
// Yellow directional light
yellowDirectional = new DirectionalLight( );
yellowDirectional.setEnable( yellowDirectionalOnOff );
yellowDirectional.setColor( new Color3f( 1.0f, 0.8f, 0.0f ) );
yellowDirectional.setDirection( new Vector3f( -1.0f, 0.5f, 1.0f ) );
yellowDirectional.setCapability( AmbientLight.ALLOW_STATE_WRITE );
yellowDirectional.setInfluencingBounds( worldBounds );
scene.addChild( yellowDirectional );
// Orange point light
orangePoint = new PointLight( );
orangePoint.setEnable( orangePointOnOff );
orangePoint.setColor( new Color3f( 1.0f, 0.5f, 0.0f ) );
orangePoint.setPosition( new Point3f( 0.0f, 0.5f, 0.0f ) );
orangePoint.setCapability( AmbientLight.ALLOW_STATE_WRITE );
orangePoint.setInfluencingBounds( worldBounds );
scene.addChild( orangePoint );
// Ground
scene.addChild( ground );
// Dome
scene.addChild( dome );
// Spur 1's
Group g = buildRing( spur1Group );
scene.addChild( g );
// Spur 2's
TransformGroup tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( 0.3927 );
tg.setTransform( tr );
g = buildRing( spur2Group );
tg.addChild( g );
scene.addChild( tg );
// Spur 3's
g = buildRing( spur3Group );
scene.addChild( g );
// Spur 4's
tg = new TransformGroup( );
tg.setTransform( tr );
g = buildRing( spur4Group );
tg.addChild( g );
scene.addChild( tg );
return scene;
}
//
// Build a ring of shapes, each shape contained in a given
// shared group
//
public Group buildRing( SharedGroup sg )
{
Group g = new Group( );
g.addChild( new Link( sg ) ); // 0 degrees
TransformGroup tg = new TransformGroup( );
Transform3D tr = new Transform3D( );
tr.rotY( 0.785 ); // 45 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( -0.785 ); // -45 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( 1.571 ); // 90 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( -1.571 ); // -90 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( 2.356 ); // 135 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( -2.356 ); // -135 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
tg = new TransformGroup( );
tr = new Transform3D( );
tr.rotY( Math.PI ); // 180 degrees
tg.setTransform( tr );
tg.addChild( new Link( sg ) );
g.addChild( tg );
return g;
}
//--------------------------------------------------------------
// USER INTERFACE
//--------------------------------------------------------------
//
// Main
//
public static void main( String[] args )
{
ExHenge ex = new ExHenge( );
ex.initialize( args );
ex.buildUniverse( );
ex.showFrame( );
}
// On/off choices
private boolean ambientOnOff = true;
private boolean brightAmbientOnOff = false;
private boolean redDirectionalOnOff = false;
private boolean yellowDirectionalOnOff = false;
private boolean orangePointOnOff = true;
private CheckboxMenuItem ambientOnOffMenu;
private CheckboxMenuItem brightAmbientOnOffMenu;
private CheckboxMenuItem redDirectionalOnOffMenu;
private CheckboxMenuItem yellowDirectionalOnOffMenu;
private CheckboxMenuItem orangePointOnOffMenu;
//
// Initialize the GUI (application and applet)
//
public void initialize( String[] args )
{
// Initialize the window, menubar, etc.
super.initialize( args );
exampleFrame.setTitle( "Java 3D ExHenge Example" );
//
// Add a menubar menu to change parameters
// Dim ambient light
// Bright ambient light
// Red directional light
// Yellow directional light
// Orange point light
//
Menu m = new Menu( "Lights" );
ambientOnOffMenu = new CheckboxMenuItem(
"Dim ambient light", ambientOnOff );
ambientOnOffMenu.addItemListener( this );
m.add( ambientOnOffMenu );
brightAmbientOnOffMenu = new CheckboxMenuItem(
"Bright ambient light", brightAmbientOnOff );
brightAmbientOnOffMenu.addItemListener( this );
m.add( brightAmbientOnOffMenu );
redDirectionalOnOffMenu = new CheckboxMenuItem(
"Red directional light", redDirectionalOnOff );
redDirectionalOnOffMenu.addItemListener( this );
m.add( redDirectionalOnOffMenu );
yellowDirectionalOnOffMenu = new CheckboxMenuItem(
"Yellow directional light", yellowDirectionalOnOff );
yellowDirectionalOnOffMenu.addItemListener( this );
m.add( yellowDirectionalOnOffMenu );
orangePointOnOffMenu = new CheckboxMenuItem(
"Orange point light", orangePointOnOff );
orangePointOnOffMenu.addItemListener( this );
m.add( orangePointOnOffMenu );
exampleMenuBar.add( m );
}
//
// Handle checkboxes
//
public void itemStateChanged( ItemEvent event )
{
Object src = event.getSource( );
if ( src == ambientOnOffMenu )
{
ambientOnOff = ambientOnOffMenu.getState( );
ambient.setEnable( ambientOnOff );
return;
}
if ( src == brightAmbientOnOffMenu )
{
brightAmbientOnOff = brightAmbientOnOffMenu.getState( );
brightAmbient.setEnable( brightAmbientOnOff );
return;
}
if ( src == redDirectionalOnOffMenu )
{
redDirectionalOnOff = redDirectionalOnOffMenu.getState( );
redDirectional.setEnable( redDirectionalOnOff );
return;
}
if ( src == yellowDirectionalOnOffMenu )
{
yellowDirectionalOnOff = yellowDirectionalOnOffMenu.getState( );
yellowDirectional.setEnable( yellowDirectionalOnOff );
return;
}
if ( src == orangePointOnOffMenu )
{
orangePointOnOff = orangePointOnOffMenu.getState( );
orangePoint.setEnable( orangePointOnOff );
return;
}
// Handle all other checkboxes
super.itemStateChanged( event );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -