返回列表 发帖

[转帖]使用Jbulider开发j2me程序

写篇关于怎样使用JBuilder开发j2me程序的例子。本人使用JBuilder2005,赶下时髦,呵呵~~2005自带WTK2.1,自己下载2.2版本,2.2那个默认的模拟器看着实在爽,^_^。另外,Jbuilder X下的WTK也是2.0的版本好象。Jbuilder 9的WTK是1.04。 首先开启Jbuilder.建立一个Project。 然后填写名字和路径。继续: 然后选择JDK路径,本身已有WTK2.1,你可以选择。但是你也可以自己选择其他的WTK版本。 点击jdk后面的路径按钮,继续: 然后ok,next.工程建立完毕. 接着点击file->New: 然后在对话框左边选择Micro,你就能看到右边出现MIDlet,Displayable. 接上面的:如图示 点击MIDlet or Displayable,则让你输入MIDlet的名字.如图: 继续下一步,让你输入一个Displayable的名字,包括类型(form,canvas等),是否自动加载Command 等等 然后Jbuilder自动生成所需代码,以及Jbuilder自加的一些东西.你可以添加或修改之. 这里我将本站一篇关于介绍Gamecanvas的文章的代码拿过来,做少量修改,作为本文章的一个演示; TestMIDlet.java文件
  1. package temps;
  2. import javax.microedition.midlet.*;
  3. import javax.microedition.lcdui.*;
  4. /**
  5. * <p>Title: </p>
  6. *
  7. * <p>Description: </p>
  8. *
  9. * <p>Copyright: Copyright (c) 2005</p>
  10. *
  11. * <p>Company: </p>
  12. *
  13. * @author not attributable
  14. * @version 1.0
  15. */
  16. public class TestMIDlet extends MIDlet
  17. {
  18. private Display display;
  19. public void startApp()
  20. {
  21. try
  22. {
  23. display = Display.getDisplay(this);
  24. Displayable1 gameCanvas = new Displayable1();
  25. gameCanvas.start();
  26. display.setCurrent(gameCanvas);
  27. }
  28. catch (Exception ex)
  29. {
  30. System.out.println(ex);
  31. }
  32. }
  33. public Display getDisplay()
  34. {
  35. return display;
  36. }
  37. public void pauseApp()
  38. {
  39. }
  40. public void destroyApp(boolean unconditional)
  41. {
  42. exit();
  43. }
  44. public void exit()
  45. {
  46. System.gc();
  47. destroyApp(false);
  48. notifyDestroyed();
  49. }
  50. }
复制代码
Displayable1.java内容:
  1. package temps;
  2. import javax.microedition.lcdui.*;
  3. import java.util.Random;
  4. import javax.microedition.lcdui.game.*;
  5. /**
  6. * <p>Title: </p>
  7. *
  8. * <p>Description: </p>
  9. *
  10. * <p>Copyright: Copyright (c) 2005</p>
  11. *
  12. * <p>Company: </p>
  13. *
  14. * @author not attributable
  15. * @version 1.0
  16. */
  17. public class Displayable1 extends GameCanvas implements Runnable
  18. {
  19. private boolean isPlay; // Game Loop runs when isPlay is true
  20. private long delay; // To give thread consistency
  21. private int currentX, currentY; // To hold current position of the ';X';
  22. private int width; // To hold screen width
  23. private int height; // To hold screen height
  24. private Random random=new Random(System.currentTimeMillis());
  25. private final int[] sequence={0,1,2,1,0,1,2,1,0,1,2,1,1,1,1,1,1};
  26. private int counter=0;
  27. private int enemyX,enemyY;
  28. private int enemyDirect=0;
  29. private boolean isdown=false;
  30. private boolean isalive=true;
  31. // Sprites to be used
  32. private Sprite playerSprite;
  33. private Sprite backgroundSprite;
  34. private Sprite enemySprite;
  35. // Layer Manager
  36. private LayerManager layerManager;
  37. // Constructor and initialization
  38. public Displayable1() throws Exception
  39. {
  40. super(true);
  41. width = getWidth();
  42. height = getHeight();
  43. currentX = width / 2;
  44. currentY = height / 2;
  45. enemyX=width/4;
  46. enemyY=height/4;
  47. delay = 100;
  48. Image playerImage=null;
  49. Image backgroundImage=null;
  50. Image enemy=null;
  51. // Load Images to Sprites
  52. try
  53. {
  54. playerImage = Image.createImage("/transparent.png");
  55. backgroundImage = Image.createImage("/background.png");
  56. enemy=Image.createImage("/frames.png");
  57. }
  58. catch(Exception e)
  59. {
  60. System.out.println("get Image error!"+e.getMessage());
  61. }
  62. playerSprite = new Sprite(playerImage, 32, 32);
  63. System.out.println(playerSprite.getRawFrameCount());
  64. enemySprite=new Sprite(enemy,50,21);
  65. backgroundSprite = new Sprite(backgroundImage);
  66. layerManager = new LayerManager();
  67. layerManager.append(playerSprite);
  68. layerManager.append(enemySprite);
  69. layerManager.append(backgroundSprite);
  70. }
  71. // Automatically start thread for game loop
  72. public void start()
  73. {
  74. isPlay = true;
  75. Thread t = new Thread(this);
  76. t.start();
  77. }
  78. public void stop()
  79. {
  80. isPlay = false;
  81. }
  82. // Main Game Loop
  83. public void run()
  84. {
  85. Graphics g = getGraphics();
  86. while (isPlay == true)
  87. {
  88. input();
  89. drawScreen(g);
  90. try
  91. {
  92. Thread.sleep(delay);
  93. }
  94. catch (InterruptedException ie)
  95. {
  96. }
  97. }
  98. }
  99. // Method to Handle User Inputs
  100. private void input()
  101. {
  102. int keyStates = getKeyStates();
  103. playerSprite.setFrame(0);
  104. if(isalive)
  105. {
  106. if(counter==0)
  107. {
  108. enemyDirect=Math.abs(random.nextInt()%4);
  109. }
  110. counter++;
  111. switch(enemyDirect)
  112. {
  113. case 0:
  114. enemyY=Math.max(0,--enemyY);
  115. break;
  116. case 1:
  117. enemyY=Math.min(++enemyY,height-enemySprite.getHeight());
  118. break;
  119. case 2:
  120. enemyX=Math.max(0,--enemyX);
  121. break;
  122. case 3:
  123. enemyX=Math.min(++enemyX,width-enemySprite.getWidth());
  124. break;
  125. }
  126. counter=(counter++)%(sequence.length());
  127. enemySprite.setFrame(sequence[counter]);
  128. }
  129. else
  130. {
  131. enemyY+=counter;
  132. enemyY=Math.min(enemyY,height-enemySprite.getHeight());
  133. if(enemyY<height-enemySprite.getHeight())
  134. counter++;
  135. enemySprite.setFrame(3);
  136. }
  137. // Left
  138. if ((keyStates & LEFT_PRESSED) != 0)
  139. {
  140. currentX = Math.max(0, currentX - 1);
  141. playerSprite.setFrame(1);
  142. }
  143. // Right
  144. if ((keyStates & RIGHT_PRESSED) != 0)
  145. if (currentX + 5 < width)
  146. {
  147. currentX = Math.min(width, currentX + 1);
  148. playerSprite.setFrame(3);
  149. }
  150. // Up
  151. if ((keyStates & UP_PRESSED) != 0)
  152. {
  153. currentY = Math.max(0, currentY - 1);
  154. playerSprite.setFrame(2);
  155. }
  156. // Down
  157. if ((keyStates & DOWN_PRESSED) != 0)
  158. {
  159. isdown=true;
  160. }
  161. if ((keyStates & DOWN_PRESSED) != 0)
  162. {
  163. if(isdown)
  164. {
  165. if (currentY + 10 < height)
  166. {
  167. currentY = Math.min(height, currentY + 1);
  168. playerSprite.setFrame(4);
  169. }
  170. }
  171. }
  172. if((keyStates & FIRE_PRESSED)!=0)
  173. {
  174. isalive=!isalive;
  175. counter=0;
  176. }
  177. if(!(enemyX+enemySprite.getWidth()<currentX || enemyY+enemySprite.getHeight()<currentY || enemyX>currentX+playerSprite.getWidth() || enemyY>currentY+playerSprite.getHeight()) && isalive)
  178. {
  179. counter=0;
  180. isalive=false;
  181. }
  182. }
  183. // Method to Display Graphics
  184. private void drawScreen(Graphics g)
  185. {
  186. g.setColor(0xffffff);
  187. g.fillRect(0, 0, getWidth(), getHeight());
  188. g.setColor(0x0000ff);
  189. // updating player sprite position
  190. playerSprite.setPosition(currentX, currentY);
  191. enemySprite.setPosition(enemyX,enemyY);
  192. layerManager.setViewWindow(0, 0, width, height);
  193. layerManager.paint(g, 0,0);
  194. g.setColor(0xffffff);
  195. flushGraphics();
  196. }
  197. }
复制代码
此程序里敌人随机向4个方向移动,而玩家控制的角色可任意移动, 如果两者发生碰撞,则敌人死亡. 另外按fire键可直接让敌人生或死,呵呵~~ 关于运行,Jbuilder里有个运行配置: 一般不用修改,如果多个Midlet,你可以在这里添加. 关于用Jbuilder打包. 一样file->new: 这个在2005和9,x里有所不同,选择Archive,右边选择MIDlet,然后ok. 这里选择要打包文件的目录,当前默认是所有的class文件和资源文件,你也可以删除掉,自己选择一部分. 然后next,直到最后. 然后运行,在运行配置中默认重新rebuild,打包等等.然后你能看到左上框里有jar文件和jad文件. 运行结果: 呵呵,用你控制的主角去碰撞这只大飞虫试试:

[转帖]使用Jbulider开发j2me程序

   Eclipse也可以开发J2ME,爱好J2ME开发的可以下载西门子、诺基亚的相关模拟器试用。

TOP

[转帖]使用Jbulider开发j2me程序

啊!好东西.偶也要学!!!

TOP

返回列表 回复 发帖