MainMenuCanvas.java
上传用户:jinxueyang
上传日期:2016-05-15
资源大小:104k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. package game;
  2. import javax.microedition.lcdui.*;
  3. public class MainMenuCanvas extends Canvas {
  4. //Display对象
  5. private Display display;
  6. //菜单序号
  7. private int index = 0;
  8. //屏幕的宽度
  9. private int width;
  10. //屏幕的高度
  11. private int height;
  12. //游戏菜单
  13. private Image image[] = new Image[6];
  14. public MainMenuCanvas(Display display)
  15. {
  16. this.display = display;
  17. //设置全屏
  18. //this.setFullScreenMode(true);
  19.         //获得屏幕高度与宽度
  20. width = this.getWidth();
  21. height = this.getHeight();
  22.     
  23. //导入图片
  24. try
  25. {
  26. image[0] = Image.createImage("/res/m_newgame.png");
  27. image[1] = Image.createImage("/res/m_highscores.png");
  28. image[2] = Image.createImage("/res/m_options.png");
  29. image[3] = Image.createImage("/res/m_help.png");
  30. image[4] = Image.createImage("/res/m_about.png");
  31. image[5] = Image.createImage("/res/m_quit.png");
  32. }
  33. catch(Exception e)
  34. {}
  35. index = ColorLinezMIDlet.menuIndex;
  36. }
  37. protected void paint(Graphics g)
  38. {
  39. //绘制背景
  40. g.setColor(0x000000);
  41. g.fillRect(0, 0, width, height);
  42. g.setColor(0xFFFFFF);
  43. for (int i = 0; i < image.length; i++)
  44. {
  45. g.drawImage(image[i], 60, 15 + i * 25, Graphics.LEFT|Graphics.TOP);
  46. }
  47. //绘制选择框
  48. g.drawRect(59, 14 + index * 25, 62, 21);
  49. }
  50. protected void keyPressed(int keyCode) {
  51. //获得游戏动作
  52. int action = this.getGameAction(keyCode);
  53. switch (action)
  54. {
  55.         //向上
  56. case Canvas.UP:
  57. if (index > 0)
  58.             {
  59. index--;
  60. }
  61.             else
  62.             {
  63. index = 5;
  64. }
  65. repaint();
  66. break;
  67.         //向下
  68. case Canvas.DOWN:
  69. if (index < 5)
  70. {
  71. index++;
  72. }
  73. else
  74. {
  75. index = 0;
  76. }
  77. repaint();
  78. break;
  79. //确定
  80. case Canvas.FIRE:
  81. ColorLinezMIDlet.menuIndex = index;
  82. switch (index)
  83. {
  84. case 0:
  85. //显示游戏界面
  86. display.setCurrent(new LinezCanvas(display));
  87. break;
  88. case 1:
  89. display.setCurrent(new HighScoreCanvas(display));
  90. break;
  91. case 2:
  92. //显示设置界面
  93. display.setCurrent(new OptionCanvas(display));
  94. break;
  95. case 3:
  96. //显示帮助界面
  97. display.setCurrent(new HelpCanvas(display));
  98. break;
  99. case 4:
  100. //显示关于界面
  101. display.setCurrent(new AboutCanvas(display));
  102. break;
  103. case 5:
  104. //退出
  105. ColorLinezMIDlet.quitApp();
  106. break;
  107. }
  108. //释放图版资源
  109. for ( int i = 0; i < 6; i++ )
  110. {
  111. image[i] = null;
  112. }
  113. break;
  114. }
  115. }
  116. }