站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题TilePuzzle剖析(2)

TilePuzzle剖析(2)

摘要: TilePuzzle剖析(2)
TilePuzzle剖析(2)

作者:

下面我们来分析一下Board类。

public Board(MIDlet midlet_) {

int i;
midlet = midlet_; //通过MIDlet应用程序中调用的Board类的构造函数传递来的参数,得到MIDlet类的一个实例
dpy = Display.getDisplay(midlet);//得到显示类
gridw = 4; //设定表格的宽和高
gridh = 4;

font = Font.getFont(Font.FACE_SYSTEM, //设定字体
Font.STYLE_PLAIN, Font.SIZE_MEDIUM);

// REMIND update when font metrics info gets implemented
cellw = font.charWidth('M') + 7; //得到行的宽高
cellh = font.getHeight() + 1;
cellxoff = 3; //和行的偏移
cellyoff = 0;

gridx = (getWidth() - (gridw*cellw) + 1) / 2; //设定表格的绘制起始位置
gridy = 10;

cheated = false;
rand = new Random();

// create the grid arrays 创建表格数组,这里使用2维数组,宽高为gridw,gridh.

grid = new Piece[gridw][];
for (i = 0; i < gridw; i++) {
grid[i] = new Piece[gridh];
}
//创建一个1维数组,大小为gridw*gridh
all = new Piece[gridw*gridh];
for (i = 0; i < (gridw*gridh)-1; i++) {
int x = i % gridw;
int y = i / gridw;
String s = letters.substring(i, i+1);//得到要显示的字符
grid[x][y] =
all[i] =
new Piece(s, i, x, y, i < (gridw*gridh/2));//初始化两个数组,并对应着一个Piece类实例。
}
// make the special blank piece设置数组的最后一块为空方块

blankp = new Piece(null, gridw*gridh-1, gridw-1, gridh-1, false);
grid[gridw-1][gridh-1] = blankp;
all[gridw*gridh-1] = blankp;

// set up commands设置命令
他的变量定义在类的开始处
如下所示:
// commands
static final int CMD_ABOUT = 0;
static final int CMD_EXIT = 1;
static final int CMD_OPTIONS = 2;
static final int CMD_RESET = 3;
static final int CMD_START = 4;
static final int CMD_UNLOCK = 5;
static final int CMD_ZLAST = 6; // must be ze last, of course
Command cmd[];
所以

cmd = new Command[CMD_ZLAST];//分配数组,用来容纳所有的命令
//数组的第一个位置放置about

cmd[CMD_ABOUT] =
new BoardCommand("About", Command.HELP, 5, CMD_ABOUT);
//第二个位置放置exit
cmd[CMD_EXIT] =
new BoardCommand("Exit", Command.EXIT, 6, CMD_EXIT);
//第三个位置放置options
cmd[CMD_OPTIONS] =
new BoardCommand("Options", Command.SCREEN, 3, CMD_OPTIONS);
//第四个位置放置reset
cmd[CMD_RESET] =
new BoardCommand("Reset", Command.SCREEN, 1, CMD_RESET);
//第五个位置放置start
cmd[CMD_START] =
new BoardCommand("Start", Command.SCREEN, 1, CMD_START);
//第六个位置放置unlock
cmd[CMD_UNLOCK] =
new BoardCommand("Unlock", Command.SCREEN, 4, CMD_UNLOCK);
其中设置命令时调用了BoardCommand类的构造函数。
class BoardCommand extends Command {
int tag;

BoardCommand(String label, int type, int pri, int tag_) {
super(label, type, pri);
tag = tag_;
}
}
该函数很简单其中一个成员变量tag和一个构造函数,并调用Command类的构造函数来建立命令。

// set up the listener命令建立好之后要设置监听
setCommandListener(this);
// set up options screen启动选项屏幕,这一部分以后会讲道
options = new Options(dpy, this);
// set up initial state启动初始化状态。
setState(INITIALIZED);
}




2006-11-15 02:37 PM

↑返回目录
前一篇: TilePuzzle剖析(3)
后一篇: 使用双缓冲区