📄 slcanvas.java
字号:
/*
* 好好学习,天天向上。
*
* 作者: 牛潇
* MSN: niuxiao@msn.com
* E-Mail: gniuxiao@gmail.com
* QQ: 19528689
*
* 创建日期: 2006-2-20
*/
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* 主Canvas
*
* @author 牛潇
*/
public class slCanvas extends Canvas
{
public static final int STATUS_HEIGHT = 20; // 状态栏高度
private slController mc;
private int top, left; // 开始绘图的左上角坐标
private int width, height; // 全部绘图的宽和高
private int curRow, curCol; // 当前选中的行、列
private Image[] known = new Image[9];
private Image exploded, flag, mine, uncovered, unknown;
public slCanvas()
{
try {
this.known[0] = Image.createImage("/img/blank.png");
this.known[1] = Image.createImage("/img/1.png");
this.known[2] = Image.createImage("/img/2.png");
this.known[3] = Image.createImage("/img/3.png");
this.known[4] = Image.createImage("/img/4.png");
this.known[5] = Image.createImage("/img/5.png");
this.known[6] = Image.createImage("/img/6.png");
this.known[7] = Image.createImage("/img/7.png");
this.known[8] = Image.createImage("/img/8.png");
this.exploded = Image.createImage("/img/exploded.png");
this.flag = Image.createImage("/img/flag.png");
this.mine = Image.createImage("/img/mine.png");
this.uncovered = Image.createImage("/img/uncovered.png");
this.unknown = Image.createImage("/img/unknown.png");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public void startNewGame(slController mc)
{
this.mc = mc;
this.width = mc.col*15;
this.height = STATUS_HEIGHT + mc.row*15;
this.top = (getHeight()-height) / 2;
this.left = (getWidth()-width) / 2;
this.curRow = 0;
this.curCol = 0;
repaint();
}
/**
* @see javax.microedition.lcdui.Canvas#keyPressed(int)
*/
protected void keyPressed(int keyCode)
{
if (mc.gameState != mc.GAME_RUNNING)
return;
switch (getGameAction(keyCode)) {
case LEFT:
if (curCol != 0) --curCol;
else curCol = mc.col-1;
break;
case RIGHT:
if (curCol != mc.col-1) ++curCol;
else curCol = 0;
break;
case UP:
if (curRow != 0) --curRow;
else curRow = mc.row-1;
break;
case DOWN:
if (curRow != mc.row-1) ++curRow;
else curRow = 0;
break;
case GAME_A:
mc.uncover(curRow, curCol);
break;
case GAME_B:
mc.mark(curRow, curCol);
break;
default:;
}
repaint();
}
/**
* @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g)
{
// 清屏并画边框
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x777777);
g.drawRect(left-1, top-1, width+1, height+2);
g.drawLine(left, top+STATUS_HEIGHT, left+width, top+STATUS_HEIGHT);
// 画状态栏
g.drawString("Remaining Mine: "+mc.remainingMineCount(), left+5, top+2, g.TOP|g.LEFT);
// 画雷区
int i,j;
for (i=0; i<mc.row; ++i) {
for (j=0; j<mc.col; ++j) {
Image img = null;
int pt = mc.map[i][j];
int x = left + j*15;
int y = top + STATUS_HEIGHT + i*15 + 1;
if (mc.gameState == mc.GAME_RUNNING) {
if ((pt&mc.FLAG) != 0)
img = flag;
else if ((pt&mc.UNKNOWN) != 0)
img = unknown;
else if ((pt&mc.KNOWN) != 0)
img = known[mc.surroundingMineCount(i, j)];
else
img = uncovered;
}
else if (mc.gameState == mc.GAME_OVER) {
if (i==curRow && j==curCol)
img = exploded;
else if ((pt&mc.MINE) != 0)
img = mine;
else if ((pt&mc.FLAG) != 0)
img = flag;
else if ((pt&mc.UNKNOWN) != 0)
img = unknown;
else if ((pt&mc.KNOWN) != 0)
img = known[mc.surroundingMineCount(i, j)];
else
img = uncovered;
}
else if (mc.gameState == mc.GAME_WIN) {
if ((pt&mc.MINE) != 0)
img = flag;
else
img = known[mc.surroundingMineCount(i, j)];
}
else
throw new RuntimeException("unknown game state: " + mc.gameState);
g.drawImage(img, x, y, g.LEFT|g.TOP);
// 画当前选中部分
if (mc.gameState == mc.GAME_RUNNING) {
x = left + curCol*15;
y = top + STATUS_HEIGHT + curRow*15 + 1;
g.setColor(0xcc0000);
g.drawRect(x, y, 14, 14);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -