📄 example.java
字号:
// SET/GET METHODS
//--------------------------------------------------------------
/**
* Sets the headlight on/off state. The headlight faces forward
* in the direction the viewer is facing. Example applications
* that add their own lights will typically turn the headlight off.
* A standard menu item enables the headlight to be turned on and
* off via user control.
*
* @param onOff a boolean turning the light on (true) or off (false)
*/
public void setHeadlightEnable( boolean onOff )
{
headlightOnOff = onOff;
if ( headlight != null )
headlight.setEnable( headlightOnOff );
if ( headlightMenuItem != null )
headlightMenuItem.setState( headlightOnOff );
}
/**
* Gets the headlight on/off state.
*
* @return a boolean indicating if the headlight is on or off
*/
public boolean getHeadlightEnable( )
{
return headlightOnOff;
}
/**
* Sets the navigation type to be either Examine or Walk. The
* Examine navigation type sets up behaviors that use mouse drags
* to rotate and translate scene content as if it is an object
* held at arm's length and under examination. The Walk navigation
* type uses mouse drags to rotate and translate the viewer as if
* they are walking through the content. The Examine type is the
* default.
*
* @param nav either Walk or Examine
*/
public void setNavigationType( int nav )
{
if ( nav == Walk )
{
navigationType = Walk;
if ( walkMenuItem != null )
walkMenuItem.setState( true );
if ( examineMenuItem != null )
examineMenuItem.setState( false );
if ( walkBehavior != null )
walkBehavior.setEnable( true );
if ( examineBehavior != null )
examineBehavior.setEnable( false );
}
else
{
navigationType = Examine;
if ( walkMenuItem != null )
walkMenuItem.setState( false );
if ( examineMenuItem != null )
examineMenuItem.setState( true );
if ( walkBehavior != null )
walkBehavior.setEnable( false );
if ( examineBehavior != null )
examineBehavior.setEnable( true );
}
}
/**
* Gets the current navigation type, returning either Walk or
* Examine.
*
* @return either Walk or Examine
*/
public int getNavigationType( )
{
return navigationType;
}
/**
* Sets whether the scene graph should be compiled or not.
* Normally this is always a good idea. For some example
* applications that use this Example framework, it is useful
* to disable compilation - particularly when nodes and node
* components will need to be made un-live in order to make
* changes. Once compiled, such components can be made un-live,
* but they are still unchangable unless appropriate capabilities
* have been set.
*
* @param onOff a boolean turning compilation on (true) or off (false)
*/
public void setCompilable( boolean onOff )
{
shouldCompile = onOff;
}
/**
* Gets whether the scene graph will be compiled or not.
*
* @return a boolean indicating if scene graph compilation is on or off
*/
public boolean getCompilable( )
{
return shouldCompile;
}
///////////
public void setMove()
{
}
// These methods will be replaced
// Set the view position and direction
public void setViewpoint( Point3f position, Vector3f direction )
{
Transform3D t = new Transform3D( );
t.set( new Vector3f( position ) );
exampleViewTransform.setTransform( t );
// how to set direction?
}
// Reset transforms
public void reset( )
{
Transform3D trans = new Transform3D( );
exampleSceneTransform.setTransform( trans );
trans.set( new Vector3f( 0.0f, 0.0f, 10.0f ) );
exampleViewTransform.setTransform( trans );
setNavigationType( navigationType );
}
//
// Gets the URL (with file: prepended) for the current directory.
// This is a terrible hack needed in the Alpha release of Java3D
// in order to build a full path URL for loading sounds with
// MediaContainer. When MediaContainer is fully implemented,
// it should handle relative path names, but not yet.
//
public String getCurrentDirectory( )
{
// Create a bogus file so that we can query it's path
File dummy = new File( "dummy.tmp" );
String dummyPath = dummy.getAbsolutePath();
// strip "/dummy.tmp" from end of dummyPath and put into 'path'
if (dummyPath.endsWith( File.separator + "dummy.tmp" ))
{
int index = dummyPath.lastIndexOf(
File.separator + "dummy.tmp" );
if ( index >= 0 )
{
int pathLength = index + 5; // pre-pend 'file:'
char[] charPath = new char[pathLength];
dummyPath.getChars(0, index, charPath, 5);
String path = new String(charPath, 0, pathLength);
path = "file:" + path.substring(5, pathLength);
return path + File.separator;
}
}
return dummyPath + File.separator;
}
//--------------------------------------------------------------
// USER INTERFACE
//--------------------------------------------------------------
/**
* Builds the example AWT Frame menubar. Standard menus and
* their options are added. Applications that subclass this
* class should build their menubar additions within their
* initialize method.
*
* @return a MenuBar for the AWT Frame
*/
private MenuBar buildMenuBar( )
{
// Build the menubar
MenuBar menuBar = new MenuBar( );
// File menu
Menu m = new Menu( "文件" );
m.addActionListener( this );
m.add( "退出" );
menuBar.add( m );
// View menu
m = new Menu( "视图" );
m.addActionListener( this );
m.add( "重设视图" );
m.addSeparator( );
walkMenuItem =
new CheckboxMenuItem( "行走模式" );
walkMenuItem.addItemListener( this );
m.add( walkMenuItem );
examineMenuItem =
new CheckboxMenuItem( "视检模式" );
examineMenuItem.addItemListener( this );
m.add( examineMenuItem );
if ( navigationType == Walk )
{
walkMenuItem.setState( true );
examineMenuItem.setState( false );
}
else
{
walkMenuItem.setState( false );
examineMenuItem.setState( true );
}
m.addSeparator( );
headlightMenuItem =
new CheckboxMenuItem( "头灯 开/关" );
headlightMenuItem.addItemListener( this );
headlightMenuItem.setState( headlightOnOff );
m.add( headlightMenuItem );
/////
/* m.addSeparator( );
moveMenuItem=
new CheckboxMenuItem("移动 开/关");
moveMenuItem.addItemListener(this);
moveMenuItem.setState( moveOnOff );
m.add(moveMenuItem);
*/
menuBar.add( m );
return menuBar;
}
/**
* Shows the application's frame, making it and its menubar,
* 3D canvas, and 3D content visible.
*/
public void showFrame( )
{
exampleFrame.show( );
}
/**
* Quits the application.
*/
public void quit( )
{
System.exit( 0 );
}
/**
* Handles menu selections.
*
* @param event an ActionEvent indicating what menu action
* requires handling
*/
public void actionPerformed( ActionEvent event )
{
String arg = event.getActionCommand( );
if ( arg.equals( "重设视图" ) )
reset( );
else if ( arg.equals( "退出" ) )
quit( );
}
/**
* Handles checkbox items on a CheckboxMenu. The Example class has
* none of its own, but subclasses may have some.
*
* @param menu which CheckboxMenu needs action
* @param check which CheckboxMenu item has changed
*/
public void checkboxChanged( CheckboxMenu menu, int check )
{
// None for us
}
/**
* Handles on/off checkbox items on a standard menu.
*
* @param event an ItemEvent indicating what requires handling
*/
public void itemStateChanged( ItemEvent event )
{
Object src = event.getSource( );
boolean state;
if ( src == headlightMenuItem )
{
state = headlightMenuItem.getState( );
headlight.setEnable( state );
}
else if ( src == walkMenuItem )
setNavigationType( Walk );
else if ( src == examineMenuItem )
setNavigationType( Examine );
/////
/* else if (src==moveMenuItem)
{ setNavigationType(Move);
state=moveMenuItem.getState();
}*/
}
/**
* Handles a window closing event notifying the application
* that the user has chosen to close the application without
* selecting the "Exit" menu item.
*
* @param event a WindowEvent indicating the window is closing
*/
public void windowClosing( WindowEvent event )
{
quit( );
}
public void windowClosed( WindowEvent event )
{
}
public void windowOpened( WindowEvent event )
{
}
public void windowIconified( WindowEvent event )
{
}
public void windowDeiconified( WindowEvent event )
{
}
public void windowActivated( WindowEvent event )
{
}
public void windowDeactivated( WindowEvent event )
{
}
// Well known colors, positions, and directions
public final static Color3f White = new Color3f( 1.0f, 1.0f, 1.0f );
public final static Color3f Gray = new Color3f( 0.7f, 0.7f, 0.7f );
public final static Color3f DarkGray = new Color3f( 0.2f, 0.2f, 0.2f );
public final static Color3f Black = new Color3f( 0.0f, 0.0f, 0.0f );
public final static Color3f Red = new Color3f( 1.0f, 0.0f, 0.0f );
public final static Color3f DarkRed = new Color3f( 0.3f, 0.0f, 0.0f );
public final static Color3f Yellow = new Color3f( 1.0f, 1.0f, 0.0f );
public final static Color3f DarkYellow=new Color3f( 0.3f, 0.3f, 0.0f );
public final static Color3f Green = new Color3f( 0.0f, 1.0f, 0.0f );
public final static Color3f DarkGreen= new Color3f( 0.0f, 0.3f, 0.0f );
public final static Color3f Cyan = new Color3f( 0.0f, 1.0f, 1.0f );
public final static Color3f Blue = new Color3f( 0.0f, 0.0f, 1.0f );
public final static Color3f DarkBlue = new Color3f( 0.0f, 0.0f, 0.3f );
public final static Color3f Magenta = new Color3f( 1.0f, 0.0f, 1.0f );
public final static Vector3f PosX = new Vector3f( 1.0f, 0.0f, 0.0f );
public final static Vector3f NegX = new Vector3f( -1.0f, 0.0f, 0.0f );
public final static Vector3f PosY = new Vector3f( 0.0f, 1.0f, 0.0f );
public final static Vector3f NegY = new Vector3f( 0.0f, -1.0f, 0.0f );
public final static Vector3f PosZ = new Vector3f( 0.0f, 0.0f, 1.0f );
public final static Vector3f NegZ = new Vector3f( 0.0f, 0.0f, -1.0f );
public final static Point3f Origin = new Point3f( 0.0f, 0.0f, 0.0f );
public final static Point3f PlusX = new Point3f( 0.75f, 0.0f, 0.0f );
public final static Point3f MinusX = new Point3f( -0.75f, 0.0f, 0.0f );
public final static Point3f PlusY = new Point3f( 0.0f, 0.75f, 0.0f );
public final static Point3f MinusY = new Point3f( 0.0f, -0.75f, 0.0f );
public final static Point3f PlusZ = new Point3f( 0.0f, 0.0f, 0.75f);
public final static Point3f MinusZ = new Point3f( 0.0f, 0.0f, -0.75f);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -