?? mycanvas.java
字號:
package game;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
/**
* @author Administrator
* 實現類
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/
public class MyCanvas extends GameCanvas implements Runnable, CommandListener {
protected MyMIDlet mymidlet; //和mylet一樣,對象變量mymidlet只在該文件中起作用
private Graphics g;
int empty; //游戲界面到屏幕邊緣的留空
int canvasW, canvasH; //畫布的長和寬
int chessLength; //棋子的直徑
int chessMapLength, chessMapGrid, chessGridLength; //棋盤的邊長,棋盤一邊格子數,每格寬度
int chessMapX, chessMapY; //棋盤左上角x,y坐標
int selectedX, selectedY; //選擇框在棋盤格局上的x,y位置
boolean isPlayer1; //是否是玩家1
Chesses[][] chesses; //棋子數組
boolean newGame; //是否是新的游戲
boolean isplay; //是否游戲進行中
private Command exitCommand;
private Command startCommand;
int player1win, player2win;
// private Display display;
public MyCanvas() {
super(true);
newGame = true;
empty = 10;
canvasW = getWidth() - empty;
canvasH = getHeight() - empty;
chessMapGrid = 15;
chesses = new Chesses[chessMapGrid + 1][chessMapGrid + 1]; //因x個格子有x+1個交點
if (canvasW > canvasH) { //根據手機屏幕設定邊長為chessMapLength的正方形棋盤
chessMapLength = canvasH - canvasH % chessMapGrid;
chessMapX = (canvasW - chessMapLength) / 2 + empty / 2;
chessMapY = (canvasH % chessMapGrid) / 2 + empty / 2;
}
else {
chessMapLength = canvasW - canvasW % chessMapGrid;
chessMapX = (canvasW % chessMapGrid) / 2 + empty / 2;
chessMapY = (canvasH - chessMapLength) / 2 + empty / 2;
}
chessGridLength = chessMapLength / chessMapGrid;
chessLength = chessGridLength - 1;
selectedX = selectedY = chessMapGrid / 2;
isPlayer1 = true;
exitCommand = new Command("退出", Command.EXIT, 1);
startCommand = new Command("重新開始", Command.SCREEN, 1);
addCommand(startCommand);
addCommand(exitCommand);
setCommandListener(this);
}
public void startup() {
isplay = true;
Thread thread = new Thread(this);
thread.start();
}
protected void paintMap(Graphics g) { //畫棋盤,一格格畫
for (int i = 0; i < chessMapGrid; i++) {
for (int j = 0; j < chessMapGrid; j++) {
g.setColor(50, 128, 100); //淺綠的格子
g.drawRect(chessMapX + j * chessGridLength, //畫矩形框
chessMapY + i * chessGridLength,
chessGridLength, chessGridLength);
g.setColor(0, 50, 0); //綠色填充格子
g.fillRect(chessMapX + 1 + j * chessGridLength,
chessMapY + 1 + i * chessGridLength,
chessGridLength - 1, chessGridLength - 1);
if (chesses[i][j] != null) {
if (chesses[i][j].isPlayer1) {
g.setColor(255, 255, 255); //Player1的棋子是白色的
}
else {
g.setColor(255, 0, 0); //紅色
}
g.fillArc(chessMapX + j * chessGridLength - chessLength / 2, //畫棋子
chessMapY + i * chessGridLength - chessLength / 2,
chessLength, chessLength, 0, 360);
}
}
}
}
protected void paintSelected(Graphics g) { //畫選擇框
g.setColor(0, 150, 255); //藍色
g.drawRect(chessMapX + selectedX * chessGridLength - chessGridLength / 2,
chessMapY + selectedY * chessGridLength - chessGridLength / 2,
chessGridLength, chessGridLength);
}
protected void paintPlayer(Graphics g, boolean isPlayer1) {
if (isPlayer1) { //用白色大框表示輪到Player1
g.setColor(255, 255, 255);
}
else {
g.setColor(255, 0, 0);
}
g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
g.drawRect(2, 2, getWidth() - 4, getHeight() - 4);
g.drawRect(3, 3, getWidth() - 6, getHeight() - 6);
}
public void paint(Graphics g) {
g.setColor(0x00000000);
g.fillRect(0, 0, getWidth(), getHeight());
paintPlayer(g, isPlayer1);
paintMap(g);
paintSelected(g); //后畫的在最上層
flushGraphics();
}
private void init() {
if (newGame) {
chesses = new Chesses[chessMapGrid + 1][chessMapGrid + 1];
isPlayer1 = true;
selectedX = selectedY = chessMapGrid / 2;
}
}
public void run() {
g = getGraphics();
while (isplay) {
try {
input(g);
paint(g);
Thread.sleep(50);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public void input(Graphics g) {
int keyStates = getKeyStates();
if ( (keyStates & LEFT_PRESSED) != 0) {
selectedX = (--selectedX + chessMapGrid + 1) % (chessMapGrid + 1);
} //之所以要取模是為了讓選擇框能循環地走
else if ( (keyStates & RIGHT_PRESSED) != 0) {
selectedX = (++selectedX) % (chessMapGrid + 1);
}
else if ( (keyStates & UP_PRESSED) != 0) {
selectedY = (--selectedY + chessMapGrid + 1) % (chessMapGrid + 1);
}
else if ( (keyStates & DOWN_PRESSED) != 0) {
selectedY = (++selectedY) % (chessMapGrid + 1);
}
else if ( (keyStates & FIRE_PRESSED) != 0) {
if (chesses[selectedY][selectedX] == null) {
chesses[selectedY][selectedX] = new Chesses(this.isPlayer1);
if (checkWin()) {
String winner;
if (isPlayer1) {
winner = "白方勝利";
player1win++;
}
else {
winner = "紅方勝利";
player2win++;
}
Alert winAlert = new Alert("",
winner + "\n白方 " + player1win + " : " +
player2win + " 紅方",
null, AlertType.INFO);
winAlert.setTimeout(Alert.FOREVER);
Display.getDisplay(MyMIDlet.instance).setCurrent(winAlert); //顯示最終結果
init();
flushGraphics();
// repaint();
}
this.isPlayer1 = !this.isPlayer1; //切換下棋方
}
}
flushGraphics();
// repaint();
}
private boolean checkWin() {
int num = 1;
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX - i, selectedY)) { //左邊排成橫線的
num++; }
else if(isPlayer1(selectedX + i, selectedY)) { //右邊排成橫線的
num++; }
else{ break;}
}
}
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX, selectedY - i)) { //上方排成豎線的
num++; }
else if(isPlayer1(selectedX, selectedY + i)) { //下方排成豎線的
num++; }
else { break;}
}
}
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX - i, selectedY - i)) { //左斜上方排成直線
num++;}
else if (isPlayer1(selectedX + i, selectedY + i)) { //右斜下方排成直線
num++;}
else { break;}
}
}
if (num < 5) {
num = 1;
for (int i = 1; i <= 4; i++) {
if (isPlayer1(selectedX + i, selectedY - i)) { //右斜上方排成直線
num++; }
else if(isPlayer1(selectedX - i, selectedY + i)) { //左斜下方排成直線
num++; }
else { break;}
}
}
if (num >= 5) { return true; }
else { return false; }
}
private boolean isPlayer1(int y, int x) {
if (x <= 15 && x >= 0 && y <= 15 && y >= 0 && chesses[x][y] != null) {
if (chesses[x][y].isPlayer1 == this.isPlayer1) { return true; }
else { return false; }
}
else { return false; }
}
public void commandAction(Command c, Displayable dpa) {
if (c.equals(startCommand)) {
init();
startup();
}
else if (c.equals(exitCommand)) {
MyMIDlet.instance.notifyDestroyed(); //這里把整個程序都關掉了
//display.setCurrent(MyMIDlet.getForm());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -