?? btfircanvas.java
字號:
import java.io.IOException;
import javax.microedition.lcdui.*;
public class BTFIRCanvas extends Canvas
implements CommandListener
{
private BTFIRMIDlet midlet;
private Command CMD_STOP;
private Command CMD_NEW;
private BTFIRLogic logic;
private int boardSize=15;
private boolean isBlackFirst=true;
private int canvasWidth;
private int canvasHeight;
private int cWidth;
private int cHeight;
private Font font;
private int fontWidth;
private int fontHeight;
private int boardX;
private int boardY;
private int boardLength;
private int gridLength;
private int stoneLength;
private int statusY;
private String status;
private int statusColor;
private boolean isUpSide;
private final Image imgStatus[] = new Image[4];
private int statusImage;
private boolean isColor;
private static int BLACK =0;
private static int WHITE =0xffffff;
private boolean isBlack=true;
private boolean isServer=true;
private boolean isMyTurn=true;
private boolean isChangeable =true;
private Connection connection;
public BTFIRCanvas(BTFIRMIDlet m,boolean isServer)
{
this.midlet = m;
this.isServer = isServer;
isMyTurn = !isServer;
isBlack = !isServer;
CMD_STOP = new Command("退出", 2, 1);
addCommand(CMD_STOP);
CMD_NEW = new Command("重玩", 2, 1);
setCommandListener(this);
canvasWidth = getWidth();
canvasHeight = getHeight();
status = "";
statusColor = 0;
isUpSide = true;
for(int i = 0; i < 4; i++)
imgStatus[i] = Image.createImage(1, 1);
try
{
imgStatus[0] = Image.createImage("/Think.png");
}
catch(IOException _ex) { }
try
{
imgStatus[1] = Image.createImage("/Win.png");
}
catch(IOException _ex) { }
try
{
imgStatus[2] = Image.createImage("/Lose.png");
}
catch(IOException _ex) { }
statusImage = 3;
isColor = Display.getDisplay(midlet).numColors() > 2;
}
public void openConnection()
{
connection = new Connection(this,isServer);
connection.start() ;
}
private void calcSize()
{
font = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_UNDERLINED|Font.STYLE_ITALIC,
Font.SIZE_LARGE);
fontWidth = font.charWidth('棋');
fontHeight = font.getHeight();
isUpSide = canvasHeight > canvasWidth;
if(isUpSide)
{
cWidth = canvasWidth;
cHeight = canvasHeight - fontHeight;
} else
{
cWidth = canvasWidth - fontWidth;
cHeight = canvasHeight;
}
boardLength = cWidth > cHeight ? cHeight : cWidth;
gridLength = boardLength / boardSize;
boardLength = gridLength * boardSize;
boardX = (cWidth - boardLength) / 2;
boardY = (cHeight - boardLength) / 2;
if(isUpSide)
boardY += fontHeight;
stoneLength = gridLength - 2;
}
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, canvasWidth, canvasHeight);
if(isColor)
{
g.setColor(219,219,0);
g.fillRect(boardX, boardY, boardLength, boardLength);
}
g.setColor(isColor ? 255 : 0);
int y;
for(int r = 0; r < boardSize; r++)
{
int x1 = boardX + gridLength / 2;
int x2 = (x1 + boardLength) - gridLength;
y = boardY + r * gridLength + gridLength / 2;
g.drawLine(x1, y, x2, y);
}
int x;
for(int c = 0; c < boardSize; c++)
{
x = boardX + c * gridLength + gridLength / 2;
int y1 = boardY + gridLength / 2;
int y2 = (y1 + boardLength) - gridLength;
g.drawLine(x, y1, x, y2);
}
Dot triedDot = logic.triedDot();
int triedRow = triedDot.row;
int triedCol = triedDot.col;
for(int r = 0; r < boardSize; r++)
{
for(int c = 0; c < boardSize; c++)
if(r != triedRow || c != triedCol)
{
int stone = logic.getTable()[r][c];
if(stone != 0)
{
x = xByCol(c) - stoneLength / 2;
y = yByRow(r) - stoneLength / 2;
g.setColor(stone == 1 ? BLACK : WHITE);
g.fillArc(x, y, stoneLength, stoneLength, 0, 360);
g.setColor(0);
g.drawArc(x, y, stoneLength, stoneLength, 0, 360);
}
}
}
Dot lastDot = logic.lastDot();
int lastRow = lastDot.row;
int lastCol = lastDot.col;
int cLast;
if(isColor)
{
cLast = 0xff0000;
} else
{
cLast = 0;
switch(logic.getTable()[lastRow][lastCol])
{
case 1:
cLast = BLACK;
break;
case 2:
cLast = WHITE;
break;
}
}
g.setColor(cLast);
x = xByCol(lastCol) - 3;
y = yByRow(lastRow) - 3;
g.drawRect(x, y, 6, 6);
g.setFont(font);
g.setColor(isColor ? statusColor : 0);
if(isUpSide)
{
g.drawImage(imgStatus[statusImage], 0, 0, 20);
x = imgStatus[statusImage].getWidth();
g.drawString(status, x, 0, 20);
} else
{
x = cWidth + fontWidth;
g.drawImage(imgStatus[statusImage], x, 0, 24);
x = cWidth + fontWidth / 2;
y = imgStatus[statusImage].getHeight();
for(int i = 0; i < status.length(); i++)
{
char c = status.charAt(i);
g.drawChar(c, x, y, 17);
y += fontHeight;
}
}
}
private int xByCol(int col)
{
return boardX + col * gridLength + gridLength / 2;
}
private int yByRow(int row)
{
return boardY + row * gridLength + gridLength / 2;
}
protected void keyPressed(int keyCode)
{
if(!logic.checkGameOver())
{
int bs = boardSize;
Dot lastDot = logic.lastDot();
int r = lastDot.row;
int c = lastDot.col;
repaintAt(r, c);
int input = getGameAction(keyCode);
if(input == Canvas.LEFT || keyCode == 52)
{
if(--c < 0)
c = bs - 1;
lastDot.setRowCol(r, c);
repaintAt(r, c);
} else
if(input == Canvas.RIGHT || keyCode == 54)
{
if(++c >= bs)
c = 0;
lastDot.setRowCol(r, c);
repaintAt(r, c);
} else
if(input == Canvas.UP || keyCode == 50)
{
if(--r < 0)
r = bs - 1;
lastDot.setRowCol(r, c);
repaintAt(r, c);
} else
if(input == Canvas.DOWN || keyCode == 56)
{
if(++r >= bs)
r = 0;
lastDot.setRowCol(r, c);
repaintAt(r, c);
} else
if(input == Canvas.FIRE || keyCode == 53)
{ if(isMyTurn){
if(isBlack) {
if(logic.playerBlackGo(r, c)){
connection.sendMessage(r, c);
setStatus("等待對方落子");
isMyTurn = false;
}
}
else
{
if(logic.playerWhiteGo(r, c))
{
connection.sendMessage(r, c);
setStatus("等待對方落子");
isMyTurn = false;
}
}
}
}
}
}
public void receiveMessage() {
System.out.println("gamewold is receivemessage");
if (!isMyTurn) {
int r = connection.getSelectedR();
int c = connection.getSelectedC();
if(isBlack) logic.playerWhiteGo(r, c);
else logic.playerBlackGo(r, c);
isMyTurn = true;
setStatus("請落子...");
}
}
protected void keyRepeated(int keyCode)
{
keyPressed(keyCode);
}
public void commandAction(Command c, Displayable s)
{
if(c == CMD_STOP)
midlet.exit();
if(c == CMD_NEW)
Display.getDisplay(midlet).setCurrent(midlet.getDisplayable());
}
public boolean newStage()
{
addCommand(CMD_STOP);
calcSize();
logic = new BTFIRLogic(this,isBlack);
if(isServer){
isMyTurn = false;
isBlack = false;
}
return true;
}
public void completeInitialization(boolean isBTReady) {
if(isServer){
if (isBTReady) {
setStatus("服務器已經建立完畢,等待對方加入:");
repaint();
return;
}
// something wrong
Alert alertError = new Alert("", "Game Over", null,
AlertType.ERROR);
alertError.setTimeout(Alert.FOREVER);
Display.getDisplay(midlet).setCurrent(alertError);
}else{
if (isBTReady) {
setStatus("已準備加入游戲:");
repaint();
return;
}
Alert alertError = new Alert("Error", "Can't inititialize bluetooth Client", null,
AlertType.ERROR);
alertError.setTimeout(Alert.FOREVER);
Display.getDisplay(midlet).setCurrent(alertError,midlet.getDisplayable());
}
}
public void notifyGameEnd(boolean isBlackWon)
{
isMyTurn = false;
if (isBlack==isBlackWon)setStatus("你贏了!", 0xff0000, 1);
else setStatus("你輸了!", 65280, 2);
isChangeable = false;
addCommand(CMD_NEW);
}
public void setStatus(String s)
{if(isChangeable){
setStatus(s, 0, 3);
}
}
public void setStatus(String s, int color, int image)
{
if(isChangeable){
status = s;
statusColor = color;
statusImage = image;
int x;
int y;
int w;
int h;
if(isUpSide)
{
x = 0;
y = 0;
w = canvasWidth;
h = fontHeight;
} else
{
x = cWidth;
y = 0;
w = fontWidth;
h = canvasHeight;
}
repaint(x, y, w, h);
}
}
public void repaintAt(int row, int col)
{
int pX = boardX + (col - 1) * gridLength;
int pY = boardY + (row - 1) * gridLength;
int pW = gridLength * 2;
int pH = pW;
repaint(pX, pY, pW, pH);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -