📄 bombscanvas.java
字号:
private boolean isValidIndex(int i, int j)
{
return (i>=0 && i<xCount && j>=0 && j<yCount);
}
private Cell getFocusCell()
{
return getCell(getFocusCellIndex());
}
private Cell getCell(int index)
{
try
{
return (Cell)cells.elementAt(index);
}
catch(Exception e)
{
return null;
}
}
private Cell getCell(int i, int j)
{
return getCell(getCellIndex(i,j));
}
private int getCellIndex(int i, int j)
{
if(!isValidIndex(i, j)) return -1;
return (xCount * j) + i;
}
private int getFocusCellIndex()
{
return getCellIndex(xFocus, yFocus);
}
static int paintCount = 0;
public void paint(Graphics g)
{
//MyLog.log("paint "+paintCount++);
if(paintBuffered)
{
g.drawImage(memImage, 0,0, g.TOP|g.LEFT);
}
else
{
Graphics memG = memImage.getGraphics();
paintAllCells(memG); //绘制到缓冲区中
g.drawImage(memImage, 0,0, g.TOP|g.LEFT);
paintBuffered = true;
}
}
private void paintAllCells(Graphics g)
{
clear(g);
cx = (getWidth()-xMargin*2) / xCount;
cy = (getHeight()-yMargin*2) / yCount;
int xLen = cx * xCount;
int yLen = cy * yCount;
xMargin = (getWidth() - xLen) / 2;
yMargin = (getHeight() - yLen) / 2;
int x, y;
g.setColor(0x0000ff);
g.setStrokeStyle(g.SOLID);
g.drawRect(xMargin-1, yMargin-1, xLen+2, yLen+2);
//画水平线
x = xMargin; y = yMargin;
for(int i=0; i<yCount+1; i++)
{
g.drawLine(x,y, x+xLen, y);
y += cy;
}
//画垂直线
x = xMargin; y = yMargin;
for(int i=0; i<xCount+1; i++)
{
g.drawLine(x,y, x, y+yLen);
x += cx;
}
//画Cells
Cell cell = null;
Rect rect = null;
for(int i=0; i<xCount; i++)
{
for(int j=0; j<yCount; j++)
{
cell = getCell(getCellIndex(i, j));
rect = getCellRect(i, j);
cell.paint(g, rect, false);
}
}
//画选中之的Cell
cell = getFocusCell();
rect = getCellRect(xFocus, yFocus);
cell.paint(g, rect, true);
if(isGameOver() || isWin())
{
//todo: 以半透明的形式显示背景区域
//g.setColor(0xffffff); //white
//g.fillRect(0, getHeight()/3, getWidth(), getHeight()/3);
g.setFont(gaveOverFont);
g.setColor(0x0); //black
Cell.paintStringInRect(g, canvasRect, isGameOver() ? "游戏结束!" : "小子你赢了!");
}
}
static Rect tempCellRect = new Rect();
private Rect getCellRect(int i, int j)
{
//Rect tempCellRect = new Rect();
tempCellRect.left = xMargin + 1 + cx*i;
tempCellRect.right = tempCellRect.left + cx - 1;
tempCellRect.top = yMargin + 1 + cy*j;
tempCellRect.bottom = tempCellRect.top + cy - 1;
return tempCellRect;
}
private void paintCellToMemImage(int i, int j, boolean focus)
{
Graphics g = memImage.getGraphics();
Cell cell = getCell(i, j);
Rect rect = getCellRect(i, j);
cell.paint(g, rect, focus);
}
public void keyPressed(int keyCode)
{
//MyLog.log("keyPressed()");
if(isGameOver() || isWin()) return;
Cell cell = null;
boolean bNeedUpdateInfoCommandLabel = false;
paintCellToMemImage(xFocus, yFocus, false); //取消原焦点
if(keyCode == KEY_NUM2 || getGameAction(keyCode) == UP) //上
{
if(yFocus>0) yFocus--; else yFocus=yCount-1;
}
else if(keyCode == KEY_NUM4 || getGameAction(keyCode) == LEFT) //左
{
if(xFocus>0) xFocus--; else xFocus=xCount-1;
}
else if(keyCode == KEY_NUM6 || getGameAction(keyCode) == RIGHT) //右
{
if(xFocus<xCount-1) xFocus++; else xFocus=0;
}
else if(keyCode == KEY_NUM8 || getGameAction(keyCode) == DOWN) //下
{
if(yFocus<yCount-1) yFocus++; else yFocus=0;
}
else if(keyCode == KEY_NUM1 || keyCode == KEY_NUM5 || getGameAction(keyCode) == FIRE)
{
cell = getCell(xFocus, yFocus);
boolean bOldIsFlag = cell.isFlag(); //get it befor openCell()
openCell(xFocus, yFocus);
isWin = checkIsWin();
if(isWin) paintBuffered = false;
if(bOldIsFlag)
{
bombLeftCount++;
bNeedUpdateInfoCommandLabel = true;
}
}
else if(keyCode == KEY_NUM3)
{
cell = getFocusCell();
if(!cell.isOpen())
{
//Flag it, or unFlag it
cell.flagIt(!cell.isFlag());
bombLeftCount = bombLeftCount + (cell.isFlag() ? -1 : 1);
bNeedUpdateInfoCommandLabel = true;
}
}
paintCellToMemImage(xFocus, yFocus, true); //画新的焦点
repaint();
if(bNeedUpdateInfoCommandLabel) setInfoCommandLabel();
}
private void setInfoCommandLabel()
{
if(isWin() || isGameOver())
setInfoCommandLabel("新局");
else
setInfoCommandLabel("雷数: "+String.valueOf(bombLeftCount));
}
private void setInfoCommandLabel(String label)
{
removeCommand(infoCommand);
infoCommand = new Command(label, Command.CANCEL, 0);
addCommand(infoCommand);
}
private void gameOver()
{
MyLog.log("Game Over!");
isGameOver = true;
paintBuffered = false;
repaint();
setInfoCommandLabel();
}
public boolean isGameOver()
{
return isGameOver;
}
public boolean isWin()
{
return isWin;
}
public int getDefaultOpenCount()
{
return defaultOpenCount;
}
public void setDefaultOpenCount(int n)
{
int max = getCellCount() / 3;
defaultOpenCount = (n <= max ? n : max);
}
//just copy from cldc1.1 source code
class MyRandom extends Random
{
public MyRandom()
{
setSeed(System.currentTimeMillis());
}
public int nextInt(int n)
{
if (n<=0)
return 0; //throw new IllegalArgumentException("n must be positive");
if ((n & -n) == n) // i.e., n is a power of 2
return (int)((n * (long)next(31)) >> 31);
int bits, val;
do
{
bits = next(31);
val = bits % n;
}while(bits - val + (n-1) < 0);
return val;
}
}
public int getXCount()
{
return xCount;
}
public void setXCount(int value)
{
xCount = value;
}
public int getYCount()
{
return yCount;
}
public void setYCount(int value)
{
yCount = value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -