龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

剖析J2ME 3D编程的第一个3D程序(1)(2)

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
J2ME 3D编程的第一个3D程序源代码如下: //First3DMIDlet.java packagefirst3d; importjavax.microedition.midlet.*; importjavax.microedition.lcdui.*; publicclassFirst3DMIDletextendsMIDlet{ pri

J2ME 3D编程的第一个3D程序源代码如下: 

  1. //First3DMIDlet.java  
  2.  
  3. packagefirst3d;  
  4.  
  5. importjavax.microedition.midlet.*;  
  6.  
  7. importjavax.microedition.lcdui.*;  
  8.  
  9. publicclassFirst3DMIDletextendsMIDlet{  
  10.  
  11. privateFirst3DCanvasdisplayable=newFirst3DCanvas();  
  12.  
  13.  
  14. publicvoidstartApp(){  
  15.  
  16. Display.getDisplay(this).setCurrent(displayable);  
  17.  
  18. }  
  19.  
  20. publicvoidpauseApp(){}  
  21.  
  22. publicvoiddestroyApp(booleanunconditional){}  
  23.  
  24. }  
  25.  
  26. //First3Dcanvas.java  
  27.  
  28. packagefirst3d;  
  29.  
  30. importjavax.microedition.lcdui.*;  
  31.  
  32. importjavax.microedition.m3g.*;  
  33.  
  34. importjava.util.*;  
  35.  
  36. /**  
  37.  
  38. *第一个3D程序  
  39.  
  40. */  
  41.  
  42. publicclassFirst3DCanvas  
  43.  
  44. extendsCanvas  
  45.  
  46. implementsRunnable{  
  47.  
  48. /**World对象*/  
  49.  
  50. privateWorldmyWorld=null;  
  51.  
  52. /**Graphics3D对象*/  
  53.  
  54. privateGraphics3Dg3d=Graphics3D.getInstance();  
  55.  
  56. /**Camera对象*/  
  57.  
  58. privateCameracam=null;  
  59.  
  60. privateintviewport_x;  
  61.  
  62. privateintviewport_y;  
  63.  
  64. privateintviewport_width;  
  65.  
  66. privateintviewport_height;  
  67.  
  68. privatelongworldStartTime=0;  
  69.  
  70. //重绘时间  
  71.  
  72. privateintvalidity=0;  
  73.  
  74. publicFirst3DCanvas(){  
  75.  
  76. //启动重绘界面的线程  
  77.  
  78. Threadthread=newThread(this);  
  79.  
  80. thread.start();  
  81.  
  82. try{  
  83.  
  84. //导入3D图片  
  85.  
  86. myWorld=(World)Loader.load("/swerve.m3g")[0];  
  87.  
  88. viewport_x=0;  
  89.  
  90. viewport_y=0;  
  91.  
  92. viewport_width=getWidth();  
  93.  
  94. viewport_height=getHeight();  
  95.  
  96. cam=myWorld.getActiveCamera();  
  97.  
  98. //设置cam对象  
  99.  
  100. float[]params=newfloat[4];  
  101.  
  102. inttype=cam.getProjection(params);  
  103.  
  104. if(type!=Camera.GENERIC){  
  105.  
  106. //calculatewindowaspectratio  
  107.  
  108. floatwaspect=viewport_width/viewport_height;  
  109.  
  110. if(waspect<params[1]){  
  111.  
  112. floatheight=viewport_width/params[1];  
  113.  
  114. viewport_height=(int)height;  
  115.  
  116. viewport_y=(getHeight()-viewport_height)/2;  
  117.  
  118. }  
  119.  
  120. else{  
  121.  
  122. floatwidth=viewport_height*params[1];  
  123.  
  124. viewport_width=(int)width;  
  125.  
  126. viewport_x=(getWidth()-viewport_width)/2;  
  127.  
  128. }  
  129.  
  130. }  
  131.  
  132. worldStartTime=System.currentTimeMillis();  
  133.  
  134. }  
  135.  
  136. catch(Exceptione){}  
  137.  
  138. }  
  139.  
  140. protectedvoidpaint(Graphicsg){  
  141.  
  142. //清除背景  
  143.  
  144. g.setColor(0x00);  
  145.  
  146. g.fillRect(0,0,getWidth(),getHeight());  
  147.  
  148. //和3D对象绑定  
  149.  
  150. g3d.bindTarget(g);  
  151.  
  152. g3d.setViewport(viewport_x,viewport_y,
  153. viewport_width,viewport_height);  
  154.  
  155. longstartTime=System.currentTimeMillis()-worldStartTime;  
  156.  
  157. validity=myWorld.animate((int)startTime);  
  158.  
  159. try{  
  160.  
  161. g3d.render(myWorld);  
  162.  
  163. }  
  164.  
  165. finally{  
  166.  
  167. g3d.releaseTarget();  
  168.  
  169. }  
  170. }  
  171.  
  172. publicvoidrun(){  
  173.  
  174. try{  
  175.  
  176. while(true){  
  177.  
  178. //重绘图形  
  179.  
  180. repaint(viewport_x,viewport_y,
  181. viewport_width,viewport_height);  
  182. }  
  183.  
  184. }catch(Exceptione){}  
  185. }  
  186. }  

精彩图集

赞助商链接