📄 minecanvas.java
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi
// Source File Name: MineCanvas.java
package com.innograte.j2me.games.minefield;
import java.util.Random;
import javax.microedition.lcdui.*;
// Referenced classes of package com.innograte.j2me.games.minefield:
// MinefieldMidlet, MinesAnimator, HighScore
public class MineCanvas extends Canvas
implements CommandListener
{
public static byte NUM_OF_MINES[] = {
10, 40, 99
};
public static byte NUM_CELL_X[] = {
9, 16, 30
};
public static byte NUM_CELL_Y[] = {
9, 16, 16
};
public byte curNumOfMines;
public byte curNumCellsX;
public byte curNumCellsY;
public static final byte UNKNOWN = -1;
public static final byte FLAGGED = 16;
public static final byte UNCOVERED = 0;
private byte flagPosition[][];
public static final byte CLEAR = 3;
public static final byte MINE = 4;
private byte minePosition[][];
public static final byte PLAYING = 5;
public static final byte GAMEOVER = 6;
public static final byte COMPLETED = 7;
private Command quitCommand;
private Command restartCommand;
private short canvasWidth;
private short canvasHeight;
private short viewStartX;
private short viewStartY;
private short viewWidth;
private short viewHeight;
private short halfViewWidth;
private short halfViewHeight;
private short boardWidth;
private short boardHeight;
private short boardStartX;
private short boardStartY;
private short boardEndX;
private short boardEndY;
private short cellWidth;
private short cellHeight;
private short halfCellWidth;
private short halfCellHeight;
private short hudStartX;
private short hudStartY;
private short hudWidth;
private short hudHeight;
private Random rand;
private Font font;
private Image button;
private Image numberImages[];
private Image flagImage;
private Image mineImage;
private Image boomImage;
private Image boardImage;
private Graphics boardImageGraphics;
private Image frameBuffer;
private Graphics frameBufferGraphics;
private MinefieldMidlet minesMidlet;
private byte selectionX;
private byte selectionY;
public byte currentMode;
private byte bombsLeft;
public int timeElapsed;
public long lastEventTime;
public long lastSelectionBoxDrawTime;
public boolean updateHud;
public boolean drawSelectionBox;
public int inputBuffer;
public int played;
public MineCanvas(MinefieldMidlet aMinesMidlet)
throws Exception
{
quitCommand = new Command("Menu", 6, 3);
restartCommand = new Command("Restart", 1, 1);
rand = new Random();
lastEventTime = 0L;
lastSelectionBoxDrawTime = 0L;
inputBuffer = -1;
played = 0;
minesMidlet = aMinesMidlet;
curNumOfMines = NUM_OF_MINES[minesMidlet.difficultyLevel];
curNumCellsX = NUM_CELL_X[minesMidlet.difficultyLevel];
curNumCellsY = NUM_CELL_Y[minesMidlet.difficultyLevel];
currentMode = 5;
canvasWidth = (short)getWidth();
canvasHeight = (short)getHeight();
flagPosition = new byte[curNumCellsX][curNumCellsY];
minePosition = new byte[curNumCellsX][curNumCellsY];
button = Image.createImage("/images/uncovered.png");
flagImage = Image.createImage("/images/flag.png");
mineImage = Image.createImage("/images/mine.png");
boomImage = Image.createImage("/images/boom.png");
numberImages = new Image[9];
int i;
for(i = 0; i < 9; i++)
numberImages[i] = Image.createImage(String.valueOf(String.valueOf((new StringBuffer("/images/")).append(i).append(".png"))));
font = Font.getFont(0, 0, 8);
cellWidth = (short)button.getWidth();
cellHeight = (short)button.getHeight();
halfCellWidth = (short)(cellWidth / 2);
halfCellHeight = (short)(cellHeight / 2);
boardWidth = (short)(cellWidth * curNumCellsX + 1);
boardHeight = (short)(cellHeight * curNumCellsY + 1);
hudHeight = (short)font.getHeight();
viewStartX = 0;
viewWidth = canvasWidth;
hudWidth = viewWidth <= boardWidth ? viewWidth : boardWidth;
if(boardHeight >= canvasHeight - hudHeight)
{
viewStartY = 0;
viewHeight = (short)(canvasHeight - hudHeight);
} else
{
viewStartY = (short)((canvasHeight - boardHeight - hudHeight) / 2);
viewHeight = boardHeight;
}
halfViewWidth = (short)(viewWidth / 2);
halfViewHeight = (short)(viewHeight / 2);
hudStartX = viewStartX;
hudStartY = (short)(viewStartY + viewHeight);
selectionX = 0;
selectionY = 0;
i = selectionY * cellWidth;
if(boardWidth <= canvasWidth)
boardStartX = (short)((canvasWidth - boardWidth) / 2);
else
if(i + cellWidth / 2 <= halfViewWidth)
boardStartX = 0;
else
if(i + cellWidth / 2 >= boardWidth - halfViewWidth)
boardStartX = (short)(viewWidth - boardWidth);
else
boardStartX = (short)(halfViewWidth - i - cellWidth / 2);
hudStartX = boardStartX;
int j = selectionY * cellHeight;
if(boardHeight <= canvasHeight - hudHeight)
boardStartY = (short)((canvasHeight - boardHeight - hudHeight) / 2);
else
if(j + cellHeight / 2 <= halfViewHeight)
boardStartY = 0;
else
if(j + cellHeight / 2 >= boardHeight - halfViewHeight)
boardStartY = (short)(viewHeight - boardHeight);
else
boardStartY = (short)(halfViewHeight - j - cellHeight / 2);
boardEndX = (short)(boardStartX + boardWidth);
boardEndY = (short)(boardStartY + boardHeight);
boardImage = Image.createImage(boardWidth, boardHeight);
boardImageGraphics = boardImage.getGraphics();
frameBuffer = Image.createImage(canvasWidth, canvasHeight);
frameBufferGraphics = frameBuffer.getGraphics();
frameBufferGraphics.setFont(font);
frameBufferGraphics.setColor(255, 255, 255);
frameBufferGraphics.fillRect(0, 0, canvasWidth, canvasHeight);
bombsLeft = curNumOfMines;
timeElapsed = 0;
resetPosition();
setCommandListener(this);
addCommand(quitCommand);
repaintAll();
}
protected void showNotify()
{
lastEventTime = System.currentTimeMillis();
minesMidlet.animator.enable();
}
public void commandAction(Command command, Displayable displayable)
{
if(command == quitCommand)
MinefieldMidlet.restart();
else
if(command == restartCommand)
{
currentMode = 5;
inputBuffer = -1;
played = 0;
selectionX = 0;
selectionY = 0;
bombsLeft = curNumOfMines;
timeElapsed = 0;
resetPosition();
frameBufferGraphics.setColor(255, 255, 255);
frameBufferGraphics.fillRect(0, 0, canvasWidth, canvasHeight);
removeCommand(restartCommand);
repaintAll();
lastEventTime = System.currentTimeMillis();
minesMidlet.animator.enable();
}
}
public synchronized void updateWorld()
{
isShown();
updateHud = false;
if(processPlayerInput(inputBuffer))
{
drawSelectionBox = true;
lastSelectionBoxDrawTime = System.currentTimeMillis();
} else
if(System.currentTimeMillis() - lastSelectionBoxDrawTime >= 500L)
{
drawSelectionBox = !drawSelectionBox;
lastSelectionBoxDrawTime = System.currentTimeMillis();
}
if(currentMode != 5)
return;
int y = selectionY * cellHeight + cellHeight / 2;
if(boardEndY > hudStartY && y + boardStartY > halfViewHeight)
{
boardStartY -= cellHeight / 3;
boardEndY = (short)(boardStartY + boardHeight);
if(boardEndY < hudStartY)
{
boardEndY = hudStartY;
boardStartY = (short)(boardEndY - boardHeight);
} else
if(y + boardStartY < halfViewHeight)
{
boardStartY = (short)(halfViewHeight - y);
boardEndY = (short)(boardStartY + boardHeight);
}
} else
if(boardStartY < 0 && y + boardStartY < halfViewHeight)
{
boardStartY += cellHeight / 3;
if(boardStartY > 0)
boardStartY = 0;
else
if(y + boardStartY > halfViewHeight)
boardStartY = (short)(halfViewHeight - y);
boardEndY = (short)(boardStartY + boardHeight);
}
int x = selectionX * cellWidth + cellWidth / 2;
if(boardEndX > viewStartX + viewWidth && x + boardStartX > halfViewWidth)
{
boardStartX -= cellWidth / 3;
boardEndX = (short)(boardStartX + boardWidth);
if(boardEndX < viewStartX + viewWidth)
{
boardEndX = (short)(viewStartX + viewWidth);
boardStartX = (short)(boardEndX - boardWidth);
} else
if(x + boardStartX < halfViewWidth)
{
boardStartX = (short)(halfViewWidth - x);
boardEndX = (short)(boardStartX + boardWidth);
}
} else
if(boardStartX < 0 && x + boardStartX < halfViewWidth)
{
boardStartX += cellWidth / 3;
if(boardStartX > 0)
boardStartX = 0;
else
if(x + boardStartX > halfViewWidth)
boardStartX = (short)(halfViewWidth - x);
boardEndX = (short)(boardStartX + boardWidth);
}
frameBufferGraphics.setClip(viewStartX, viewStartY, viewWidth, viewHeight);
frameBufferGraphics.drawImage(boardImage, boardStartX, boardStartY, 20);
if(drawSelectionBox)
frameBufferGraphics.setColor(0, 0, 0);
else
frameBufferGraphics.setColor(255, 255, 255);
frameBufferGraphics.drawRect(selectionX * cellWidth + boardStartX, selectionY * cellHeight + boardStartY, cellWidth - 1, cellHeight - 1);
long now = System.currentTimeMillis();
int k = (int)(now - lastEventTime);
int i1 = k / 1000;
if(i1 >= 1)
{
lastEventTime = now - (long)(k % 1000);
timeElapsed += i1;
updateHud = true;
}
if(updateHud)
{
frameBufferGraphics.setClip(viewStartX, viewStartY, viewWidth, viewHeight + hudHeight);
frameBufferGraphics.setClip(0, 0, canvasWidth, canvasHeight);
repaintHud();
repaint(viewStartX, viewStartY, viewWidth, viewHeight + hudHeight);
} else
{
repaint(viewStartX, viewStartY, viewWidth, viewHeight);
}
}
public void resetPosition()
{
for(int x = 0; x < curNumCellsX; x++)
{
for(int y = 0; y < curNumCellsY; y++)
{
flagPosition[x][y] = -1;
minePosition[x][y] = 3;
}
}
for(int i = 0; i < curNumOfMines; i++)
{
int x;
int y;
do
{
int n = Math.abs(rand.nextInt() % (curNumCellsX * curNumCellsY));
x = n % curNumCellsX;
y = n / curNumCellsX;
} while(minePosition[x][y] == 4);
minePosition[x][y] = 4;
}
boardImageGraphics.setColor(0, 0, 0);
boardImageGraphics.drawRect(0, 0, boardWidth - 1, boardHeight - 1);
for(int nx = 0; nx < curNumCellsX; nx++)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -