⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menu.java

📁 初学者的佳音 初学者的佳音 初学者的佳音 初学者的佳音 初学者的佳音
💻 JAVA
字号:
package game;

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;
import lib.*;

public class Menu extends Frame implements CommandListener{

  public static final int MENU_MAIN=0;
  public static final int MENU_HISCORE=1;
  public static final int MENU_SETTING=2;

  private Resource resource=Resource.getResource();
  private Game game;
  private int mainMenuIndex=0;
  private int mainMenuCount=7;
  private int settingMenuIndex=0;
  private int settingMenuCount=2;
  private Command cmd=new Command("返回",Command.CANCEL,0);
  private Form helpForm=new Form("操作说明");
  private Form aboutForm=new Form("关于游戏");
  private int currMenu;

  private Font font=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
  private String strHiscore="游戏积分";
  private String strSetting="游戏设置";

  public void commandAction(Command c, Displayable d){
    Display.getDisplay(MyMIDlet.midlet).setCurrent(MyCanvas.xcanvas);
  }
  public Menu(){
    helpForm.append(resource.helpString);
    helpForm.setCommandListener(this);
    helpForm.addCommand(cmd);
    aboutForm.append(resource.aboutString);
    aboutForm.setCommandListener(this);
    aboutForm.addCommand(cmd);
    resource.helpString=null;
    resource.aboutString=null;
  }
  public void setGame(Game game){
    this.game=game;
  }
  //画主菜单
  public void paintMainMenu(Graphics g){
    //画框
    for(int i=0;i<mainMenuCount;i++){
      if(i==mainMenuIndex){
        Toolkit.drawRegion(g,resource.imgMenuBoard,0,16,70,15,(XCanvas.WIDTH-resource.imgMenuBoard.getWidth())/2,
                         (XCanvas.HEIGHT-resource.imgMenuText.getHeight())/2+i*17);
      }
      else{
        Toolkit.drawRegion(g,resource.imgMenuBoard,0,0,70,15,(XCanvas.WIDTH-resource.imgMenuBoard.getWidth())/2,
                         (XCanvas.HEIGHT-resource.imgMenuText.getHeight())/2+i*17);
      }
    }
    //画文字
    g.drawImage(resource.imgMenuText,(XCanvas.WIDTH-resource.imgMenuText.getWidth())/2,
                (XCanvas.HEIGHT-resource.imgMenuText.getHeight())/2,0);
    //画标签
    Toolkit.drawRegion(g,resource.imgText,1,104,47,19,10,XCanvas.HEIGHT-19-10);
//    Toolkit.drawRegion(g,resource.imgText,0,124,47,19,XCanvas.WIDTH-47-10,XCanvas.HEIGHT-19-10);
  }

