?? efeicanvas.java
字號:
/*
* EfeiCanvas.java 04-8-9
*
* @author Efei
* @version 1.0
*/
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet.*;
import java.util.*;
import java.io.*;
public class EfeiCanvas extends Canvas implements CommandListener {
public Graphics gg = null;
Random rnd = new Random();
//public int lineNumber;
public int lineNumber = 11;
int gridWidth; //格子的寬度
int gridHeight; //格子的高度
int lineColor = 0x00006699; //棋盤格子線的顏色
int focusColor = 0x0000ff00; //焦點的顏色
public int intRunMode = 0; //0-等待初始態,1-下棋,2-等待用戶響應
public int intPlayer = 0; //0-黑棋(先下者),1-白棋(后下者)
public int[] qipan;
//檢查后,如果不存在五子,則全為0,如果存在,則依次是五個子的位置
private int[] wuzi = new int[5];
private int[][] q;
private int[][] m;
//int[] g={1,16,15,17}; //這個
int[] g = new int[4];
int baseX; //棋盤左上角X
int baseY; //棋盤左上角Y
int currentX; //當前所在區域的左上角X
int currentY; //當前所在區域的左上角Y
int currentA; //當前是第幾個豎線
int currentB; //當前是第幾個橫線
int LastChessmanA; //最后一個子位于第幾根豎線
int LastChessmanB; //最后一個子位于第幾根橫線
boolean loadedMenu = false;
Command cmdRestart = new Command("重新開始", Command.SCREEN, 1);
Command cmdExit = new Command("不玩了", Command.EXIT, 1);
/**
* 設置一些棋盤參數
*/
public void setParam(int[] value) {
lineNumber = value[0];
gridWidth = value[1];
gridHeight = value[2];
baseX = value[3];
baseY = value[4];
qipan = new int[lineNumber * lineNumber];
q = new int[3][lineNumber * lineNumber];
m = new int[3][lineNumber * lineNumber];
g[0] = 1;
g[1] = lineNumber ;
g[2] = lineNumber - 1;
g[3] = lineNumber + 1;
}
//
public void paint(Graphics g) {
if (!loadedMenu) {
addCommand(cmdExit);
addCommand(cmdRestart);
setCommandListener(this);
loadedMenu = true;
}
if (gg == null) gg = g;
if (intRunMode == 0) {
buildChessboard();
}
}
//事件處理函數
public void commandAction(Command c, Displayable s) {
if (!c.getLabel().equals("重新開始")) {
removeCommand(c);
addCommand(cmdRestart);
}
if (c.getCommandType() == Command.SCREEN) {
//重新開始游戲
intRunMode = 0;
repaint();
}
else if (c.getCommandType() == Command.EXIT) {
//返回主界面
intRunMode = 0;
five.flow(1);
}
}
/**
public void ShowLogo()
{
Image img = null;
try{
img = Image.createImage("/logo.png");
}catch(Exception e){}
gg.drawImage(img,0,0,gg.BOTTOM|gg.HCENTER);
repaint();
}
*/
/**
* 開始游戲時所做的一些工作
* 比如畫棋盤,初始化一些參數、數組,并設置最開始時的焦點位置
*/
public void buildChessboard() {
try {
//初始化數組
for (int i = 0; i < lineNumber * lineNumber; i++) {
qipan[i] = 0;
q[0][i] = 0;
q[1][i] = 0;
q[2][i] = 0;
m[0][i] = 0;
m[1][i] = 0;
m[2][i] = 0;
}
for (int i = 0; i < 5; i++)
wuzi[i] = 0;
gg.setColor(0x006699cc);
gg.fillRect(0, 0, getWidth(), getHeight());
gg.setColor(lineColor);
for (int i = 1; i < lineNumber + 1; i++) {
gg.drawLine(gridWidth, i * gridHeight, gridWidth * lineNumber + 1,
i * gridHeight);
gg.drawLine(i * gridWidth, gridHeight, i * gridWidth,
lineNumber * gridHeight + 1);
}
//初始時在中間
if (lineNumber % 2 == 0) {
currentX = baseX + gridWidth * (lineNumber / 2 - 1) - gridWidth / 2;
currentY = baseY + gridHeight * (lineNumber / 2 - 1) - gridHeight / 2;
currentA = lineNumber / 2;
}
else {
currentX = baseX + gridWidth * (lineNumber - 1) / 2 - gridWidth / 2;
currentY = baseY + gridHeight * (lineNumber - 1) / 2 - gridHeight / 2;
currentA = (lineNumber + 1) / 2;
}
currentB = currentA;
intRunMode = 1;
intPlayer = 0;
moveFoucs(currentX - gridWidth / 2, currentY - gridHeight / 2 - 1,
gridWidth * 3, gridHeight * 2);
//moveFoucs(currentX,currentY,gridWidth*2,gridHeight);
}
catch (Exception e) {
System.out.println("buildChessboard Error:" + e);
}
}
/**
* 移動焦點,需要對六個格子大的范圍進行重畫(可以優化為兩個格子)
* 重畫內容包括六個棋盤格子,十二個可能存在的棋子,以及一個焦點
*/
public void moveFoucs(int x, int y, int width, int height) {
try {
gg.setColor( (rnd.nextInt() & 0x7FFFFFFF) % 256,
(rnd.nextInt() & 0x7FFFFFFF) % 256,
(rnd.nextInt() & 0x7FFFFFFF) % 256);
gg.setColor(0x006699cc);
gg.fillRect(x, y, width, height);
//gg.setColor((rnd.nextInt()&0x7FFFFFFF)%256,(rnd.nextInt()&0x7FFFFFFF)%256,(rnd.nextInt()&0x7FFFFFFF)%256);
//這六個格子可能是橫著的,也可能是豎的,其中有四個格子的位置是不變的
mydrawRect(x, y, gridWidth, gridHeight);
mydrawRect(x, y + gridHeight, gridWidth, gridHeight);
mydrawRect(x + gridWidth, y, gridWidth, gridHeight);
mydrawRect(x + gridWidth, y + gridHeight, gridWidth, gridHeight);
//gg.setColor(lineColor);
if (width > height) {
gg.drawLine(x,y+gridHeight/2,x+gridWidth*2,y+gridHeight/2);
gg.drawLine(x+gridWidth/2,y,x+gridWidth/2,y+gridHeight);
gg.drawLine(x+gridWidth*3/2,y,x+gridWidth*3/2,y+gridHeight);
mydrawRect(x + gridWidth * 2, y, gridWidth, gridHeight);
mydrawRect(x + gridWidth * 2, y + gridHeight, gridWidth, gridHeight);
//最右邊的豎線
DrawChessman(x + gridWidth * 3, y);
DrawChessman(x + gridWidth * 3, y + gridHeight);
DrawChessman(x + gridWidth * 3, y + gridHeight * 2);
//最下面的橫線
DrawChessman(x, y + gridHeight * 2);
DrawChessman(x + gridWidth, y + gridHeight * 2);
DrawChessman(x + gridWidth * 2, y + gridHeight * 2);
}
else {
gg.drawLine(x+gridWidth/2,y,x+gridWidth/2,y+gridHeight*2);
gg.drawLine(x,y+gridHeight/2,x+gridWidth,y+gridHeight/2);
gg.drawLine(x,y+gridHeight*3/2,x+gridWidth,y+gridHeight*3/2);
mydrawRect(x, y + gridHeight * 2, gridWidth, gridHeight);
mydrawRect(x + gridWidth, y + gridHeight * 2, gridWidth, gridHeight);
//最右邊的豎線
DrawChessman(x + gridWidth * 2, y);
DrawChessman(x + gridWidth * 2, y + gridHeight);
DrawChessman(x + gridWidth * 2, y + gridHeight * 2);
DrawChessman(x + gridWidth * 2, y + gridHeight * 3);
//最下面的橫線
DrawChessman(x, y + gridHeight * 3);
DrawChessman(x + gridWidth, y + gridHeight * 3);
}
gg.setColor(focusColor);
//畫光標
int offsetValue = 1;
int offsetBase = 5;
gg.drawLine(currentX, currentY,
currentX + gridWidth * offsetValue / offsetBase, currentY);
gg.drawLine(currentX +
gridWidth * (offsetBase - offsetValue) / offsetBase, currentY,
currentX + gridWidth, currentY);
gg.drawLine(currentX, currentY, currentX,
currentY + gridHeight * offsetValue / offsetBase);
gg.drawLine(currentX,
currentY + gridHeight * (offsetBase - offsetValue) / offsetBase,
currentX, currentY + gridHeight);
gg.drawLine(currentX + gridWidth, currentY, currentX + gridWidth,
currentY + gridHeight * offsetValue / offsetBase);
gg.drawLine(currentX + gridWidth,
currentY +
gridHeight * (offsetBase - offsetValue) / offsetBase,
currentX + gridWidth, currentY + gridHeight);
gg.drawLine(currentX, currentY + gridHeight,
currentX + gridWidth * offsetValue / offsetBase,
currentY + gridHeight);
gg.drawLine(currentX +
gridWidth * (offsetBase - offsetValue) / offsetBase,
currentY + gridHeight, currentX + gridWidth,
currentY + gridHeight);
repaint();
}
catch (Exception e) {
System.out.println("moveFoucs Error:" + e);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -