📄 chess.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;
import java.awt.Point;
public class Chess 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 Chess()
{
bar=new JMenuBar();
fileMenu=new JMenu("中国象棋");
制作棋谱=new JMenuItem("制作棋谱");
保存棋谱=new JMenuItem("保存棋谱");
演示棋谱=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,670,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 Chess();
}
}
class ChessPoint
{
int x,y;
boolean 有棋子;
ChessPiece piece=null;
ChessBoard board=null;
public ChessPoint(int x,int y,boolean boo)
{
this.x=x;
this.y=y;
有棋子=boo;
}
public boolean isPiece()
{
return 有棋子;
}
public void set有棋子(boolean boo)
{
有棋子=boo;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public void setPiece(ChessPiece piece,ChessBoard board)
{
this.board=board;
this.piece=piece;
board.add(piece);
int w=(board.unitWidth);
int h=(board.unitHeight);
piece.setBounds(x-w/2,y-h/2,w,h);
有棋子=true;
board.validate();
}
public ChessPiece getPiece()
{
return piece;
}
public void reMovePiece(ChessPiece piece,ChessBoard board)
{
this.board=board;
this.piece=piece;
board.remove(piece);
board.validate();
有棋子=false;
}
}
class ChessPiece extends JLabel
{
String name;
Color backColor=null,foreColor;
String 颜色类别=null;
ChessBoard board=null;
int width,height;
public ChessPiece(String name,Color fc,Color bc,int width,int height,ChessBoard board)
{
this.name=name;
this.board=board;
this.width=width;
this.height=height;
foreColor=fc;
backColor=bc;
setSize(width,height);
setBackground(bc);
addMouseMotionListener(board);
addMouseListener(board);
}
public void paint(Graphics g)
{
g.setColor(foreColor);
g.fillOval(2,2,width-2,height-2);
g.setColor(Color.white);
g.setFont(new Font("隶书",Font.BOLD,28));
g.drawString(name,7,height-8);
g.setColor(Color.yellow);
g.drawOval(2,2,width-2,height-2);
}
public int getWidth()
{
return width;
}
public int getHeight()
{
return height;
}
public String getName()
{
return name;
}
public Color 获取棋子颜色()
{
return foreColor;
}
public void set棋子类别(String 类别)
{
颜色类别=类别;
}
public String 棋子类别()
{
return 颜色类别;
}
}
class ChessBoard extends JPanel implements MouseListener,MouseMotionListener
{
public ChessPoint point[][];
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(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) ;
红车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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -