/*
*中国象棋Java版V3.0
*作者
*源文件:Chess.java
*最后修改时间:2005-8-20
*添加功能:实现了当前棋局的保存
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
//主类
public class Chess
{
public static void main(String args[])
{
new ChessMainFrame("中国象棋Java版");
}
}
//主框架类
class ChessMainFrame extends JFrame implements ActionListener,MouseListener,Runnable
{
//玩家
JLabel play[] = new JLabel[32];
//棋盘
JLabel image;
//窗格
Container con;
//工具栏
JToolBar jmain;
//重新开始
JButton anew;
//悔棋
JButton repent;
//打开
JButton showOpen;
//保存
JButton showSave;
//退出
JButton exit;
//当前信息
JLabel text;
//保存当前操作
Vector FileVar;
Vector Var;
//规则类对象(使于调用方法)
ChessRule rule;
/*单击棋子**********************************/
/*chessManClick = true 闪烁棋子 并给线程响应*/
/*chessManClick = false 吃棋子 停止闪烁 并给线程响应*/
boolean chessManClick;
/*控制玩家走棋****************************/
/*chessPlayClick=1 黑棋走棋*/
/*chessPlayClick=2 红棋走棋 默认红棋*/
/*chessPlayClick=3 双方都不能走棋*/
int chessPlayClick=2;
//控制棋子闪烁的线程
Thread tmain;
//把第一次的单击棋子给线程响应
static int Man,i;
ChessMainFrame(){}
ChessMainFrame(String Title)
{
//获行客格引用
con = this.getContentPane();
con.setLayout(null);
//实例化规则类
rule = new ChessRule();
FileVar = new Vector();
Var = new Vector();
//创建工具栏
jmain = new JToolBar();
text = new JLabel(" 热烈欢迎");
text.setToolTipText("提示信息");
anew = new JButton(" 新 游 戏 " );
anew.setToolTipText("重新开始新的一局");
exit = new JButton(" 退 出 ");
exit.setToolTipText("退出本程序");
repent = new JButton(" 悔 棋 ");
repent.setToolTipText("返回到上次走棋的位置");
showOpen = new JButton("打开");
showOpen.setToolTipText("打开以前棋局");
showSave = new JButton("保存");
showSave.setToolTipText("保存当前棋局");
//把组件添加到工具栏
jmain.setLayout(new GridLayout(0,6));
jmain.add(anew);
jmain.add(repent);
jmain.add(showOpen);
jmain.add(showSave);
jmain.add(exit);
jmain.add(text);
jmain.setBounds(0,500,450,30);
con.add(jmain);
//添加棋子标签
drawChessMan();
/*注册监听者*/
//注册按扭监听
anew.addActionListener(this);
repent.addActionListener(this);
exit.addActionListener(this);
showOpen.addActionListener(this);
showSave.addActionListener(this);
//注册棋子移动监听
for (int i=0;i<32;i++)
{
con.add(play[i]);
play[i].addMouseListener(this);
}
//添加棋盘标签
con.add(image = new JLabel(new ImageIcon("CChess.GIF")));
image.setBounds(0,0,445,498);
image.addMouseListener(this);
//注册窗体关闭监听
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
//窗体居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2 - 200 ,(screenSize.height - frameSize.height ) / 2 - 290);
this.setIconImage(new ImageIcon("相1.gif").getImage());
this.setResizable(false);
this.setTitle(Title);
this.setSize(450,555);
this.show();
}
/*添加棋子方法************/
public void drawChessMan()
{
//流程控制
int i,k;
//图标
Icon in;
//黑色棋子
//车
in = new ImageIcon("车1.GIF");
for (i=0,k=10;i<2;i++,k+=385)
{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName("车1");
}
//马
in = new ImageIcon("马1.GIF");
for (i=4,k=60;i<6;i++,k+=285)
{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName("马1");
}
//相
in = new ImageIcon("相1.GIF");
for (i=8,k=105;i<10;i++,k+=195)
{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName("相1");
}
//士
in = new ImageIcon("士1.GIF");
for (i=12,k=155;i<14;i++,k+=95)
{
play[i] = new JLabel(in);
play[i].setBounds(k,10,40,40);
play[i].setName("士1");
}
//卒
in = new ImageIcon("卒1.GIF");
for (i=16,k=10;i<21;i++,k+=96.5)
{
play[i] = new JLabel(in);
play[i].setBounds(k,160,40,40);
play[i].setName("卒1" + i);
}
//炮
in = new ImageIcon("炮1.GIF");
for (i=26,k=60;i<28;i++,k+=289)
{
play[i] = new JLabel(in);
play[i].setBounds(k,110,40,40);
play[i].setName("炮1" + i);
}
//将
in = new ImageIcon("将1.GIF");
play[30] = new JLabel(in);
play[30].setBounds(205,10,40,40);
play[30].setName("将1");
//红色棋子
//车
in = new ImageIcon("车2.GIF");
for (i=2,k=10;i<4;i++,k+=385)
{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName("车2");
}
//马
in = new ImageIcon("马2.GIF");
for (i=6,k=60;i<8;i++,k+=285)
{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName("马2");
}
//相
in = new ImageIcon("相2.GIF");
for (i=10,k=105;i<12;i++,k+=195)
{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName("相2");
}
//士
in = new ImageIcon("士2.GIF");
for (i=14,k=155;i<16;i++,k+=95)
{
play[i] = new JLabel(in);
play[i].setBounds(k,450,40,40);
play[i].setName("士2");
}
//兵
in = new ImageIcon("兵2.GIF");
for (i=21,k=10;i<26;i++,k+=96.5)
{
play[i] = new JLabel(in);
play[i].setBounds(k,300,40,40);
play[i].setName("兵2" + i);
}
//炮
in = new ImageIcon("炮2.GIF");
for (i=28,k=60;i<30;i++,k+=289)
{
play[i] = new JLabel(in);
play[i].setBounds(k,350,40,40);
play[i].setName("炮2" + i);
}
//帅
in = new ImageIcon("帅2.GIF");
play[31] = new JLabel(in);
play[31].setBounds(205,450,40,40);
play[31].setName("帅2");
}
/*线程方法控制棋子闪烁*/
public void run()
{
while (true)
{
//单击棋子第一下开始闪烁
if (chessManClick)
{
play[Man].setVisible(false);
//时间控制
try
{
tmain.sleep(500);
}
catch(Exception e){}
play[Man].setVisible(true);
}
//闪烁当前提示信息 以免用户看不见
else
{
text.setVisible(false);
//时间控制
try
{
tmain.sleep(500);
}
catch(Exception e){}
text.setVisible(true);
}
try
{
tmain.sleep(500);
}
catch (Exception e){}
}
}
/*单击棋子方法************************/
public void mouseClicked(MouseEvent me)
{
System.out.println("Mouse");
//当前坐标
int Ex=0,Ey=0;
//启动线程
if (tmain == null)
{
tmain = new Thread(this);
tmain.start();
}
//单击棋盘(移动棋子)
if (me.getSource().equals(image))
{
//该红棋走棋的时候
if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2')
{
Ex = play[Man].getX();
Ey = play[Man].getY();
//移动卒、兵
if (Man > 15 && Man < 26)
{
rule.armsRule(Man,play[Man],me);
}
//移动炮
else if (Man > 25 && Man < 30)
{
rule.cannonRule(play[Man],play,me);
}
//移动车
else if (Man >=0 && Man < 4)
{
rule.cannonRule(play[Man],play,me);
}
//移动马
else if (Man > 3 && Man < 8)
{
rule.horseRule(play[Man],play,me);
}