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

📄 menu.java

📁 很不错的泡泡堂手机游戏
💻 JAVA
字号:
/*
 * 创建日期 2007-2-20 @author cpiz
 * 由源码爱好者_www.codefans.net整理下载 
 * TODO
 */
package com.cpiz.poptang;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Menu extends Canvas
{
    private PopTang midlet = null;

    // 图形
    private Image imgMenuBg = null; // 菜单背景

    private Image imgCursor = null; // 光标图形

    // 画面绘制锚点
    private int anchorX = 0;

    private int anchorY = 0;

    Font font = null;

    // 屏幕宽与高
    private int screenWidth = 0;

    private int screenHeight = 0;

    // 当前选择项索引
    private int selectedIndex = 0;

    // 动画效果Timer
    Timer timer = null;

    // 动画效果计时标记
    private boolean wink = true;
    
    private Help help = null;
    private Help about = null;

    /**
     * 菜单构造函数
     * @param midlet 游戏MIDlet
     */
    public Menu(PopTang midlet)
    {
        this.midlet = midlet;
        this.setFullScreenMode(true);

        screenWidth = getWidth();
        screenHeight = getHeight();

        anchorX = (screenWidth - PopTang.GAME_WIDTH) / 2;
        anchorY = (screenHeight - PopTang.GAME_HEIGHT) / 2;

        font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);

        try
        {
            imgMenuBg = Image.createImage(PopTang.IMAGE_SRC_MENU);
            imgCursor = Image.createImage(PopTang.IMAGE_SRC_CURSOR);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

        timer = new Timer();
        timer.schedule(new TimerTask()
        {
            public void run()
            {
                repaint();                
                wink = !wink;
            }
        }, PopTang.MENU_WINK_TIME, PopTang.MENU_WINK_TIME);
    }

    /**
     * 清除屏幕
     * @param g 屏幕对象
     */
    private void clearGraphics(Graphics g)
    {
        int lastColor = g.getColor();
        g.setColor(0xFFFFFF);// 默认清理为白色
        g.fillRect(0, 0, screenWidth, screenHeight);
        g.setColor(lastColor);
    }
    
    /**
     * 显示菜单
     */
    protected void showMe()
    {
        midlet.setDisplayable(this);
        midlet.ncuscSplash = null;
        midlet.splash = null;
        System.gc();
    }
    
    protected void paint(Graphics g)
    {
        clearGraphics(g);
        g.setFont(font);
        
        int x = screenWidth / 2;
        int y = 0;

// 绘制菜单背景
g.drawImage(imgMenuBg, anchorX, anchorY, Graphics.LEFT | Graphics.TOP);
       
        // 绘制菜单内容
        g.setColor(PopTang.COLOR_YELLOW);
        y = anchorY + PopTang.MENU_Y_START;
        for (int i = 0; i < PopTang.MENU_OPTIONS.length; i++)
        {
            // 计算菜单项Y轴位置,底对齐
            y += (font.getHeight() + PopTang.MENU_ROW_SPACING);
            
            // 绘制菜单项
            g.drawString(PopTang.MENU_OPTIONS[i], x, y, Graphics.HCENTER
                    | Graphics.BOTTOM);
            
            // 绘制光标
            if (i == selectedIndex)
            {
                // 计算偏移以显示光标动画效果
                int offset = wink ? 0 : PopTang.CELL_WIDTH;
        
                g.setClip(x - PopTang.MENU_CURSOR_SPACING, y - PopTang.CELL_HEIGHT,
                        PopTang.CELL_WIDTH, PopTang.CELL_HEIGHT);
                g.drawImage(imgCursor, x - PopTang.MENU_CURSOR_SPACING - offset,
                        y, Graphics.LEFT | Graphics.BOTTOM);
                g.setClip(0, 0, screenWidth, screenHeight);
            }           
        }
    }

    protected void keyPressed(int key)
    {       
        // 按键甄别依据Nokia 6070键位码
        switch (getGameAction(key))
        {        
        case Canvas.UP:// 向上
        case Canvas.KEY_NUM2:// 2键
            selectedIndex = (selectedIndex + PopTang.MENU_OPTIONS.length - 1)
                    % PopTang.MENU_OPTIONS.length;
            repaint();
            break;
        case Canvas.DOWN: // 向下
        case Canvas.KEY_NUM8: // 8键
            selectedIndex = ++selectedIndex % PopTang.MENU_OPTIONS.length;
            repaint();
            break;
        case Canvas.FIRE: // 中键
        case Canvas.KEY_NUM5: // 5键
            goMenu();
            break;
        default:
            break;
        }
    }
    
    /**
     * 执行菜单选择
     *
     */
    private void goMenu()
    {
        switch(selectedIndex)
        {
        case 0:
            midlet.game.startPlay(1);
            midlet.setDisplayable(midlet.game);
            break;
        case 1:
            help = new Help(Help.HELP, midlet, this);
            midlet.setDisplayable(help);
            break;
        case 2:
            about = new Help(Help.ABOUT, midlet, this);
            midlet.setDisplayable(about);
            break;
        case 3: // 退出游戏
            midlet.quitGame();
        default:
            break;
        }
    }
}

⌨️ 快捷键说明

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