一、实践目的:
1.鼠标点击、拖动等事件的应用与区别
2.棋谱文件的保存与读取
3.完善象棋的规则。
二、实践内容:
中国象棋历史悠久,吸引了无数的人研究,现对中国象棋的对战和实现棋谱的制作做如下的设计和说明,供大家参考学习。
1、机机对弈,红方先手。在符合规则的情况下拖动棋子到目的地,松鼠标落子。
人人对弈图
2、制作棋谱,选择制作棋谱菜单后,对弈开始,并记录了下棋过程。
选择“制作棋谱”菜单
棋谱制作完毕红方胜出
一方胜出后弹出胜利消息对话框。点击确定后,选择“保存棋谱”菜单,弹出保存文件对话框。
保存棋谱对话框
3.演示棋谱,选择演示棋谱菜单后,弹出打开对话框,选择保存好的棋谱,开始演示。
演示棋谱对话框
演示棋谱过程(自动和手动两种)
三、参考代码:
1.象棋主类 文件ChineseChess.java
package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
/**
* 象棋主类
*
* @author cnlht
*/
public class ChineseChess extends JFrame implements ActionListener {
ChessBoard board = null;
Demon demon = null;
MakeChessManual record = null;
Container con = null;
JMenuBar bar;
JMenu fileMenu;
JMenuItem 制作棋谱, 保存棋谱, 演示棋谱;
JFileChooser fileChooser = null;
LinkedList 棋谱 = null;
public ChineseChess() {
bar = new JMenuBar();
fileMenu = new JMenu("中国象棋");
制作棋谱 = new JMenuItem("制作棋谱");
保存棋谱 = new JMenuItem("保存棋谱");
保存棋谱.setEnabled(false);
演示棋谱 = new JMenuItem("演示棋谱");
fileMenu.add(制作棋谱);
fileMenu.add(保存棋谱);
fileMenu.add(演示棋谱);
bar.add(fileMenu);
setJMenuBar(bar);
setTitle(制作棋谱.getText());
制作棋谱.addActionListener(this);
保存棋谱.addActionListener(this);
演示棋谱.addActionListener(this);
board = new ChessBoard(45, 45, 9, 10);
record = board.record;
con = getContentPane();
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
board, record);
split.setDividerSize(5);
split.setDividerLocation(460);
con.add(split, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
setBounds(60, 20, 690, 540);
fileChooser = new JFileChooser();
con.validate();
validate();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == 制作棋谱) {
con.removeAll();
保存棋谱.setEnabled(true);
this.setTitle(制作棋谱.getText());
board = new ChessBoard(45, 45, 9, 10);
record = board.record;
JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
true, board, record);
split.setDividerSize(5);
split.setDividerLocation(460);
con.add(split, BorderLayout.CENTER);
validate();
}
if (e.getSource() == 保存棋谱) {
int state = fileChooser.showSaveDialog(null);
File saveFile = fileChooser.getSelectedFile();
if (saveFile != null && state == JFileChooser.APPROVE_OPTION) {
try {
FileOutputStream outOne = new FileOutputStream(saveFile);
ObjectOutputStream outTwo = new ObjectOutputStream(outOne);
outTwo.writeObject(record.获取棋谱());
outOne.close();
outTwo.close();
} catch (IOException event) {
}
}
}
if (e.getSource() == 演示棋谱) {
con.removeAll();
con.repaint();
con.validate();
validate();
保存棋谱.setEnabled(false);
int state = fileChooser.showOpenDialog(null);
File openFile = fileChooser.getSelectedFile();
if (openFile != null && state == JFileChooser.APPROVE_OPTION) {
try {
FileInputStream inOne = new FileInputStream(openFile);
ObjectInputStream inTwo = new ObjectInputStream(inOne);
棋谱 = (LinkedList) inTwo.readObject();
inOne.close();
inTwo.close();
ChessBoard board = new ChessBoard(45, 45, 9, 10);
demon = new Demon(board);
demon.set棋谱(棋谱);
con.add(demon, BorderLayout.CENTER);
con.validate();
validate();
this.setTitle(演示棋谱.getText() + ":" + openFile);
} catch (Exception event) {
JLabel label = new JLabel("不是棋谱文件");
label.setFont(new Font("隶书", Font.BOLD, 60));
label.setForeground(Color.red);
label.setHorizontalAlignment(SwingConstants.CENTER);
con.add(label, BorderLayout.CENTER);
con.validate();
this.setTitle("没有打开棋谱");
validate();
}
} else {
JLabel label = new JLabel("没有打开棋谱文件呢");
label.setFont(new Font("隶书", Font.BOLD, 50));
label.setForeground(Color.pink);
label.setHorizontalAlignment(SwingConstants.CENTER);
con.add(label, BorderLayout.CENTER);
con.validate();
this.setTitle("没有打开棋谱文件呢");
validate();
}
}
}
public static void main(String args[]) {
new ChineseChess();
}
}
2.象棋棋盘类文件ChessBoard.java
package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 棋盘类
*
* @author cnlht
*/
public class ChessBoard extends JPanel implements MouseListener,
MouseMotionListener {
public ChessPoint point[][];
public int unitWidth, unitHeight;
private int x轴长, y轴长;
private int x, y;
private Image img;
protected Image pieceImg;
private 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(int w, int h, int r, int c) {
setLayout(null);
addMouseListener(this);
addMouseMotionListener(this);
Color bc = getBackground();
unitWidth = w;
unitHeight = h;
x轴长 = r;
y轴长 = c;
point = new ChessPoint[r + 1][c + 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);
img = Toolkit.getDefaultToolkit().getImage("board.jpg");
pieceImg = Toolkit.getDefaultToolkit().getImage("piece.gif");
红车1 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this);
红车1.set棋子类别(红方颜色);
红车2 = new ChessPiece("車", Color.red, bc,