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

📄 swingtest.java

📁 《Java 3D编程》的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		objRoot.addChild( aLgt );		objRoot.addChild( lgt1 );		// wire the scenegraph together		objTrans.addChild( sceneBranchGroup );		objRoot.addChild( objTrans );		// return the root of the scene side of the scenegraph		return objRoot;	}	/**	*	Create a BranchGroup that contains a Cube. The user data	*	for the BranchGroup is set so the BranchGroup can be	*	identified.	*/	protected BranchGroup createCube( )	{		BranchGroup bg = new BranchGroup( );		bg.setCapability( BranchGroup.ALLOW_DETACH );		bg.addChild( new com.sun.j3d.utils.geometry.ColorCube( ) );		bg.setUserData( "Cube" );		return bg;	}	/**	*	Create a BranchGroup that contains a Sphere. The user data	*	for the BranchGroup is set so the BranchGroup can be	*	identified. 	*/	protected BranchGroup createSphere( )	{		BranchGroup bg = new BranchGroup( );		bg.setCapability( BranchGroup.ALLOW_DETACH );		Appearance app = new Appearance( );		Color3f objColor = new Color3f( 1.0f, 0.7f, 0.8f );		Color3f black = new Color3f( 0.0f, 0.0f, 0.0f );		app.setMaterial( new Material( objColor, black, objColor, black, 80.0f ) );		bg.addChild( new com.sun.j3d.utils.geometry.Sphere( 1, app ) );		bg.setUserData( "Sphere" );		return bg;	}	/**	*	Removes a BranchGroup from the scene based on user data	*	@param name the user data to look for	*/	protected void removeShape( String name )	{		try		{			java.util.Enumeration enum = sceneBranchGroup.getAllChildren( );			int index = 0;			while ( enum.hasMoreElements( ) != false )			{				SceneGraphObject sgObject = (SceneGraphObject) enum.nextElement( );				Object userData = sgObject.getUserData( );				if ( userData instanceof String && ((String) userData).compareTo( name ) == 0 )				{					System.out.println( "Removing: " + sgObject.getUserData( ) );							sceneBranchGroup.removeChild( index );				}				index++;			}		}		catch( Exception e )		{			// the scenegraph may not have yet been synchronized...		}	}	/**	*	Creates the PhysicalBody for the View	*/	protected PhysicalBody createPhysicalBody( )	{		return new PhysicalBody( );	}	/**	*	Creates the PhysicalEnvironment for the View	*/	protected PhysicalEnvironment createPhysicalEnvironment( )	{		return new PhysicalEnvironment( );	}	/**	*	Returns the View Platform Activation Radius	*/	protected float getViewPlatformActivationRadius( )	{		return 100;	}	/**	*	Creates the View Platform for the View	*/	protected ViewPlatform createViewPlatform( )	{		ViewPlatform vp = new ViewPlatform( );		vp.setViewAttachPolicy( View.RELATIVE_TO_FIELD_OF_VIEW );		vp.setActivationRadius( getViewPlatformActivationRadius( ) );		return vp;	}	/**	*	Returns the distance to the rear clipping plane.	*/	protected double getBackClipDistance( )	{		return 100.0;	}	/**	*	Returns the distance to the near clipping plane.	*/	protected double getFrontClipDistance( )	{		return 1.0;	}	/**	*	Creates the View side BranchGroup. The ViewPlatform is	*	wired in beneath the TransformGroups.	*/	protected BranchGroup createViewBranchGroup( TransformGroup[] tgArray, ViewPlatform vp )	{		BranchGroup vpBranchGroup = new BranchGroup( );		if( tgArray != null && tgArray.length > 0 )		{			Group parentGroup = vpBranchGroup;			TransformGroup curTg = null;			for( int n = 0; n < tgArray.length; n++ )			{				curTg = tgArray[n];				parentGroup.addChild( curTg );				parentGroup = curTg;			}			tgArray[tgArray.length-1].addChild( vp );		}		else			vpBranchGroup.addChild( vp );		return vpBranchGroup;	}	/**	*	Creates the VirtualUniverse for the application.	*/	protected VirtualUniverse createVirtualUniverse( )	{				return new VirtualUniverse( );	}	/**	*	Called to render the scene into the offscreen	*	Canvas3D and save the image (as a JPEG) to disk.	*/	protected void onSaveImage( )	{		offScreenCanvas3D.renderOffScreenBuffer( );		offScreenCanvas3D.waitForOffScreenRendering( );		System.out.println( "Rendered to offscreen" );		try		{			FileOutputStream fileOut = new FileOutputStream( "image.jpg" );			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder( fileOut );			encoder.encode( imageComponent.getImage( ) );			fileOut.flush( );			fileOut.close( );		}		catch( Exception e )		{			System.err.println( "Failed to save image: " + e );			}		System.out.println( "Saved image." );	}	/**	*	AWT callback to indicate that an items has been selected 	*	from a menu.	*/	public void actionPerformed( ActionEvent ae )	{		System.out.println( "Action Performed: " + ae.getActionCommand( ) );		java.util.StringTokenizer toker = new java.util.StringTokenizer( ae.getActionCommand( ), "|" );		String menu = toker.nextToken( );		String command = toker.nextToken( );		if ( menu.equals( "File" ) )		{			if ( command.equals( "Exit" ) )			{				System.exit( 0 );			}			else if ( command.equals( "Save Image" )  )			{				onSaveImage( );			}		}		else if ( menu.equals( "View" ) )		{			if ( command.equals( "Cube" ) )			{				removeShape( "Sphere" );				sceneBranchGroup.addChild( createCube( ) );			}			else if ( command.equals( "Sphere" ) )			{				removeShape( "Cube" );				sceneBranchGroup.addChild( createSphere( ) );			}		}		else if ( menu.equals( "Rotate" ) )		{			if ( command.equals( "On" ) )			{				rotator.setEnable( true );			}			else if ( command.equals( "Off" ) )			{				rotator.setEnable( false );			}		}	}	/**	*	Helper method to creates a Swing JMenuItem.	*/	private JMenuItem createMenuItem( String menuText, String buttonText, ActionListener listener )	{		JMenuItem menuItem = new JMenuItem( buttonText );		menuItem.addActionListener( listener );		menuItem.setActionCommand( menuText + "|" + buttonText );		return menuItem;	}	/*	*	Registers a window listener to handle ALT+F4 window closing.	*	*	@param frame the JFrame for which  we want to intercept close	*	messages.	*/	static protected void registerWindowListener( JFrame frame )	{		// disable automatic close support for Swing frame.		frame.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );		// adds the window listener		frame.addWindowListener( new WindowAdapter( )			{			// handles the system exit window message			public void windowClosing( WindowEvent e )			{				System.exit( 1 );			}		}		);	}	public JMenuBar createMenuBar( )	{		JMenuBar menuBar = new JMenuBar( );		JMenu menu = null;		menu = new JMenu( "File" );		menu.add( createMenuItem( "File", "Save Image", this ) );		menu.add( createMenuItem( "File", "Exit", this ) );		menuBar.add( menu );		menu = new JMenu( "View" );		menu.add( createMenuItem( "View", "Cube", this ) );		menu.add( createMenuItem( "View", "Sphere", this ) );		menuBar.add( menu );		menu = new JMenu( "Rotate" );		menu.add( createMenuItem( "Rotate", "On", this ) );		menu.add( createMenuItem( "Rotate", "Off", this ) );		menuBar.add( menu );		return menuBar;		}	/**	*	main entry point for the application. Creates the parent	*	JFrame, the JMenuBar and creates the JPanel which is the	*	application itself.	*/	public static void main( String[] args )	{		JPopupMenu.setDefaultLightWeightPopupEnabled( false );		ToolTipManager ttm = ToolTipManager.sharedInstance( );		ttm.setLightWeightPopupEnabled( false );		JFrame frame = new JFrame( );		SwingTest swingTest = new SwingTest( );						frame.setJMenuBar( swingTest.createMenuBar( ) );		frame.getContentPane( ).add( swingTest );		frame.setSize( 550, 550 );		registerWindowListener( frame );		frame.setVisible( true );	}}

⌨️ 快捷键说明

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