import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ChessGameUI extends Application {
private ImageView[][] pieceImages;
private int selectedX = -1;
private int selectedY = -1;
private int a=9;
private int b=10;
// 移动棋子,返回是否成功
public boolean movePiece(int fromX, int fromY, int toX, int toY) {
// 实现移动逻辑
try {
pieceImages[toX][toY].setImage(pieceImages[fromX][fromY].getImage());
pieceImages[fromX][fromY].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\board.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return true;
}
@Override
public void start(Stage primaryStage) {
pieceImages = new ImageView[a][b];
Group root = new Group();
Scene scene = new Scene(root, 100*a, 100*b);
// 绘制棋盘
for (int x = 0; x < a; x++) {
for (int y = 0; y < b; y++) {
ImageView imageView = new ImageView();
imageView.setX(x * 100);
imageView.setY(y * 100);
imageView.setOnMouseClicked(this::handleMouseClick);
pieceImages[x][y] = imageView;
root.getChildren().add(imageView);
}
}
// 加载棋子图片
loadPieceImages();
primaryStage.setTitle("Chess Game");
primaryStage.setScene(scene);
primaryStage.show();
}
private void loadPieceImages() {
// 加载棋子图片并设置到ImageView
for (int i=0;i<a;i++){
for (int j=0;j<b;j++){
try {
pieceImages[i][j].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\board.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
try {
pieceImages[0][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\car.png")));
pieceImages[0][3].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[2][3].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[4][3].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[6][3].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[1][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\ma.png")));
pieceImages[1][2].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\pao.png")));
pieceImages[2][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\xiang.png")));
pieceImages[3][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\shi.png")));
pieceImages[4][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\shuai.png")));
pieceImages[8][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\car.png")));
pieceImages[8][3].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[7][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\ma.png")));
pieceImages[7][2].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\pao.png")));
pieceImages[6][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\xiang.png")));
pieceImages[5][0].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\shi.png")));
pieceImages[0][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\car.png")));
pieceImages[0][6].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[2][6].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[4][6].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[6][6].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[1][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\ma.png")));
pieceImages[1][7].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\pao.png")));
pieceImages[2][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\xiang.png")));
pieceImages[3][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\shi.png")));
pieceImages[4][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\shuai.png")));
pieceImages[8][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\car.png")));
pieceImages[8][6].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\bing.png")));
pieceImages[7][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\ma.png")));
pieceImages[7][7].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\pao.png")));
pieceImages[6][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\xiang.png")));
pieceImages[5][9].setImage(new Image(new FileInputStream("C:\\Users\\zhour\\Pictures\\chess\\shi.png")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void handleMouseClick(MouseEvent event) {
if (event.getButton() == MouseButton.PRIMARY) {
int x = (int) event.getX() / 100;
int y = (int) event.getY() / 100;
if (selectedX == -1) {
// 选择棋子
selectedX = x;
selectedY = y;
// 高亮选中的棋子
} else {
// 移动选中的棋子
movePiece(selectedX, selectedY, x, y);
selectedX = -1;
selectedY = -1;
// 更新棋盘UI
}
}
}
public static void main(String[] args) {
launch(args);
}
}
点击棋子可以让它移动到再次点击的位置