  //画排行榜菜单
  public void paintHiscoreMenu(Graphics g){
    g.setColor(255,192,0);
    g.drawRect((XCanvas.WIDTH-120)/2,(XCanvas.HEIGHT-150)/2,120,150);
    g.setColor(0,0,0);
    g.fillRect((XCanvas.WIDTH-120)/2+1,(XCanvas.HEIGHT-150)/2+1,119,149);
    g.setColor(255,255,255);
    g.drawString(strHiscore,(XCanvas.WIDTH-font.stringWidth(strHiscore))/2,35,0);
    for(int i=0;i<8;i++){
      g.drawString("No. "+(i+1),40,60+i*15,0);
      g.drawString(""+resource.data[i],100,60+i*15,0);
    }
//    Toolkit.drawRegion(g,resource.imgText,1,104,47,19,10,XCanvas.HEIGHT-19-10);
    Toolkit.drawRegion(g,resource.imgText,0,124,47,19,XCanvas.WIDTH-47-10,XCanvas.HEIGHT-19-10);
  }
  public void paintSetting(Graphics g){
    g.setColor(255,192,0);
    g.drawRect((XCanvas.WIDTH-120)/2,(XCanvas.HEIGHT-60)/2,120,60);
    g.setColor(0,0,0);
    g.fillRect((XCanvas.WIDTH-120)/2+1,(XCanvas.HEIGHT-60)/2+1,120-1,60-1);
    g.setColor(255,255,255);
    g.drawString(strSetting,(XCanvas.WIDTH-font.stringWidth(strSetting))/2,80,0);
    g.drawString("音乐",(XCanvas.WIDTH-120)/2+5,(XCanvas.HEIGHT-font.getHeight())/2+10,0);
    if(settingMenuIndex==0){
      g.setColor(255,255,255);
      g.drawString("开",(XCanvas.WIDTH-120)/2+60,(XCanvas.HEIGHT-font.getHeight())/2+10,0);
      g.setColor(128,128,128);
      g.drawString("关",(XCanvas.WIDTH-120)/2+90,(XCanvas.HEIGHT-font.getHeight())/2+10,0);
    }
    else{
      g.setColor(128,128,128);
      g.drawString("开",(XCanvas.WIDTH-120)/2+60,(XCanvas.HEIGHT-font.getHeight())/2+10,0);
      g.setColor(255,255,255);
      g.drawString("关",(XCanvas.WIDTH-120)/2+90,(XCanvas.HEIGHT-font.getHeight())/2+10,0);
    }

    Toolkit.drawRegion(g,resource.imgText,1,104,47,19,10,XCanvas.HEIGHT-19-10);
    Toolkit.drawRegion(g,resource.imgText,0,124,47,19,XCanvas.WIDTH-47-10,XCanvas.HEIGHT-19-10);
  }
  public void paint(Graphics g){
    //画背景
    g.drawImage(resource.imgMenuBack,(XCanvas.WIDTH-resource.imgMenuBack.getWidth())/2,
                  (XCanvas.HEIGHT-resource.imgMenuBack.getHeight())/2,0);
    g.setFont(font);

    switch(currMenu){
      case MENU_MAIN:
        paintMainMenu(g);
        break;
      case MENU_HISCORE:
        paintHiscoreMenu(g);
        break;
      case MENU_SETTING:
        paintSetting(g);
        break;
    }
  }
  public void keyPressed(int keyCode){
    switch(currMenu){
      case MENU_MAIN:
        switch(keyCode){
        case XCanvas.KEY_UP:
        case XCanvas.KEY_NUM2:
          mainMenuIndex=(mainMenuIndex-1+mainMenuCount)%mainMenuCount;
          break;
        case XCanvas.KEY_DOWN:
        case XCanvas.KEY_NUM8:
          mainMenuIndex=(mainMenuIndex+1)%mainMenuCount;
          break;
        case XCanvas.KEY_SOFT1:
          switch(mainMenuIndex){
          //新游戏
          case 0:
            resource.releaseMenu();
            game.newGame();
            getFrameManager().setCurrFrame(game);
            break;
          //继续游戏
          case 1:
            resource.releaseMenu();
            game.continueGame();
            getFrameManager().setCurrFrame(game);
            break;
          //操作说明
          case 2:
            Display.getDisplay(MyMIDlet.midlet).setCurrent(helpForm);
            break;
          //游戏设置
          case 3:
            currMenu=MENU_SETTING;
            break;
          //游戏积分
          case 4:
            resource.readData();
            currMenu=MENU_HISCORE;
            break;
          //关于游戏
          case 5:
            if(resource.isMIPD2_0){
              resource.openMedia.stop();
              resource.backMedia.stop();
              resource.winMedia.stop();
              resource.loseMedia.stop();
            }
            Display.getDisplay(MyMIDlet.midlet).setCurrent(aboutForm);
            break;
          //退出游戏
          case 6:
            MyMIDlet.midlet.notifyDestroyed();
            break;
          }
          break;
        }
        break;
      case MENU_HISCORE:
        switch(keyCode){
        case XCanvas.KEY_SOFT2:
          currMenu=MENU_MAIN;
          break;
        }
        break;
      case MENU_SETTING:
        switch(keyCode){
          case XCanvas.KEY_LEFT:
          case XCanvas.KEY_RIGHT:
            settingMenuIndex=settingMenuIndex==0?1:0;
            break;
          case XCanvas.KEY_SOFT2:
            currMenu=MENU_MAIN;
            break;
          case XCanvas.KEY_SOFT1:
            resource.sound=settingMenuIndex==0?true:false;
            currMenu=MENU_MAIN;
            break;
        }
        break;
    }
  }
  public void showNotify(){
    currMenu=MENU_MAIN;
    mainMenuIndex=0;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -