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

📄 exsound.java

📁 我刚刚在学习java3d
💻 JAVA
📖 第 1 页 / 共 2 页
字号:



	private int[] coordinateIndices = {
		0, 1, 5, 4,  // front
		1, 2, 6, 5,  // right
		2, 3, 7, 6,  // back
		3, 0, 4, 7,  // left
		4, 5, 6, 7,  // top
		3, 2, 1, 0,  // bottom
	};

	private float[] textureCoordinates = {
		0.0f, 0.0f,
		1.0f, 0.0f,
		1.0f, 1.0f,
		0.0f, 1.0f,
	};

	private int[] textureCoordinateIndices = {
		0, 1, 2, 3,
		0, 1, 2, 3,
		0, 1, 2, 3,
		0, 1, 2, 3,
		0, 1, 2, 3,
		0, 1, 2, 3,
	};

	private float[] normals = {
		0.0f, 0.0f, 1.0f,  // front
		1.0f, 0.0f, 0.0f,  // right
		0.0f, 0.0f, -1.0f, // back
		-1.0f, 0.0f, 0.0f, // left
		0.0f, 1.0f, 0.0f,  // top
		0.0f, -1.0f, 0.0f, // bottom
	};

	private int[] normalIndices = {
		0, 0, 0, 0,
		1, 1, 1, 1,
		2, 2, 2, 2,
		3, 3, 3, 3,
		4, 4, 4, 4,
		5, 5, 5, 5,
	};

	private Shape3D buildBox( float width, float height, float depth,
		Appearance app )
	{
		float w2 = width/2.0f;
		float h2 = height/2.0f;
		float d2 = depth/2.0f;

		float[] coordinates = new float[8*3];
		int n = 0;
		// Around the bottom of the box
		coordinates[n+0] = -w2;
		coordinates[n+1] = -h2;
		coordinates[n+2] = d2;
		n += 3;
		coordinates[n+0] = w2;
		coordinates[n+1] = -h2;
		coordinates[n+2] = d2;
		n += 3;
		coordinates[n+0] = w2;
		coordinates[n+1] = -h2;
		coordinates[n+2] = -d2;
		n += 3;
		coordinates[n+0] = -w2;
		coordinates[n+1] = -h2;
		coordinates[n+2] = -d2;
		n += 3;

		// Around the top of the box
		coordinates[n+0] = -w2;
		coordinates[n+1] = h2;
		coordinates[n+2] = d2;
		n += 3;
		coordinates[n+0] = w2;
		coordinates[n+1] = h2;
		coordinates[n+2] = d2;
		n += 3;
		coordinates[n+0] = w2;
		coordinates[n+1] = h2;
		coordinates[n+2] = -d2;
		n += 3;
		coordinates[n+0] = -w2;
		coordinates[n+1] = h2;
		coordinates[n+2] = -d2;
		n += 3;

		IndexedQuadArray quads = new IndexedQuadArray(
			coordinates.length,  // vertex count
			GeometryArray.COORDINATES |
			GeometryArray.NORMALS |
			GeometryArray.TEXTURE_COORDINATE_2,
			coordinateIndices.length );
		quads.setCoordinates( 0, coordinates );
		quads.setCoordinateIndices( 0, coordinateIndices );
		quads.setNormals( 0, normals );
		quads.setNormalIndices( 0, normalIndices );
		quads.setTextureCoordinates( 0, textureCoordinates );
		quads.setTextureCoordinateIndices( 0, textureCoordinateIndices);

		Shape3D shape = new Shape3D( quads, app );
		return shape;
	}


	private Group buildTumblingBox( float width, float height,
		float depth, Appearance app,
		int xDur, int yDur, int zDur )
	{
		BoundingSphere worldBounds = new BoundingSphere(
			new Point3d( 0.0, 0.0, 0.0 ),  // Center
			1000.0 );                      // Extent

		//  Build a box to tumble
		Shape3D box = buildBox( width, height, depth, app );

		//  Build a set of nested transform groups.  Attach
		//  to each one a behavior that rotates around an X,
		//  Y, or Z axis.  Use different rotation speeds for
		//  each axis to create a tumbling effect.
		TransformGroup outerGroup = new TransformGroup( );
		outerGroup.setCapability(
			TransformGroup.ALLOW_TRANSFORM_WRITE );
		Transform3D yAxis = new Transform3D();
		Alpha alpha = new Alpha(
			-1,            // loop count:  -1 = forever
			Alpha.INCREASING_ENABLE,  // increasing
			0,             // trigger time:  0 = now
			0,             // delay:  0 = none
			xDur,          // increasing duration
			0,             // increasing ramp duration
			0,             // at one (sustain) duration
			0,             // decreasing duration
			0,             // decreasing ramp duration
			0 );           // at zero duration
		RotationInterpolator rot = new RotationInterpolator(
			alpha,         // Alpha control
			outerGroup,    // Target transform group
			yAxis,         // Y axis rotation
			0.0f,          // Minimum angle
			2.0f*(float)Math.PI );// Maximum angle
		rot.setSchedulingBounds( worldBounds );
		outerGroup.addChild( rot );

		TransformGroup middleGroup = new TransformGroup( );
		middleGroup.setCapability(
			TransformGroup.ALLOW_TRANSFORM_WRITE );
		Transform3D xAxis = new Transform3D();
		xAxis.rotZ( -1.571f );
		alpha = new Alpha(
			-1,            // loop count:  -1 = forever
			Alpha.INCREASING_ENABLE,  // increasing
			0,             // trigger time:  0 = now
			0,             // delay:  0 = none
			yDur,          // increasing duration
			0,             // increasing ramp duration
			0,             // at one (sustain) duration
			0,             // decreasing duration
			0,             // decreasing ramp duration
			0 );           // at zero duration
		rot = new RotationInterpolator(
			alpha,         // Alpha control
			middleGroup,   // Target transform group
			xAxis,         // Y axis rotation
			0.0f,          // Minimum angle
			2.0f*(float)Math.PI );// Maximum angle
		rot.setSchedulingBounds( worldBounds );
		middleGroup.addChild( rot );
		outerGroup.addChild( middleGroup );

		TransformGroup innerGroup = new TransformGroup( );
		innerGroup.setCapability(
			TransformGroup.ALLOW_TRANSFORM_WRITE );
		Transform3D zAxis = new Transform3D();
		zAxis.rotX( 1.571f );
		alpha = new Alpha(
			-1,            // loop count:  -1 = forever
			Alpha.INCREASING_ENABLE,  // increasing
			0,             // trigger time:  0 = now
			0,             // delay:  0 = none
			zDur,          // increasing duration
			0,             // increasing ramp duration
			0,             // at one (sustain) duration
			0,             // decreasing duration
			0,             // decreasing ramp duration
			0 );           // at zero duration
		rot = new RotationInterpolator(
			alpha,         // Alpha control
			innerGroup,    // Target transform group
			zAxis,         // Y axis rotation
			0.0f,          // Minimum angle
			2.0f*(float)Math.PI );// Maximum angle
		rot.setSchedulingBounds( worldBounds );
		innerGroup.addChild( rot );
		middleGroup.addChild( innerGroup );

		innerGroup.addChild( box );
		return outerGroup;
	}



	//--------------------------------------------------------------
	//  USER INTERFACE
	//--------------------------------------------------------------

	//
	//  Main
	//
	public static void main( String[] args )
	{
		ExSound ex = new ExSound( );
		ex.initialize( args );
		ex.buildUniverse( );
		ex.showFrame( );
	}


	//  On/off choices
	private boolean backgroundSoundOnOff = false;
	private CheckboxMenuItem backgroundSoundOnOffMenu = null;

	private boolean pointSoundOnOff = false;
	private CheckboxMenuItem pointSoundOnOffMenu = null;

	//  Volume menu choices
	private NameValue[] volumes = {
		new NameValue( "Silent",        new Float( 0.0f ) ),
		new NameValue( "Low volume",    new Float( 0.5f ) ),
		new NameValue( "Medium volume", new Float( 1.0f ) ),
		new NameValue( "High volume",   new Float( 2.0f ) ),
	};
	private int currentVolume = 2;
	private CheckboxMenu volumeMenu = null;


	//
	//  Initialize the GUI (application and applet)
	//
	public void initialize( String[] args )
	{
		// Initialize the window, menubar, etc.
		super.initialize( args );
		exampleFrame.setTitle( "Java 3D Sound Example" );


		//
		//  Add a menubar menu to change node parameters
		//    Background sound on/off
		//    Point sound on/off
		//

		Menu m = new Menu( "Sounds" );

		backgroundSoundOnOffMenu = new CheckboxMenuItem( "Background sound on/off",
			backgroundSoundOnOff );
		backgroundSoundOnOffMenu.addItemListener( this );
		m.add( backgroundSoundOnOffMenu );

		pointSoundOnOffMenu = new CheckboxMenuItem( "Point sound on/off",
			pointSoundOnOff );
		pointSoundOnOffMenu.addItemListener( this );
		m.add( pointSoundOnOffMenu );

		volumeMenu = new CheckboxMenu( "Volume", volumes,
			currentVolume, this );
		m.add( volumeMenu );

		exampleMenuBar.add( m );
	}


	//
	//  Handle checkboxes and menu choices
	//
	public void checkboxChanged( CheckboxMenu menu, int check )
	{
		if ( menu == volumeMenu )
		{
			// Change the sound volumes
			currentVolume = check;
			float vol = ((Float)volumes[check].value).floatValue( );
			backgroundSound.setInitialGain( vol );
			pointSound.setInitialGain( vol );
			return;
		}

		// Handle all other checkboxes
		super.checkboxChanged( menu, check );
	}

	public void itemStateChanged( ItemEvent event )
	{
		Object src = event.getSource( );
		if ( src == backgroundSoundOnOffMenu )
		{
			// Turn the background sound on or off
			backgroundSoundOnOff = backgroundSoundOnOffMenu.getState( );
			backgroundSound.setEnable( backgroundSoundOnOff );
			ambientLight.setEnable( backgroundSoundOnOff );
			return;
		}
		if ( src == pointSoundOnOffMenu )
		{
			// Turn the point sound on or off
			pointSoundOnOff = pointSoundOnOffMenu.getState( );
			pointSound.setEnable( pointSoundOnOff );
			pointLight.setEnable( pointSoundOnOff );

			return;
		}

		// Handle all other checkboxes
		super.itemStateChanged( event );
	}
}

⌨️ 快捷键说明

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