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

📄 menucanvas.java~9~

📁 一个手机游戏菜单登入界面
💻 JAVA~9~
字号:
package gamemidlet;

import javax.microedition.lcdui.*;

/**
 * <p>Title: 黄小辉8000106286网通062班</p>
 * <p>Description: GameMIDlet</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: NCU</p>
 * @author not attributable
 * @version 1.0
 */

public class MenuCanvas
    extends Canvas
    implements CommandListener {
  private String[] menuItem = {
      "开始游戏", "最高分", "选项", "游戏规则说明", "关于", "退出"};
  private Font menuFont;
  private int currentY = 0;
  private int currentIndex;
  private Image bckImg;
  private int[] rgbData;
  private int MenuHeight;
  private int topBottom;
  private int BarSpace = 3;
  private Command yesCmd, noCmd;
  public MenuCanvas() {
    this.setFullScreenMode(true);
     menuFont = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE);
    MenuHeight = menuFont.getHeight() + 4;
    topBottom = (this.getHeight() - menuItem.length * MenuHeight -
                 BarSpace * (menuItem.length - 1)) / 2;
    currentIndex = 1;
    bckImg = GameMIDlet.loadImage("/pic/Tank.png");
    rgbData = new int[bckImg.getWidth() * bckImg.getHeight()];
    bckImg.getRGB(rgbData, 0, bckImg.getWidth(), 0, 0, bckImg.getWidth(),
                  bckImg.getHeight());
    int alpha = 100;
    for (int i = 0; i < rgbData.length; i++) {
      rgbData[i] = (alpha << 24) | (rgbData[i] & 0x00FFFFFF);

    }
  }

  protected void paint(Graphics g) {
    g.setColor(0x00000000);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    int imgW = bckImg.getWidth();
    int imgH = bckImg.getHeight();
    int cols = (this.getWidth() / imgW) + 1;
    int rows = (this.getHeight() / imgH) + 1;
    for (int w = 0; w <= cols; w++) {
      for (int h = 0; h <= rows; h++) {
        int gx = w * imgW;
        int gy = h * imgH;
        g.translate(gx, gy);
        g.drawRGB(rgbData, 0, bckImg.getWidth(), 0, 0, bckImg.getWidth(),
                  bckImg.getHeight(), true);
        g.translate(0 - g.getTranslateX(), 0 - g.getTranslateY());
      }
    }
    int lineY = 0;
    for (int n = 1; n <= menuItem.length; n++) {
      lineY = n * MenuHeight + topBottom;
      g.setColor(0X0081ae08);
      g.fillRect(0, lineY, this.getWidth(), 2);
      g.fillRoundRect(MenuHeight, lineY - 8, this.getWidth() - 40,
                      MenuHeight - 2, 6, 6);
      g.setColor(0X00FFFFFF);
      g.setFont(menuFont);
      g.drawString(menuItem[n - 1], this.getWidth() / 2,
                   lineY - menuFont.getHeight() / 2 + 2, g.TOP | g.HCENTER);
    }
    setFocusItem(currentIndex, g);
  }

  private void getFocusIndex() {
    currentIndex = (currentY - topBottom) / MenuHeight;
  }

  private void setFocusItem(int itemIndex, Graphics g) {
    int lineY = 0;
    lineY = itemIndex * MenuHeight + topBottom;
    g.setColor(0x00d5fe67);
    g.fillRect(0, lineY, this.getWidth(), 2);
    g.fillRoundRect(MenuHeight, lineY - 8, this.getWidth() - 40, MenuHeight - 2,
                    6, 6);
    g.setColor(0x00FF0000);
    g.setFont(menuFont);
    g.drawString(menuItem[itemIndex - 1], this.getWidth() / 2,
                 lineY - menuFont.getHeight() / 2 + 2, g.TOP | g.HCENTER);
    currentY = lineY;
    getFocusIndex();
  }

  protected void keyPressed(int keyCode) {
    int keyAction = this.getGameAction(keyCode);
    switch (keyAction) {
      case UP:
        if (currentIndex > 1) {
          currentIndex = currentIndex - 1;
        }
        repaint();
        break;
      case DOWN:
        if (currentIndex >= 1 && currentIndex < menuItem.length) {
          currentIndex = currentIndex + 1;
        }
        repaint();
        break;
      case FIRE:
        switch (currentIndex) {
          case 1:
            showMessage("提示", "为此菜单项编写[" + menuItem[currentIndex - 1] + "]处理代码",
                        0);
            break;
          case 2:
            showMessage("提示", "为此菜单项编写[" + menuItem[currentIndex - 1] + "]处理代码",
                        0);
            break;
          case 3:
            showMessage("提示", "为此菜单项编写[" + menuItem[currentIndex - 1] + "]处理代码",
                        0);
            break;
          case 4:
            showMessage("提示", "为此菜单项编写[" + menuItem[currentIndex - 1] + "]处理代码",
                        0);
            break;
          case 5:
            AboutCanvas about = new AboutCanvas();
            Display.getDisplay(GameMIDlet.instance).setCurrent(about);
            break;
          case 6:
            showMessage("提示", "确定要退出游戏吗", 0);
            break;
        }
        break;
    }
  }

  protected void keyRepeated(int keyCode) {
    keyPressed(keyCode);
  }

  private void showMessage(String TitleStr, String message, int type) {
    yesCmd = new Command("是", Command.OK, 1);
    noCmd = new Command("否", Command.SCREEN, 1);
    Image logo = GameMIDlet.loadImage("/pic/Tank20.png");
    Alert aboutAlert = new Alert(TitleStr, message, logo,
                                 AlertType.CONFIRMATION);
    if (type == 0) {
      aboutAlert.setTimeout(3000);
    }
    else {
      aboutAlert.setTimeout(Alert.FOREVER);
      aboutAlert.addCommand(yesCmd);
      aboutAlert.addCommand(noCmd);
      aboutAlert.setCommandListener(this);
    }
    Display.getDisplay(GameMIDlet.instance).setCurrent(aboutAlert);
  }

  public void commandAction(Command c, Displayable displayable) {
    if (c == yesCmd) {
      GameMIDlet.quitApp();
    }
    if (c == noCmd) {
      Display.getDisplay(GameMIDlet.instance).setCurrent(this);
    }
  }

}

⌨️ 快捷键说明

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