ChineseChess.docx
《ChineseChess.docx》由会员分享,可在线阅读,更多相关《ChineseChess.docx(53页珍藏版)》请在冰点文库上搜索。
ChineseChess
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.util.LinkedList;
/*
*象棋主类
*/
publicclassChineseChessextendsJFrameimplementsActionListener
{
ChessBoardboard=null;
Demondemon=null;
MakeChessManualrecord=null;
Containercon=null;
JMenuBarbar;
JMenufileMenu;
JMenuItem制作棋谱,保存棋谱,演示棋谱;
JFileChooserfileChooser=null;
LinkedList棋谱=null;
publicChineseChess()
{
bar=newJMenuBar();
fileMenu=newJMenu("中国象棋");
制作棋谱=newJMenuItem("制作棋谱");
保存棋谱=newJMenuItem("保存棋谱");
保存棋谱.setEnabled(false);
演示棋谱=newJMenuItem("演示棋谱");
fileMenu.add(制作棋谱);
fileMenu.add(保存棋谱);
fileMenu.add(演示棋谱);
bar.add(fileMenu);
setJMenuBar(bar);
setTitle(制作棋谱.getText());
制作棋谱.addActionListener(this);
保存棋谱.addActionListener(this);
演示棋谱.addActionListener(this);
board=newChessBoard(45,45,9,10);
record=board.record;
con=getContentPane();
JSplitPanesplit=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,board,record);
split.setDividerSize(5);
split.setDividerLocation(460);
con.add(split,BorderLayout.CENTER);
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{System.exit(0);
}
});
setVisible(true);
setBounds(60,20,670,540);
fileChooser=newJFileChooser();
con.validate();
validate();
this.setResizable(false);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==制作棋谱)
{
con.removeAll();
保存棋谱.setEnabled(true);
this.setTitle(制作棋谱.getText());
board=newChessBoard(45,45,9,10);
record=board.record;
JSplitPanesplit=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,board,record);
split.setDividerSize(5);
split.setDividerLocation(460);
con.add(split,BorderLayout.CENTER);
validate();
}
if(e.getSource()==保存棋谱)
{
intstate=fileChooser.showSaveDialog(null);
FilesaveFile=fileChooser.getSelectedFile();
if(saveFile!
=null&&state==JFileChooser.APPROVE_OPTION)
{try
{
FileOutputStreamoutOne=newFileOutputStream(saveFile);
ObjectOutputStreamoutTwo=newObjectOutputStream(outOne);
outTwo.writeObject(record.获取棋谱());
outOne.close();
outTwo.close();
}
catch(IOExceptionevent)
{
}
}
}
if(e.getSource()==演示棋谱)
{
con.removeAll();
con.repaint();
con.validate();
validate();
保存棋谱.setEnabled(false);
intstate=fileChooser.showOpenDialog(null);
FileopenFile=fileChooser.getSelectedFile();
if(openFile!
=null&&state==JFileChooser.APPROVE_OPTION)
{try
{
FileInputStreaminOne=newFileInputStream(openFile);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
棋谱=(LinkedList)inTwo.readObject();
inOne.close();
inTwo.close();
ChessBoardboard=newChessBoard(45,45,9,10);
demon=newDemon(board);
demon.set棋谱(棋谱);
con.add(demon,BorderLayout.CENTER);
con.validate();
validate();
this.setTitle(演示棋谱.getText()+":
"+openFile);
}
catch(Exceptionevent)
{
JLabellabel=newJLabel("不是棋谱文件");
label.setFont(newFont("隶书",Font.BOLD,60));
label.setForeground(Color.red);
label.setHorizontalAlignment(SwingConstants.CENTER);
con.add(label,BorderLayout.CENTER);
con.validate();
this.setTitle("没有打开棋谱");
validate();
}
}
else
{
JLabellabel=newJLabel("没有打开棋谱文件呢");
label.setFont(newFont("隶书",Font.BOLD,50));
label.setForeground(Color.pink);
label.setHorizontalAlignment(SwingConstants.CENTER);
con.add(label,BorderLayout.CENTER);
con.validate();
this.setTitle("没有打开棋谱文件呢");
validate();
}
}
}
publicstaticvoidmain(Stringargs[])
{
newChineseChess();
}
}
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.geom.AffineTransform;
importjava.awt.geom.Area;
/*
*棋盘类
*/
publicclassChessBoardextendsJPanelimplementsMouseListener,
MouseMotionListener{
publicChessPointpoint[][];
publicintunitWidth,unitHeight;
intx轴长,y轴长;
intx,y;
booleanmove=false;
publicstaticString红方颜色="红色";
publicString黑方颜色="黑色";
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;
intstartX,startY;
intstartI,startJ;
publicstaticboolean红方走棋=true;
publicboolean黑方走棋=false;
Rulerule=null;
publicMakeChessManualrecord=null;
publicChessBoard(intw,inth,intr,intc){
setLayout(null);
addMouseListener(this);
addMouseMotionListener(this);
Colorbc=getBackground();
unitWidth=w;
unitHeight=h;
x轴长=r;
y轴长=c;
point=newChessPoint[r+1][c+1];
for(inti=1;i<=r;i++){
for(intj=1;j<=c;j++){
point[i][j]=newChessPoint(i*unitWidth,j*unitHeight,
false);
}
}
rule=newRule(this,point);
record=newMakeChessManual(this,point);
红车1=newChessPiece("车",Color.red,bc,w-4,h-4,this);
红车1.set棋子类别(红方颜色);
红车2=newChessPiece("车",Color.red,bc,w-4,h-4,this);
红车2.set棋子类别(红方颜色);
红马1=newChessPiece("马",Color.red,bc,w-4,h-4,this);
红马1.set棋子类别(红方颜色);
红马2=newChessPiece("马",Color.red,bc,w-4,h-4,this);
红马2.set棋子类别(红方颜色);
红炮1=newChessPiece("炮",Color.red,bc,w-4,h-4,this);
红炮1.set棋子类别(红方颜色);
红炮2=newChessPiece("炮",Color.red,bc,w-4,h-4,this);
红炮2.set棋子类别(红方颜色);
红相1=newChessPiece("相",Color.red,bc,w-4,h-4,this);
红相1.set棋子类别(红方颜色);
红相2=newChessPiece("相",Color.red,bc,w-4,h-4,this);
红相2.set棋子类别(红方颜色);
红士1=newChessPiece("士",Color.red,bc,w-4,h-4,this);
红士1.set棋子类别(红方颜色);
红士2=newChessPiece("士",Color.red,bc,w-4,h-4,this);
红士2.set棋子类别(红方颜色);
红帅=newChessPiece("帅",Color.red,bc,w-4,h-4,this);
红帅.set棋子类别(红方颜色);
红兵1=newChessPiece("兵",Color.red,bc,w-4,h-4,this);
红兵1.set棋子类别(红方颜色);
红兵2=newChessPiece("兵",Color.red,bc,w-4,h-4,this);
红兵2.set棋子类别(红方颜色);
红兵3=newChessPiece("兵",Color.red,bc,w-4,h-4,this);
红兵3.set棋子类别(红方颜色);
红兵4=newChessPiece("兵",Color.red,bc,w-4,h-4,this);
红兵4.set棋子类别(红方颜色);
红兵5=newChessPiece("兵",Color.red,bc,w-4,h-4,this);
红兵5.set棋子类别(红方颜色);
黑将=newChessPiece("将",Color.blue,bc,w-4,h-4,this);
黑将.set棋子类别(黑方颜色);
黑士1=newChessPiece("士",Color.blue,bc,w-4,h-4,this);
黑士1.set棋子类别(黑方颜色);
黑士2=newChessPiece("士",Color.blue,bc,w-4,h-4,this);
黑士2.set棋子类别(黑方颜色);
黑车1=newChessPiece("车",Color.blue,bc,w-4,h-4,this);
黑车1.set棋子类别(黑方颜色);
黑车2=newChessPiece("车",Color.blue,bc,w-4,h-4,this);
黑车2.set棋子类别(黑方颜色);
黑炮1=newChessPiece("炮",Color.blue,bc,w-4,h-4,this);
黑炮1.set棋子类别(黑方颜色);
黑炮2=newChessPiece("炮",Color.blue,bc,w-4,h-4,this);
黑炮2.set棋子类别(黑方颜色);
黑象1=newChessPiece("象",Color.blue,bc,w-4,h-4,this);
黑象1.set棋子类别(黑方颜色);
黑象2=newChessPiece("象",Color.blue,bc,w-4,h-4,this);
黑象2.set棋子类别(黑方颜色);
黑马1=newChessPiece("马",Color.blue,bc,w-4,h-4,this);
黑马1.set棋子类别(黑方颜色);
黑马2=newChessPiece("马",Color.blue,bc,w-4,h-4,this);
黑马2.set棋子类别(黑方颜色);
黑卒1=newChessPiece("卒",Color.blue,bc,w-4,h-4,this);
黑卒1.set棋子类别(黑方颜色);
黑卒2=newChessPiece("卒",Color.blue,bc,w-4,h-4,this);
黑卒2.set棋子类别(黑方颜色);
黑卒3=newChessPiece("卒",Color.blue,bc,w-4,h-4,this);
黑卒3.set棋子类别(黑方颜色);
黑卒4=newChessPiece("卒",Color.blue,bc,w-4,h-4,this);
黑卒4.set棋子类别(黑方颜色);
黑卒5=newChessPiece("卒",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);
}
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
ImageIconicon3=newImageIcon(
"icons/mood.jpg");
Imageimage3=icon3.getImage();
g.drawImage(image3,0,0,this);
for(intj=1;j<=y轴长;j++){
g.drawLine(point[1][j].x,point[1][j].y,point[x轴长][j].x,
point[x轴长][j].y);
}
for(inti=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);
Fontfont1=newFont("宋体",Font.BOLD,20);
g.setFont(font1);
g.drawString("楚",100,255);