您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > linux/Unix相关 > 魔方java源代码
//存为MoFang.javaimportjava.applet.Applet;importjava.awt.*;importcom.sun.j3d.utils.applet.MainFrame;importjava.awt.BorderLayout;importcom.sun.j3d.utils.universe.SimpleUniverse;importjavax.media.j3d.*;importjavax.vecmath.*;importcom.sun.j3d.utils.behaviors.mouse.*;importcom.sun.j3d.utils.behaviors.keyboard.*;importcom.sun.j3d.utils.picking.behaviors.*;importcom.sun.j3d.utils.geometry.*;importcom.sun.j3d.utils.image.TextureLoader;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjavax.swing.JFrame.*;importjavax.swing.*;classmySimpleUniverseextendsApplet{BranchGroupcreateSceneGraph(Canvas3Dcanvas){//System.out.print(**1**);//创建变换组,无用的t3DTransform3Dt3d=newTransform3D();TransformGrouptrans=newTransformGroup(t3d);trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);//创建分枝组BranchGroupobjRoot=newBranchGroup();//测试//SomeShape3D.book3D(this,trans);SomeShape3D.addText3DDonghua(trans,魔方,newPoint3f(-7.0f,6.0f,6.0f),0.1f,newColor3f(1.0f,0.0f,0.0f),1);//初始化数据结构System.out.println(\n\n载入方块,并向变换组中加入每个方块的坐标系和方块...);for(inti=0;i=2;i++)for(intj=0;j=2;j++)for(intk=0;k=2;k++){int[]p;p=Position.getPxyzFromPositionAy(i,j,k,MoFang.positionArray);MoFang.blockArray[i][j][k]=newBlock(i,j,k,p[0],p[1],p[2],trans,t3d,objRoot,this);}System.out.println(加入每个方块的坐标系和方块,完成.\n);//创建大坐标轴,自动加到主坐标系SomeShape3D.zuoBiaoZhuBigXShape3D(trans);SomeShape3D.zuoBiaoZhuBigYShape3D(trans);SomeShape3D.zuoBiaoZhuBigZShape3D(trans);//创建边界对象BoundingSpherebounds=newBoundingSphere(newPoint3d(0.0,0.0,0.0),100);//创建鼠标pick行为,加到分支组objRootPickRotateBehaviorpickRotate=newPickRotateBehavior(objRoot,canvas,bounds);PickTranslateBehaviorpickTranslate=newPickTranslateBehavior(objRoot,canvas,bounds);PickZoomBehaviorpickZoom=newPickZoomBehavior(objRoot,canvas,bounds);//objRoot.addChild(pickRotate);objRoot.addChild(pickTranslate);//objRoot.addChild(pickZoom);//创建鼠标旋转行为MouseRotatebehavior=newMouseRotate();behavior.setTransformGroup(trans);behavior.setSchedulingBounds(bounds);//创建鼠标平移行为//MouseTranslatemyMouseRotate=newMouseTranslate();//myMouseRotate.setTransformGroup(trans);//myMouseRotate.setSchedulingBounds(bounds);//创建鼠标缩放行为MouseZoommyMouseZoom=newMouseZoom();myMouseZoom.setTransformGroup(trans);myMouseZoom.setSchedulingBounds(bounds);//创建键盘默认行为KeyNavigatorBehaviorkeyNavBeh=newKeyNavigatorBehavior(trans);keyNavBeh.setSchedulingBounds(bounds);objRoot.addChild(keyNavBeh);//白色背景Backgroundbg=newBackground(newColor3f(0.0f,0.0f,0.0f));bg.setApplicationBounds(bounds);objRoot.addChild(bg);//创建带材质的背景//TextureLoaderbgTexture=newTextureLoader(bg3.jpg,this);//Backgroundbg=newBackground(bgTexture.getImage());//bg.setApplicationBounds(bounds);//trans.addChild(shape1);//3D物体加到变换组//trans.addChild(shape2);//3D物体加到变换组objRoot.addChild(trans);//变换组加到分枝组objRoot.addChild(behavior);//鼠标行为加到分枝组//objRoot.addChild(myMouseRotate);//objRoot.addChild(myMouseZoom);//objRoot.addChild(bg);//背景加到分枝组//编译objRoot.compile();//回送创建好的带3D物体的分枝组returnobjRoot;}mySimpleUniverse(){//创建带控制的画布GraphicsConfigurationconfig=SimpleUniverse.getPreferredConfiguration();Canvas3Dc=newCanvas3D(config);//创建以画布为依托的简单场景图对象,没有多个LocaleSimpleUniverseu=newSimpleUniverse(c);u.getViewingPlatform().setNominalViewingTransform();//创建分支组对象BranchGroupscene=createSceneGraph(c);//组装,分支组对象加到场景图u.addBranchGraph(scene);//带场景图的画布加到本applet中setLayout(newBorderLayout());add(Center,c);}//测试码//publicstaticvoidmain(Stringaregs[])//{newMainFrame(newmySimpleUniverse(),200,200);//加applet到应用程序界面//}}classSomeShape3D{publicstaticfloatzuoBiaoZhouSmallDingDian=0.09f;//小坐标顶点位置publicstaticfloatzuoBiaoZhouSmallDingXi=0.02f;//小坐标顶点伞的半径publicstaticfloatzuoBiaoZhouSmallDingChang=0.07f;//小坐标顶点伞的长度publicstaticfloatzuoBiaoZhouSmallWeiDian=-0.09f;//小坐标尾巴的位置publicstaticfloatzuoBiaoZhouBigDingDian=1.0f;//大坐标顶点位置publicstaticfloatzuoBiaoZhouBigDingXi=0.04f;//大坐标顶点伞的半径publicstaticfloatzuoBiaoZhouBigDingChang=0.8f;//大坐标顶点伞的长度publicstaticfloatzuoBiaoZhouBigWeiDian=-1.0f;//大坐标尾巴的位置publicstaticfloatfangKuaiBanJing=0.18f;//每个方块的半径publicstaticvoidzuoBiaoZhuBigXShape3D(TransformGrouptrans){//创建大坐标轴对象inti;floatx1,x2,y1,y2,z1,z2;SomeShape3D.addText3DDonghua(trans,X,newPoint3f(zuoBiaoZhouBigDingDian*10,0.0f,0.0f),0.1f,Block.mianColor[0],0);Point3f[]vert=newPoint3f[41];Color3f[]colors=newColor3f[41];for(i=0;i27;i++){if(i==0){vert[i]=newPoint3f(zuoBiaoZhouBigDingDian,0.0f,0.0f);colors[i]=Block.mianColor[0];}else{z1=(float)(zuoBiaoZhouBigDingXi*Math.cos(i*2*Math.PI/25));x1=zuoBiaoZhouBigDingChang;y1=(float)(zuoBiaoZhouBigDingXi*Math.sin(i*2*Math.PI/25));vert[i]=newPoint3f(x1,y1,z1);colors[i]=Block.mianColor[0];}}for(i=0;i14;i++){if(i==0){vert[27+i]=newPoint3f(zuoBiaoZhouBigDingDian,0.0f,0.0f);colors[27+i]=Block.mianColor[0];}else{z1=(float)(0.01f*Math.cos(i*2*Math.PI/12));x1=zuoBiaoZhouBigWeiDian;y1=(float)(0.01f*Math.sin(i*2*Math.PI/12));vert[27+i]=newPoint3f(x1,y1,z1);colors[27+i]=Block.mianColor[1];}}intcount[]=newint[2];count[0]=27;count[1]=14;TriangleFanArraytri=newTriangleFanArray(vert.length,TriangleFanArr
本文标题:魔方java源代码
链接地址:https://www.777doc.com/doc-7027992 .html