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

📄 simplesound.java

📁 包括了JAVA3D(全世界都能看到的网络三维动画)的源代码部分! 很多基础但是却很精彩的例子!有什么不明白的也可以和我交流MSN:guorui0728@hotmail.com
💻 JAVA
字号:
//SimpleSound.java
//演示如何使用声音
import java.applet.Applet;
import java.awt.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.mouse.*;

public class SimpleSound extends Applet {
	
	public BranchGroup createSceneGraph() {
		BranchGroup sceneRoot = new BranchGroup();
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

		Color3f bgColor = new Color3f(0.0f, 0.0f, 0.0f);
		Background bg = new Background(bgColor);
		bg.setApplicationBounds(bounds);
		sceneRoot.addChild(bg);

		TransformGroup trans = new TransformGroup();
		trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		sceneRoot.addChild(trans);
		
		trans.addChild(new ColorCube(0.8));
		
		MediaContainer media  = new MediaContainer();
//      media.setURLString("file:./water.wav");
//		media.setURLString("file:./water.au");
		
		media.setURLString("file:./water.aif");

/*		BackgroundSound myBS = new BackgroundSound();
		myBS.setLoop( -1 );
		myBS.setInitialGain( 0.7f );
		myBS.setSchedulingBounds(bounds);
		myBS.setSoundData( media );
		myBS.setEnable( true ); 
		trans.addChild( myBS );
*/
		float distance1[] = {0.4f, 6.f, 10.0f};
		float gain1[] = {1.0f, 1.f, 0.0f};
		float distance2[] = {0.1f, 0.5f, 1.f};
		float gain2[] = {1.0f, 0.5f, 0.0f};
		Point3f[]	attenuation =new Point3f[4];
		attenuation[0]=new Point3f(0.12f,0.8f,ConeSound.NO_FILTER);
		attenuation[1]=new Point3f(0.26f,0.6f,18000.0f);
		attenuation[2]=new Point3f(0.32f,0.4f,15000.0f);
		attenuation[3]=new Point3f(0.40f,0.2f,11000.0f);
		
		
/*		PointSound myPS = new PointSound();
		myPS.setDistanceGain(distance1, gain1);
		myPS.setSchedulingBounds(bounds);
		myPS.setSoundData( media );
		myPS.setEnable( true ); 
		myPS.setLoop( -1 );  
		trans.addChild( myPS );
*/
	    ConeSound myCS = new ConeSound();
		myCS.setDistanceGain(distance1,gain1,distance2,gain2);
		myCS.setAngularAttenuation(attenuation);
		myCS.setSchedulingBounds(bounds);
		myCS.setSoundData( media );
		myCS.setDirection(new Vector3f(0.0f, 0.0f, 1.0f));
		myCS.setEnable( true ); 
		myCS.setLoop( -1 );  
		trans.addChild( myCS );

		// Create a new Behavior object that will perform the desired
		// operation on the specified transform object and add it into the
		// scene graph.
		
		MouseRotate myMR = new MouseRotate();
		myMR.setTransformGroup(trans);
		myMR.setSchedulingBounds(bounds);
		sceneRoot.addChild(myMR);

		MouseZoom myMZ = new MouseZoom();
		myMZ.setTransformGroup(trans);
		myMZ.setSchedulingBounds(bounds);
		sceneRoot.addChild(myMZ);

		MouseTranslate myMT = new MouseTranslate();
		myMT.setTransformGroup(trans);
		myMT.setSchedulingBounds(bounds);
		sceneRoot.addChild(myMT);
		
		sceneRoot.compile();
		return sceneRoot;
	}

	public SimpleSound() {
		setLayout(new BorderLayout());
		GraphicsConfiguration config =
									  SimpleUniverse.getPreferredConfiguration();
		Canvas3D canvas = new Canvas3D(config);
		add("Center", canvas);
		Viewer viewer = new Viewer(canvas);
		Vector3d viewpoint = new Vector3d(0.0, 0.0, 10.0);  //初始观察点位置
		Transform3D t = new Transform3D();
		t.set(viewpoint);
		ViewingPlatform v = new ViewingPlatform( );
		v.getViewPlatformTransform().setTransform(t);

		BranchGroup scene = createSceneGraph();
		SimpleUniverse su = new SimpleUniverse(v, viewer);
		su.getViewingPlatform();
		su.addBranchGraph(scene);
		AudioDevice audioDev = su.getViewer().createAudioDevice();
	}
	
	public static void main(String[] args) {
		new MainFrame(new SimpleSound(), 256, 256);
	}
}

⌨️ 快捷键说明

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