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

📄 simplebackground.java

📁 包括了JAVA3D(全世界都能看到的网络三维动画)的源代码部分! 很多基础但是却很精彩的例子!有什么不明白的也可以和我交流MSN:guorui0728@hotmail.com
💻 JAVA
字号:
//SimpleBackground.java//演示使用背景对象import  java.io.RandomAccessFile;import  java.io.IOException;import  java.applet.Applet;import  java.awt.*;import  java.util.Hashtable;import  java.util.Properties;import  javax.media.j3d.*;import  javax.vecmath.*;import	com.sun.j3d.utils.applet.MainFrame; import	com.sun.j3d.utils.image.TextureLoader;import	com.sun.j3d.utils.universe.*;import  com.sun.j3d.utils.behaviors.mouse.MouseRotate;import	com.sun.j3d.utils.behaviors.keyboard.*;public class SimpleBackground extends Applet{	Canvas3D            canvas          = null;	SimpleUniverse		su				= null;	Locale              locale          = null;	PhysicalBody        physicalBody    = null;	PhysicalEnvironment physicalEnv     = null;	View                view            = null;	ViewPlatform        vp              = null;	TransformGroup      vpTrans         = null;	BranchGroup         sceneRoot       = null;	float				scalingFactor	= 0.005f;	String				filename		="747.3ds";	//	String				bgfile			="bg.jpg";		private BranchGroup createScene(){		//创建根分支节点		sceneRoot   = new BranchGroup();		//创建场景范围		BoundingSphere  bounds  =								 new BoundingSphere( new Point3d( 0.0, 0.0, 0.0 ), 100.0 );		//创建背景		Background      bg      = new Background();		//指定背景颜色		bg.setColor(new Color3f(0.1f,0.4f,0.7f));		//指定背景的有效范围		bg.setApplicationBounds( bounds );		sceneRoot.addChild( bg );		/*		TextureLoader myLoader = new TextureLoader( "bg.jpg", this );		ImageComponent2D myImage = myLoader.getImage( );		bg.setImage( myImage );*/		bg.setColor (new Color3f(0.0f,0.0f,0.0f));		bg.setGeometry (createBackGraph());		//添加环境灯		Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);		AmbientLight ambientLight = new AmbientLight(ambientColor);		ambientLight.setInfluencingBounds(bounds);		sceneRoot.addChild(ambientLight);		//添加白色平行光		Color3f light1Color = new Color3f(1.0f, 1.0f,1.0f);		Vector3f light1Direction  = new Vector3f(4.0f, -7.0f, -12.0f);		DirectionalLight light1			= new DirectionalLight(light1Color, light1Direction);		light1.setInfluencingBounds(bounds);		sceneRoot.addChild(light1);		//添加蓝绿色平行光		Color3f light2Color = new Color3f(0.0f, 0.3f, 0.4f);		Vector3f light2Direction  = new Vector3f(-6.0f, -2.0f, -1.0f);		DirectionalLight light2			= new DirectionalLight(light2Color, light2Direction);		light2.setInfluencingBounds(bounds);		sceneRoot.addChild(light2);		//添加键盘控制功能,左右键控制旋转视角,上下键控制前进后退视角		KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(																  su.getViewingPlatform().getViewPlatformTransform());		keyNavBeh.setSchedulingBounds (bounds);		sceneRoot.addChild(keyNavBeh);				//缩放物体		Transform3D	scaleT3 = new Transform3D();		scaleT3.setScale( scalingFactor );		TransformGroup scaleTG   = new TransformGroup( scaleT3 );		sceneRoot.addChild( scaleTG );		//物体绕y轴旋转90度		Transform3D	rotyT3 = new Transform3D();		rotyT3.rotY(Math.PI/2);		TransformGroup rotyTG   = new TransformGroup( rotyT3 );		scaleTG.addChild (rotyTG);		//物体绕z轴旋转45度		Transform3D	rotzT3 = new Transform3D();		rotzT3.rotZ(Math.PI/4);		TransformGroup rotzTG   = new TransformGroup( rotzT3 );		rotyTG.addChild (rotzTG);					try{				//导入3DS模型			Load3DS   l           = new Load3DS( filename, this,												 new Hashtable(), new Hashtable(),												 new Properties() );			rotzTG.addChild( l.root() );		}		catch ( IOException e ){			System.err.println( "Exception: " + e.getMessage() );			e.printStackTrace( System.err );		}		return sceneRoot;	}    public BranchGroup createBackGraph() {         // Create the root of the branch graph		BranchGroup objRoot = new BranchGroup();		         PointArray starGeom1 = new PointArray(7, PointArray.COORDINATES);                          Appearance starAppear1 = new Appearance();         starGeom1.setCoordinate( 0, new Point3f(-0.357771f,0.268328f,-0.894427f));         starGeom1.setCoordinate( 1, new Point3f(-0.330825f,0.271168f,-0.903893f));         starGeom1.setCoordinate( 2, new Point3f(-0.303873f,0.27376f,-0.912533f));         starGeom1.setCoordinate( 3, new Point3f(-0.276172f,0.276172f,-0.920575f));         starGeom1.setCoordinate( 4, new Point3f(-0.320406f,0.243509f,-0.915447f));         starGeom1.setCoordinate( 5, new Point3f(-0.32264f,0.214786f,-0.92183f));         starGeom1.setCoordinate( 6, new Point3f(-0.365148f,0.182574f,-0.912871f));         PointAttributes point1 = new PointAttributes(4.0f, false);         starAppear1.setPointAttributes(point1);         objRoot.addChild(new Shape3D(starGeom1, starAppear1));		 		 PointArray starGeom2 = new PointArray(8, PointArray.COORDINATES);                          Appearance starAppear2 = new Appearance();         starGeom2.setCoordinate( 0, new Point3f(-0.232873f,0.279448f,-0.931493f));         starGeom2.setCoordinate( 1, new Point3f(-0.249493f,0.248932f,-0.935834f));         starGeom2.setCoordinate( 2, new Point3f(-0.265972f,0.218748f,-0.938833f));         starGeom2.setCoordinate( 3, new Point3f(-0.282216f,0.188144f,-0.940721f));         starGeom2.setCoordinate( 4, new Point3f(-0.219939f,0.250767f,-0.942732f));         starGeom2.setCoordinate( 5, new Point3f(-0.206407f,0.222035f,-0.952941f));         starGeom2.setCoordinate( 6, new Point3f(-0.19245f,0.19245f,-0.96225f));		 starGeom2.setCoordinate( 7, new Point3f(-0.236567f,0.220481f,-0.946269f));         PointAttributes point2 = new PointAttributes(4.0f, false);		 point2.setPointAntialiasingEnable (true);         starAppear2.setPointAttributes(point2);         objRoot.addChild(new Shape3D(starGeom2, starAppear2));		 		 PointArray starGeom3 = new PointArray(7, PointArray.COORDINATES);                          Appearance starAppear3 = new Appearance();         starGeom3.setCoordinate( 0, new Point3f(-0.188144f,0.282216f,-0.940721f));         starGeom3.setCoordinate( 1, new Point3f(-0.174425f,0.253121f,-0.951581f));         starGeom3.setCoordinate( 2, new Point3f(-0.160159f,0.223992f,-0.961341f));         starGeom3.setCoordinate( 3, new Point3f(-0.145521f,0.194028f,-0.970142f));         starGeom3.setCoordinate( 4, new Point3f(-0.128742f,0.225033f,-0.965808f));         starGeom3.setCoordinate( 5, new Point3f(-0.111973f,0.255445f,-0.960318f));         starGeom3.setCoordinate( 6, new Point3f(-0.0953463f,0.286039f,-0.953463f));         PointAttributes point3 = new PointAttributes(2.0f, false);         starAppear3.setPointAttributes(point3);         objRoot.addChild(new Shape3D(starGeom3, starAppear3));		 		 PointArray starGeom4 = new PointArray(8, PointArray.COORDINATES);                          Appearance starAppear4 = new Appearance();         starGeom4.setCoordinate( 0, new Point3f(-0.09759f,0.19518f,-0.9759f));         starGeom4.setCoordinate( 1, new Point3f(-0.0808613f,0.226179f,-0.970724f));         starGeom4.setCoordinate( 2, new Point3f(-0.064229f,0.25653f,-0.9644f));         starGeom4.setCoordinate( 3, new Point3f(-0.0478365f,0.287019f,-0.95673f));         starGeom4.setCoordinate( 4, new Point3f(-0.0321643f,0.256928f,-0.965895f));         starGeom4.setCoordinate( 5, new Point3f(-0.0161648f,0.226892f,-0.973786f));         starGeom4.setCoordinate( 6, new Point3f(0f,0.196116f,-0.980581f));		 starGeom4.setCoordinate( 7, new Point3f(-0.048638f,0.226653f,-0.97276f));         PointAttributes point4 = new PointAttributes(3.0f, false);		 starAppear4.setPointAttributes(point4);         objRoot.addChild(new Shape3D(starGeom4, starAppear4));         int[] stripCount1 = {9};         LineStripArray orion1 = new LineStripArray(9, LineStripArray.COORDINATES, stripCount1);         orion1.setCoordinate( 0, new Point3f(0.0971f,-0.218475f,-0.971f));         orion1.setCoordinate( 1, new Point3f(0.121662f,-0.194659f,-0.973296f));         orion1.setCoordinate( 2, new Point3f(0.169129f,-0.193291f,-0.966454f));         orion1.setCoordinate( 3, new Point3f(0.19151f,-0.215449f,-0.957552f));         orion1.setCoordinate( 4, new Point3f(0.16738f,-0.239114f,-0.956456f));         orion1.setCoordinate( 5, new Point3f(0.189352f,-0.26036f,-0.946762f));         orion1.setCoordinate( 6, new Point3f(0.165313f,-0.283394f,-0.944648f));         orion1.setCoordinate( 7, new Point3f(0.118879f,-0.28531f,-0.951034f));         orion1.setCoordinate( 8, new Point3f(0.0959754f,-0.263932f,-0.959754f));         objRoot.addChild(new Shape3D(orion1));		 		 int[] stripCount2 = {7};         LineStripArray orion2 = new LineStripArray(7, LineStripArray.COORDINATES, stripCount2);         orion2.setCoordinate( 0, new Point3f(0.282216f,-0.188144f,-0.940721f));         orion2.setCoordinate( 1, new Point3f(0.345124f,-0.184066f,-0.920331f));         orion2.setCoordinate( 2, new Point3f(0.363542f,-0.204493f,-0.908856f));         orion2.setCoordinate( 3, new Point3f(0.359846f,-0.247394f,-0.899615f));         orion2.setCoordinate( 4, new Point3f(0.33804f,-0.270432f,-0.901441f));         orion2.setCoordinate( 5, new Point3f(0.276172f,-0.276172f,-0.920575f));         orion2.setCoordinate( 6, new Point3f(0.282216f,-0.188144f,-0.940721f));         objRoot.addChild(new Shape3D(orion2));                 objRoot.compile();         return objRoot;     } 	public SimpleBackground(){		//创建画布对象,这样Java 3D可以渲染场景,并添加到applet窗口		setLayout( new BorderLayout() );		canvas = new Canvas3D( null );		canvas.setStereoEnable(false);		add( "Center", canvas );		//创建虚拟空间,这是所有场景的容器		su = new SimpleUniverse(canvas);		//后移视角平台,这样观察者可以看见整个场景		su.getViewingPlatform().setNominalViewingTransform();		//创建场景		createScene();		//将场景添加到虚拟空间		su.addBranchGraph(sceneRoot);		//得到该虚拟空间的高精度坐标系		locale=su.getLocale();		//创建适当的视角以便观察		createView();	}	private void createView(){		//创建观察者的头部和所处的环境		physicalBody    = new PhysicalBody();		physicalEnv     = new PhysicalEnvironment();		//创建视角,只有处在视角之内的场景被渲染,是可见的		view    = new View();		//设置观察者		view.setPhysicalBody( physicalBody );		view.setPhysicalEnvironment( physicalEnv );		//创建观察者的位置为(0,0,6),沿屏幕后退6单位		BranchGroup     vpRoot  = new BranchGroup();		Transform3D     t       = new Transform3D();		t.set( new Vector3f( 0.0f, 0.0f, 6.0f ) );		TransformGroup  vpTrans = new TransformGroup( t );		vp = new ViewPlatform();		vpTrans.addChild( vp );		vpRoot.addChild( vpTrans );		view.attachViewPlatform( vp );		//将观察者和坐标系联系起来		locale.addBranchGraph( vpRoot );	}	public static void main( String[] args ){		Frame frame = new MainFrame(new SimpleBackground(), 500, 400);	}}

⌨️ 快捷键说明

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