关闭

自己总结的一个移动菜单方法,比较简陋哦

615人阅读 评论(0) 收藏 举报

应该有许多错误,,不过是自己的理解

Img _img;//需要建立Img类,读取图片文件
int _MenuX=32;//图象是333*96
_img= new Img("/menu.png", 5, 1, true);

void Update_Menu(Img _img,int _Menunum,int _MenuX,int _MenuY)
{
            Graphics g;     
            boolean _MenuMove;
            int menu;//临时记录菜单
            int _menuDir ;//控制方向

            _img.drawFrame(g, _kartX, _kartY, _kart,16+4);
            if(!_MenuMove && (_keyMap[4] > 0 || _keyMap[10] > 0))
            {
                _MenuMove = true;
                _menuDir = -1;//选择时菜单方向的控制1为向右,-1为向左
            } else
            if(!_MenuMove && (_keyMap[5] > 0 || _keyMap[12] > 0))
            {
                _MenuMove = true;
                _menuDir = 1;//选择时菜单方向的控制1为向右,-1为向左
            } else
            if(!_MenuMove)
                _menuDir = 0;
            if(_menuDir == -1)//选择时菜单方向的控制1为向右,-1为向左
            {
                _MenuX -= 20;//加载时选的3种车型的x位置,只是初位置
                if(_MenuX < -88)
                {
                    _MenuX = 172;
                    _menu--;
                    if(_menu< 0)
                        _menu = _Menunum;//一共几种菜单
                }
                if(_MenuX == 32)
                    _MenuMove = false;
            } else
            if(_menuDir == 1)
            {
                _MenuX += 20;
                if(_MenuX > 172)
                {
                    _MenuX = -88;
                    _menu++;
                    if(_menu >  _Menunum)
                        _menu= 0;
                }
                if(_MenuX == 32)
                    _MenuMove = false;
            }
            if(_ok)
            {
                _mainState =1;        //程序状态       
            }
            if(_back)
            {
                _menuState = 0;             
            }

img.java文件

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

public class Img
{

    static final int SCREENHEIGHT = 208;
    static final int SCREENWIDTH = 176;
    int dx;
    int dy;
    int he;
    Image im;
    Image im2;
    boolean loaded;
    String name;
    int px;
    int py;
    int wi;

    public Img(String s, int i, int j, boolean flag)
    {
        im2 = null;
        px = 1;
        py = 1;
        wi = 0;//显示图片的宽度
        he = 0;//显示图片的高度
        dx = 1;//图片所在的X坐标
        dy = 1;//图片所在的Y坐标
        name = s;//把图片的地址传过来
        px = i;//每行有几张图片
        py = j;//每列有几张图片
        im = null;//图片对象
        loaded = false;//没有加载
        if(flag)
            load();
    }

      void drawFrame(Graphics g, int i, int j, int k)
    {
        boolean flag = false;
        if(!loaded)
            load();
        if(loaded)
        {
            g.setClip(i, j, dx, dy);
            int l = k % px;
            if(l < 0)
                l = -l;
            int i1 = i - l * dx;
            int j1 = j - (k / px) * dy;
            if(flag)
                g.drawImage(im2, i1, j1, 0);
            else
                g.drawImage(im, i1, j1, 0);
        }
    }

    void drawFrame(Graphics g, int i, int j, int k, int l)
    {
        if(!loaded)
            load();
        if(loaded)
        {
            int i1 = 0;
            int j1 = 0;
            Graphics _tmp = g;
            if((l & 0x20) != 0)
                j1 = 204 - dy;
            Graphics _tmp1 = g;
            if((l & 0x8) != 0)
                i1 = 176 - dx;
            Graphics _tmp2 = g;
            if((l & 0x2) != 0)
                i1 = (176 - dx) / 2;
            Graphics _tmp3 = g;
            if((l & 0x1) != 0)
                j1 = (204 - dy) / 2;
            drawFrame(g, i1 + i, j1 + j, k);
        }
    }

    boolean load()
    {
        try
        {
            im = Image.createImage(name);
            wi = im.getWidth();
            he = im.getHeight();
            dx = wi / px;//图片宽度除以每行有几张照片,结果是单个图片的小宽度
            dy = he / py;//图像所在Y坐标
            loaded = true;
        }
        catch(Exception exception) { }
        return loaded;
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:40515次
    • 积分:777
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:4篇
    • 译文:0篇
    • 评论:11条
    最新评论