j2me 3d场景设计

  • L8_698409
    了解作者
  • 2.6MB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-24 09:57
    上传日期
基于j2me的3d场景设计 立即模式 构建5个五角星 可实现五角星的旋转 缩放 旋转加速等
TestStar.rar
内容介绍
import java.io.IOException; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.m3g.Appearance; import javax.microedition.m3g.Background; import javax.microedition.m3g.Camera; import javax.microedition.m3g.Graphics3D; import javax.microedition.m3g.Image2D; import javax.microedition.m3g.IndexBuffer; import javax.microedition.m3g.Light; import javax.microedition.m3g.Material; import javax.microedition.m3g.Mesh; import javax.microedition.m3g.PolygonMode; import javax.microedition.m3g.Texture2D; import javax.microedition.m3g.Transform; import javax.microedition.m3g.TriangleStripArray; import javax.microedition.m3g.VertexArray; import javax.microedition.m3g.VertexBuffer; import javax.microedition.m3g.World; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; public class myStar extends GameCanvas implements Runnable{ private Thread th; private Graphics3D g3d; private Camera iCamera; private Light iLight; private Transform iTransform=new Transform() ; private Transform iTransform2=new Transform() ; private Transform iTransform3=new Transform() ; private Transform iTransform4=new Transform() ; private Transform iTransform5=new Transform() ; private Background iBackground =new Background(); private VertexBuffer vb; private IndexBuffer iIb; private Appearance iAppearance; private Material iMaterial = new Material(); private Image iImage; private Image2D backDrop; private Transform transform3D; private Mesh mesh; private Mesh mesh2; private Mesh mesh3; private Mesh mesh4; private Mesh mesh5; private World world = new World(); private float angle=5.0f; protected myStar(boolean suppressKeyEvents) { super(suppressKeyEvents); setFullScreenMode(true);//设置全屏 th = new Thread(this); g3d = Graphics3D.getInstance(); //顶点位置 short[]vert={ //前后 17,8,1, 5,8,6, 10,-2,6, 0,0,8, 0,22,1, -5,8,6, 5,8,6, 0,0,8, -17,8,1, -10,-2,6, -5,8,6, 0,0,8, -13,-20,1, 0,-8,6, -10,-2,6, 0,0,8, 13,-20,1, 10,-2,6, 0,-8,6, 0,0,8, 17,8,-1, 10,-2,-6, 5,8,-6, 0,0,-8, 0,22,-1, 5,8,-6, -5,8,-6, 0,0,-8, -17,8,-1, -5,8,-6, -10,-2,-6, 0,0,-8, -13,-20,-1,-10,-2,-6, 0,-8,-6, 0,0,-8, 13,-20,-1, 0,-8,-6, 10,-2,-6, 0,0,-8, //左右 -5,8,1, -5,8,-1, -10,-2,6, -10,-2,-6, -10,-2,6, -10,-2,-6, -13,-20,1, -13,-20,-1, 17,8,-1, 17,8,1, 10,-2,-6, 10,-2,6, 10,-2,-6, 10,-2,6, 13,-20,-1, 13,-20,1, //上下 -17,8,-1, -17,8,1, -5,8,-6, -5,8,6, -5,8,-6, -5,8,6, 0,22,-1, 0,22,1, 0,22,-1, 0,22,1, 5,8,-6, 5,8,6, 5,8,-6, 5,8,6, 17,8,-1, 17,8,1, //下 -13,-20,1, -13,-20,-1, 0,-8,6, 0,-8,-6, 0,-8,6, 0,-8,-6, 13,-20,1, 13,-20,-1 }; VertexArray vertArray = new VertexArray(vert.length/3,3,2); vertArray.set(0, vert.length/3, vert); vb =new VertexBuffer(); vb.setPositions(vertArray, 1.0f, null); int[] stripLen = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}; iIb = new TriangleStripArray(0, stripLen); //添加外观模型 iAppearance = new Appearance(); iMaterial.setVertexColorTrackingEnable(true); iMaterial.setColor(Material.SPECULAR,0X00000000); iMaterial.setShininess(100.0f); iAppearance.setMaterial(iMaterial); PolygonMode polygonMode =new PolygonMode(); polygonMode.setPerspectiveCorrectionEnable(true); polygonMode.setCulling(PolygonMode.CULL_BACK); polygonMode.setShading(PolygonMode.SHADE_SMOOTH); polygonMode.setTwoSidedLightingEnable(true); polygonMode.setLocalCameraLightingEnable(false); iAppearance.setPolygonMode(polygonMode); //实现立方体模型的纹理映射 mesh=new Mesh(vb,iIb,iAppearance); mesh2=new Mesh(vb,iIb,iAppearance); mesh3=new Mesh(vb,iIb,iAppearance); mesh4=new Mesh(vb,iIb,iAppearance); mesh5=new Mesh(vb,iIb,iAppearance); try{ iImage = Image.createImage("/star6.png"); }catch(java.io.IOException e){ System.out.println(e.getMessage()); } Image2D image2D = new Image2D(Image2D.RGB,iImage); Texture2D texture =new Texture2D(image2D); texture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP); texture.setBlending(Texture2D.FUNC_REPLACE); texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST); iAppearance.setTexture(0, texture); //设置背景图片 Image image = null; try { image = Image.createImage("/background4.png"); } catch (IOException e) { e.printStackTrace(); } backDrop = new Image2D(Image2D.RGB,image); iBackground.setImage(backDrop); world.setBackground(iBackground); //设置光线为白色强光 iLight=new Light(); iLight.setColor(0xffffff); iLight.setIntensity(1.25f); world.addChild(iLight); //设置为透射投影方式 iCamera = new Camera(); iCamera.setPerspective(80.0f, (float)getWidth()/(float)getHeight(), 1.0f, 1000.0f); world.addChild(iCamera); world.addChild(mesh); iTransform.postScale(0.5f, 0.5f, 0.5f); iTransform2.postTranslate(-35.0f, 20.0f, -4.0f); iTransform3.postTranslate(-25.0f, 10.0f, 12.0f); iTransform4.postTranslate(-16.0f, 0.0f, 20.0f); iTransform5.postTranslate(-0.0f, -10.0f, 25.0f); iTransform2.postScale(0.2f, 0.2f, 0.2f); iTransform3.postScale(0.2f, 0.2f, 0.2f); iTransform4.postScale(0.2f, 0.2f, 0.2f); iTransform5.postScale(0.2f, 0.2f, 0.2f); world.addChild(mesh2); world.addChild(mesh3); world.addChild(mesh4); world.addChild(mesh5); world.setActiveCamera(iCamera); //对摄像机进行位置的调整 transform3D = new Transform(); transform3D.postTranslate(0.0f, 0.0f, 50.0f); iCamera.setTransform(transform3D); th.start();// 启动线程 } public void keyPressed(int key) { if (key == -1) { transform3D.postTranslate(0.0f, 0.0f, 10.0f); iCamera.setTransform(transform3D); } if(key==-2){ transform3D.postTranslate(0.0f, 0.0f, -10.0f); iCamera.setTransform(transform3D); } else if (key == -3) { angle=angle+2.0f; } else if(key== -4){ angle=angle-1.0f; } } protected void keyRepeated(int key) { keyPressed(key); } public void draw(Graphics g) { try { g3d.bindTarget(g);// @备注5 // 将画笔绑定在3d空间画笔 g3d.clear(null); // 清屏 参数null 标识默认刷屏方式 iTransform.postRotate(15.0f, 3.0f, 1.0f, 0.0f); mesh.setTransform(iTransform); mesh2.setTransform(iTransform2); mesh3.setTransform(iTransform3); mesh4.setTransform(iTransform4); mesh5.setTransform(iTransform5); mesh2.preRotate(angle, 1, 2, 6); mesh3.preRotate(angle, 1, 2, 6); mesh4.preRotate(angle, 1, 2, 6); mesh5.preRotate(angle, 1, 2, 6); g3d.render(world); } catch (Exception e) { System.out.println("draw -> Error!! "); } finally { g3d.releaseTarget(); } g.setColor(0xffffff); } public void run() { while (true) { try { draw(this.getGraphics()); flushGraphics();// midp2.0 刷新画笔 Thread.sleep(100); // 休眠线程 } catch (InterruptedException e) { e.printStackTrace(); } } } }
评论
    相关推荐
    • buildu3d
      buildu3d
    • 3D GIS
      3D GIS中三维地形模拟与树木实体造型的研究
    • 3dmax
      水晶吊顶灯制作成品,3dmax实战项目1
    • cub3d
      cub3d
    • simEngine3D
      simEngine3D
    • cub3d
      cub3d
    • Milkshape3D
      Milkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zipMilkshape3D.zip
    • spindle3d
      一个斐济插件,用于自动测量有丝分裂纺锤体体积图像的3D形态学参数。 安装 安装 启动斐济并: 主轴3D 3D ImageJ套件 重新启动斐济
    • PerspectiveMatrix3D
      开发Stage3D时所用到的两个adobe工具包中的两个类PerspectiveMatrix3D 和 AGALMiniAssembler。
    • 3d
      3D