當前位置:編程學習大全網 - 源碼下載 - Java 跳棋的程序 急~~

Java 跳棋的程序 急~~

先導入三個按鈕圖片?

正常顯示的圖片"Begin1.jpg"

鼠標移動到按鈕上時顯示的圖片"Begin2.jpg"

按下鼠標時顯示的圖片"Begin1.jpg"

final ImageLoader imageBegin1 = new ImageLoader(sShell.getDisplay(), "Begin1.jpg");

final ImageLoader imageBegin2 = new ImageLoader(sShell.getDisplay(), "Begin2.jpg");

final ImageLoader imageBegin3 = new ImageLoader(sShell.getDisplay(),"Begin1.jpg");

創建按鈕?

lblBegin = new Label(parent, SWT.NO_BACKGROUND);

lblBegin.setImage(imageBegin1.getImage());

lblBegin.setBounds(70, 40, 75, 38);

為按鈕各事件寫入代碼?

lblBegin.addMouseTrackListener(new MouseTrackAdapter() {

public void mouseEnter(MouseEvent e) { //鼠標移動到按鈕上方

lblBegin.setImage(imageBegin2.getImage());

}

public void mouseExit(MouseEvent e) { //鼠標從按鈕上方移開

lblBegin.setImage(imageBegin1.getImage());

}

});

lblBegin.addMouseListener(new MouseAdapter() {

public void mouseDown(MouseEvent e) {

if (e.button == 1) {//按下鼠標左鍵

lblBegin.setImage(imageBegin3.getImage()); //在這裏寫入單擊事件代碼

}

}

public void mouseUp(MouseEvent e) {

if (e.button == 1) {//釋放鼠標左鍵

lblBegin.setImage(imageBegin2.getImage());

}

}

});

如圖所示,當X坐標為1時,Y的坐標只能為5,當X坐標為2時,Y的坐標可以5或6。於是我們建立壹個數組:

final static private int[][] pos = {

{5,5}, //X坐標為1,Y的上限是5,下限是5

{5,6}, //X坐標為2,Y的上限是5,下限是6

{5,7}, //X坐標為3,Y的上限是5,下限是7

{5,8}, //X坐標為4,Y的上限是5,下限是8

{1,13}, //X坐標為5,Y的上限是1,下限是13

{2,13}, //6

{3,13}, //7

{4,13}, //8

{5,13}, //9

{5,14}, //10

{5,15}, //11

{5,16}, //12

{5,17}, //13

{10,13}, //14

{11,13}, //15

{12,13}, //16

{13,13}, //17

};

在Position類中IsLegalPosition函數可以確定壹個坐標是否合法

public static boolean IsLegalPosition(int x, int y) {

if ((x < 1) || (x > 17)) {

return false;

}

if ((y < pos[x - 1][0]) || (y > pos[x - 1][1])) {

return false;

}

return true;

}

3. 棋盤類(ChessBoard)中棋子和坐標的索引關系

?棋盤中所有Chess集合

private Chess[] chesses = null;//所有棋子對象都保存這個數組當中

下面函數可以根據索引號返回棋子對象

public Chess getChess(int index) {

return chesses[index];

}

?棋子和坐標的對應關系

private Position[] chessesPosition = null;//所有棋子坐標都保存在這個數組當中

下面函數可以根據棋子對象或棋子索引號返回坐標

public Position getPosition(Chess chess) {

return chessesPosition[chess.getindex()];

}

public Position getPosition(int index) {

return chessesPosition[index];

}

?坐標和棋子的對應關系

private Chess[][] chessesIndex = new Chess[17][17];//數組保存了17*17個棋子對象指針

下面函數可以根據棋子坐標返回該位置上的棋子,如果沒有棋子返回Null

public Chess getChess(Position position) {

if (position == null){

return null;

}

return chessesIndex[position.getx() - 1][position.gety() - 1];

}

  • 上一篇:ROS2.9.7路由詳細安裝及設置步驟
  • 下一篇:關於如何進入衡水中學
  • copyright 2024編程學習大全網