- package game;
- import javax.microedition.lcdui.*;
- public class MainMenuCanvas extends Canvas {
- //Display对象
- private Display display;
- //菜单序号
- private int index = 0;
- //屏幕的宽度
- private int width;
- //屏幕的高度
- private int height;
- //游戏菜单
- private Image image[] = new Image[6];
- public MainMenuCanvas(Display display)
- {
- this.display = display;
- //设置全屏
- //this.setFullScreenMode(true);
- //获得屏幕高度与宽度
- width = this.getWidth();
- height = this.getHeight();
- //导入图片
- try
- {
- image[0] = Image.createImage("/res/m_newgame.png");
- image[1] = Image.createImage("/res/m_highscores.png");
- image[2] = Image.createImage("/res/m_options.png");
- image[3] = Image.createImage("/res/m_help.png");
- image[4] = Image.createImage("/res/m_about.png");
- image[5] = Image.createImage("/res/m_quit.png");
- }
- catch(Exception e)
- {}
- index = ColorLinezMIDlet.menuIndex;
- }
- protected void paint(Graphics g)
- {
- //绘制背景
- g.setColor(0x000000);
- g.fillRect(0, 0, width, height);
- g.setColor(0xFFFFFF);
- for (int i = 0; i < image.length; i++)
- {
- g.drawImage(image[i], 60, 15 + i * 25, Graphics.LEFT|Graphics.TOP);
- }
- //绘制选择框
- g.drawRect(59, 14 + index * 25, 62, 21);
- }
- protected void keyPressed(int keyCode) {
- //获得游戏动作
- int action = this.getGameAction(keyCode);
- switch (action)
- {
- //向上
- case Canvas.UP:
- if (index > 0)
- {
- index--;
- }
- else
- {
- index = 5;
- }
- repaint();
- break;
- //向下
- case Canvas.DOWN:
- if (index < 5)
- {
- index++;
- }
- else
- {
- index = 0;
- }
- repaint();
- break;
- //确定
- case Canvas.FIRE:
- ColorLinezMIDlet.menuIndex = index;
- switch (index)
- {
- case 0:
- //显示游戏界面
- display.setCurrent(new LinezCanvas(display));
- break;
- case 1:
- display.setCurrent(new HighScoreCanvas(display));
- break;
- case 2:
- //显示设置界面
- display.setCurrent(new OptionCanvas(display));
- break;
- case 3:
- //显示帮助界面
- display.setCurrent(new HelpCanvas(display));
- break;
- case 4:
- //显示关于界面
- display.setCurrent(new AboutCanvas(display));
- break;
- case 5:
- //退出
- ColorLinezMIDlet.quitApp();
- break;
- }
- //释放图版资源
- for ( int i = 0; i < 6; i++ )
- {
- image[i] = null;
- }
- break;
- }
- }
- }