⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chessboard.java

📁 JAVA版中国象棋
💻 JAVA
字号:
package xq;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ChessBoard extends JPanel implements MouseListener,MouseMotionListener{
	private Sound postsound = new Sound("post.au");
	public ChessPoint point[][];
	private Chess chess;
	public int unitWidth, unitHeight;
	int x轴长, y轴长;
	int x, y;
	boolean move = false;
	public String 红方颜色 = "红色", 黑方颜色 = "蓝色";
	ChessPiece 红车1, 红车2, 红马1, 红马2, 红相1, 红相2, 红帅,
	红士1, 红士2, 红兵1, 红兵2, 红兵3, 红兵4, 红兵5, 红炮1, 红炮2;
	ChessPiece 黑车1, 黑车2, 黑马1, 黑马2, 黑将, 黑士1, 黑士2, 
	黑卒1, 黑卒2, 黑卒3, 黑卒4, 黑卒5, 黑象1, 黑象2, 黑炮1, 黑炮2;
	
	int startX,startY;
	int startI, startJ;
	public boolean 红方走棋 = true, 黑方走棋 = false;
	Rule rule = null;
	public MakeChessManual record = null;

	public ChessBoard(Chess chess, int w, int h, int r, int c) {
		// TODO 自动生成构造函数存根
		this.chess = chess;
		setLayout(null);
		addMouseListener(this);
		addMouseMotionListener(this);
		Color bc = getBackground();
		unitWidth = w;
		unitHeight = h;
		x轴长 = r;
		y轴长 = c;
		
		point = new ChessPoint[r+1][c+1];
		//棋盘的左上角的点是point[1][1],
		//第一行的点分别是point[1][1],point[2][1],point[3][1];
		
		for(int i =1; i<= r; i++){
			for(int j=1; j<=c; j++){
				point[i][j] = new ChessPoint(i*unitWidth, j*unitHeight, false);
			}
		}
		
		rule = new Rule(this,point);
		record = new MakeChessManual(this,point);
		
		红车1 = new ChessPiece("车", Color.red, bc, w-4, h-4, this);
		红车1.set棋子类别(红方颜色);
		红车2 = new ChessPiece("车", Color.red, bc, w-4, h-4, this);
		红车2.set棋子类别(红方颜色);
		红马1 = new ChessPiece("马", Color.red, bc, w-4, h-4, this);
		红马1.set棋子类别(红方颜色);
		红马2 = new ChessPiece("马", Color.red, bc, w-4, h-4, this);
		红马2.set棋子类别(红方颜色);
		红炮1 = new ChessPiece("炮", Color.red, bc, w-4, h-4, this);
		红炮1.set棋子类别(红方颜色);
		红炮2 = new ChessPiece("炮", Color.red, bc, w-4, h-4, this);
		红炮2.set棋子类别(红方颜色);
		红相1 = new ChessPiece("相", Color.red, bc, w-4, h-4, this);
		红相1.set棋子类别(红方颜色);
		红相2 = new ChessPiece("相", Color.red, bc, w-4, h-4, this);
		红相2.set棋子类别(红方颜色);
		红士1 = new ChessPiece("士", Color.red, bc, w-4, h-4, this);
		红士1.set棋子类别(红方颜色);
		红士2 = new ChessPiece("士", Color.red, bc, w-4, h-4, this);
		红士2.set棋子类别(红方颜色);
		红帅 = new ChessPiece("帅", Color.red, bc, w-4, h-4, this);
		红帅.set棋子类别(红方颜色);
		红兵1 = new ChessPiece("兵", Color.red, bc, w-4, h-4, this);
		红兵1.set棋子类别(红方颜色);
		红兵2 = new ChessPiece("兵", Color.red, bc, w-4, h-4, this);
		红兵2.set棋子类别(红方颜色);
		红兵3 = new ChessPiece("兵", Color.red, bc, w-4, h-4, this);
		红兵3.set棋子类别(红方颜色);
		红兵4 = new ChessPiece("兵", Color.red, bc, w-4, h-4, this);
		红兵4.set棋子类别(红方颜色);
		红兵5 = new ChessPiece("兵", Color.red, bc, w-4, h-4, this);
		红兵5.set棋子类别(红方颜色);
		
		
		黑将 = new ChessPiece("将", Color.blue, bc, w-4, h-4, this);
		黑将.set棋子类别(黑方颜色);
		黑士1 = new ChessPiece("士", Color.blue, bc, w-4, h-4, this);
		黑士1.set棋子类别(黑方颜色);
		黑士2 = new ChessPiece("士", Color.blue, bc, w-4, h-4, this);
		黑士2.set棋子类别(黑方颜色);
		黑车1 = new ChessPiece("车", Color.blue, bc, w-4, h-4, this);
		黑车1.set棋子类别(黑方颜色);
		黑车2 = new ChessPiece("车", Color.blue, bc, w-4, h-4, this);
		黑车2.set棋子类别(黑方颜色);
		黑炮1 = new ChessPiece("炮", Color.blue, bc, w-4, h-4, this);
		黑炮1.set棋子类别(黑方颜色);
		黑炮2 = new ChessPiece("炮", Color.blue, bc, w-4, h-4, this);
		黑炮2.set棋子类别(黑方颜色);
		黑马1 = new ChessPiece("马", Color.blue, bc, w-4, h-4, this);
		黑马1.set棋子类别(黑方颜色);
		黑马2 = new ChessPiece("马", Color.blue, bc, w-4, h-4, this);
		黑马2.set棋子类别(黑方颜色);
		黑象1 = new ChessPiece("象", Color.blue, bc, w-4, h-4, this);
		黑象1.set棋子类别(黑方颜色);
		黑象2 = new ChessPiece("象", Color.blue, bc, w-4, h-4, this);
		黑象2.set棋子类别(黑方颜色);
		黑卒1 = new ChessPiece("卒", Color.blue, bc, w-4, h-4, this);
		黑卒1.set棋子类别(黑方颜色);
		黑卒2 = new ChessPiece("卒", Color.blue, bc, w-4, h-4, this);
		黑卒2.set棋子类别(黑方颜色);
		黑卒3 = new ChessPiece("卒", Color.blue, bc, w-4, h-4, this);
		黑卒3.set棋子类别(黑方颜色);
		黑卒4 = new ChessPiece("卒", Color.blue, bc, w-4, h-4, this);
		黑卒4.set棋子类别(黑方颜色);
		黑卒5 = new ChessPiece("卒", Color.blue, bc, w-4, h-4, this);
		黑卒5.set棋子类别(黑方颜色);
		
		point[1][10].setPiece(红车1, this);
		point[2][10].setPiece(红马1, this);
		point[3][10].setPiece(红相1, this);
		point[4][10].setPiece(红士1, this);
		point[5][10].setPiece(红帅, this);
		point[6][10].setPiece(红士2, this);
		point[7][10].setPiece(红相2, this);
		point[8][10].setPiece(红马2, this);
		point[9][10].setPiece(红车2, this);
		point[2][8].setPiece(红炮1, this);
		point[8][8].setPiece(红炮2, this);
		point[1][7].setPiece(红兵1, this);
		point[3][7].setPiece(红兵2, this);
		point[5][7].setPiece(红兵3, this);
		point[7][7].setPiece(红兵4, this);
		point[9][7].setPiece(红兵5, this);
		
		point[1][1].setPiece(黑车1, this);
		point[2][1].setPiece(黑马1, this);
		point[3][1].setPiece(黑象1, this);
		point[4][1].setPiece(黑士1, this);
		point[5][1].setPiece(黑将, this);
		point[6][1].setPiece(黑士2, this);
		point[7][1].setPiece(黑象2, this);
		point[8][1].setPiece(黑马2, this);
		point[9][1].setPiece(黑车2, this);
		point[2][3].setPiece(黑炮1, this);
		point[8][3].setPiece(黑炮2, this);
		point[1][4].setPiece(黑卒1, this);
		point[3][4].setPiece(黑卒2, this);
		point[5][4].setPiece(黑卒3, this);
		point[7][4].setPiece(黑卒4, this);
		point[9][4].setPiece(黑卒5, this);
		
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		for(int j=1; j<=y轴长; j++){//画出棋盘的横线
			g.drawLine(point[1][j].x, point[1][j].y, point[x轴长][j].x, point[x轴长][j].y);
		}
		for(int i=1; i<=x轴长; i++){//画出棋盘的竖线,注意河界的位置
			if(i !=1 && i != x轴长){
				g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长-5].x, point[i][y轴长-5].y);
				g.drawLine(point[i][y轴长-4].x, point[i][y轴长-4].y, point[i][y轴长].x, point[i][y轴长].y);
			}
			else{
				g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长].x, point[i][y轴长].y);
			}
		}
		
		//画出将帅区
		g.drawLine(point[4][1].x, point[4][1].y, point[6][3].x, point[6][3].y);
		g.drawLine(point[6][1].x, point[6][1].y, point[4][3].x, point[4][3].y);
		g.drawLine(point[4][8].x, point[4][8].y, point[6][y轴长].x, point[6][y轴长].y);
		g.drawLine(point[4][y轴长].x, point[4][y轴长].y, point[6][8].x, point[6][8].y);
		
		//画出棋盘两边的提示坐标
		for(int i=1; i<=x轴长; i++){
			g.drawString(""+i, i*unitWidth, unitHeight/2);
		}
		int j = 1;
		for(char c='A'; c<='J';c++){
			g.drawString(""+c, unitWidth/4, j*unitHeight);
			j++;
		}
	}
	
	public void mousePressed(MouseEvent e){
		ChessPiece piece = null;
		Rectangle rect = null;
		if(e.getSource() == this)
			move = false;
		if(move  == false)
			if(e.getSource() instanceof ChessPiece){
				piece = (ChessPiece)e.getSource();//获取用户选中的棋子对象
				startX = piece.getBounds().x;//获取棋子的初始位置
				startY = piece.getBounds().y;
				//检查棋子所在的棋点
				rect = piece.getBounds();
				for(int i=1; i<=x轴长; i++){
					for(int j=1; j<=y轴长; j++){
						int x = point[i][j].getX();
						int y = point[i][j].getY();
						if(rect.contains(x, y)){
							startI = i;
							startJ = j;
							break;
						}
					}
				}
			}
	}
	
	
	public void mouseMoved(MouseEvent e){
		
	}
	
	public void mouseDragged(MouseEvent e){
		//假如在棋子上拖动鼠标,将导致棋子上发生鼠标拖动事件
		ChessPiece piece = null;
		if(e.getSource() instanceof ChessPiece){
			piece = (ChessPiece)e.getSource();//获取用户拖动的棋子对象
			move = true;
			//将鼠标拖动事件传递给棋盘,导致棋盘上发生鼠标拖动事件
			e = SwingUtilities.convertMouseEvent(piece, e, this);//E将传递给mouseDragged方法的参数
		}
		if(e.getSource() == this){
			if(move && piece != null){
				x = e.getX();
				y = e.getY();
				if(红方走棋 && ((piece.棋子类别()).equals(红方颜色))){
					piece.setLocation(x-piece.getWidth()/2, y-piece.getHeight()/2);
				}
				if(黑方走棋 && ((piece.棋子类别()).equals(黑方颜色))){
					piece.setLocation(x-piece.getWidth()/2, y-piece.getHeight()/2);
				}
			}
		}
	}
	
	public void mouseReleased(MouseEvent e){
		ChessPiece piece = null;
		move = false;
		Rectangle rect = null;
		if(e.getSource() instanceof ChessPiece){
			piece = (ChessPiece)e.getSource();
			rect = piece.getBounds();
			e = SwingUtilities.convertMouseEvent(piece, e, this);
		}
		if(e.getSource() == this){
			boolean containChessPoint = false;
			int x=0, y=0;
			int m=0, n=0;
			if(piece != null){
				//检查棋子当前覆盖的区域内是否包含一个棋点
				for(int i=1; i<=x轴长; i++){
					for(int j=1; j<=y轴长; j++){
						x = point[i][j].getX();
						y = point[i][j].getY();
						if(rect.contains(x, y)){
							containChessPoint = true;
							m = i;
							n = j;
							break;
						}
					}
				}
			}
			if(piece != null && containChessPoint){
				String str = null;
				Color pieceColor = piece.获取棋子颜色();
				if(point[m][n].isPiece()){
					str = (point[m][n].getPiece()).getName();
					Color c = (point[m][n].getPiece()).获取棋子颜色();
					if(pieceColor.getRGB() == c.getRGB()){
						//如果棋子点上有棋子且该棋子颜色和所拖动的棋子相同。将所拖动的棋子放回原处
						piece.setLocation(startX, startY);
						//将所拖放的棋子的初始棋点仍设置为有棋子
						(point[startI][startJ]).set有棋子(true);
					}
					else{
						//否则,如果棋子的移动符合规则,就将所拖动的棋子放置在当前棋点上,并将当前棋点上的对方棋子吃掉
						boolean ok = rule.movePieceRule(piece, startI, startJ, m, n, str);
						if(ok){
							postsound.play();
							ChessPiece pieceRemoved = point[m][n].getPiece();
							point[m][n].reMovePiece(pieceRemoved, this);
							point[m][n].setPiece(piece, this);
							(point[startI][startJ]).set有棋子(false);
							record.记录棋谱(piece, startI, startJ, m, n);
							record.记录吃掉的棋子(pieceRemoved);//留待悔棋时使用
							if(str.equals("帅") || str.equals("将")){
								String type = (point[m][n].getPiece()).棋子类别();
								JOptionPane.showMessageDialog(this, type + "胜利!","胜利",JOptionPane.ERROR_MESSAGE);
								chess.restart1();
							}
							if(piece.棋子类别().equals(红方颜色)){
							    红方走棋 = false;
							    黑方走棋 = true;
							}
							if(piece.棋子类别().equals(黑方颜色)){
								黑方走棋 = false;
								红方走棋 = true;
							}
							validate();
							repaint();
						}
						else{
							piece.setLocation(startX, startY);
							(point[startI][startJ]).set有棋子(true);
						}
					}
				}
				else{
					//如果该棋点上无棋子,且所拖放的棋子的走法符合规则,就将棋子放置在该棋点上
					boolean ok = rule.movePieceRule(piece, startI, startJ, m, n, "无");
					if(ok){
						postsound.play();
						point[m][n].setPiece(piece, this);
						(point[startI][startJ]).set有棋子(false);
						record.记录棋谱(piece, startI, startJ, m, n);
						record.记录吃掉的棋子("没吃棋子");
						if(piece.棋子类别().equals(红方颜色)){
						    红方走棋 = false;
						    黑方走棋 = true;
						}
						if(piece.棋子类别().equals(黑方颜色)){
							黑方走棋 = false;
							红方走棋 = true;
						}
					}
					else{
						piece.setLocation(startX, startY);
						(point[startI][startJ]).set有棋子(true);
					}
				}
			}
			if(piece != null && !containChessPoint){
				piece.setLocation(startX, startY);
				(point[startI][startJ]).set有棋子(true);
			}
		}
	}
	
	public void mouseEntered(MouseEvent ev){
		
	}
	
	public void mouseExited(MouseEvent e){
		
	}
	
	public void mouseClicked(MouseEvent evt){
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -