?? chess.java
字號(hào):
/*
*中國(guó)象棋Java版V3.0
*作者
*源文件:Chess.java
*最后修改時(shí)間:2005-8-20
*添加功能:實(shí)現(xiàn)了當(dāng)前棋局的保存
*/
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("中國(guó)象棋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;
//當(dāng)前信息
JLabel text;
//保存當(dāng)前操作
Vector FileVar;
Vector Var;
//規(guī)則類對(duì)象(使于調(diào)用方法)
ChessRule rule;
/*單擊棋子**********************************/
/*chessManClick = true 閃爍棋子 并給線程響應(yīng)*/
/*chessManClick = false 吃棋子 停止閃爍 并給線程響應(yīng)*/
boolean chessManClick;
/*控制玩家走棋****************************/
/*chessPlayClick=1 黑棋走棋*/
/*chessPlayClick=2 紅棋走棋 默認(rèn)紅棋*/
/*chessPlayClick=3 雙方都不能走棋*/
int chessPlayClick=2;
//控制棋子閃爍的線程
Thread tmain;
//把第一次的單擊棋子給線程響應(yīng)
static int Man,i;
ChessMainFrame(){}
ChessMainFrame(String Title)
{
//獲行客格引用
con = this.getContentPane();
con.setLayout(null);
//實(shí)例化規(guī)則類
rule = new ChessRule();
FileVar = new Vector();
Var = new Vector();
//創(chuàng)建工具欄
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("保存當(dāng)前棋局");
//把組件添加到工具欄
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);
//添加棋子標(biāo)簽
drawChessMan();
/*注冊(cè)監(jiān)聽者*/
//注冊(cè)按扭監(jiān)聽
anew.addActionListener(this);
repent.addActionListener(this);
exit.addActionListener(this);
showOpen.addActionListener(this);
showSave.addActionListener(this);
//注冊(cè)棋子移動(dòng)監(jiān)聽
for (int i=0;i<32;i++)
{
con.add(play[i]);
play[i].addMouseListener(this);
}
//添加棋盤標(biāo)簽
con.add(image = new JLabel(new ImageIcon("CChess.GIF")));
image.setBounds(0,0,445,498);
image.addMouseListener(this);
//注冊(cè)窗體關(guān)閉監(jiān)聽
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;
//圖標(biāo)
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);
//時(shí)間控制
try
{
tmain.sleep(500);
}
catch(Exception e){}
play[Man].setVisible(true);
}
//閃爍當(dāng)前提示信息 以免用戶看不見
else
{
text.setVisible(false);
//時(shí)間控制
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");
//當(dāng)前坐標(biāo)
int Ex=0,Ey=0;
//啟動(dòng)線程
if (tmain == null)
{
tmain = new Thread(this);
tmain.start();
}
//單擊棋盤(移動(dòng)棋子)
if (me.getSource().equals(image))
{
//該紅棋走棋的時(shí)候
if (chessPlayClick == 2 && play[Man].getName().charAt(1) == '2')
{
Ex = play[Man].getX();
Ey = play[Man].getY();
//移動(dòng)卒、兵
if (Man > 15 && Man < 26)
{
rule.armsRule(Man,play[Man],me);
}
//移動(dòng)炮
else if (Man > 25 && Man < 30)
{
rule.cannonRule(play[Man],play,me);
}
//移動(dòng)車
else if (Man >=0 && Man < 4)
{
rule.cannonRule(play[Man],play,me);
}
//移動(dòng)馬
else if (Man > 3 && Man < 8)
{
rule.horseRule(play[Man],play,me);
}
//移動(dòng)相、象
else if (Man > 7 && Man < 12)
{
rule.elephantRule(Man,play[Man],play,me);
}
//移動(dòng)仕、士
else if (Man > 11 && Man < 16)
{
rule.chapRule(Man,play[Man],play,me);
}
//移動(dòng)將、帥
else if (Man == 30 || Man == 31)
{
rule.willRule(Man,play[Man],play,me);
}
//是否走棋錯(cuò)誤(是否在原地沒有動(dòng))
if (Ex == play[Man].getX() && Ey == play[Man].getY())
{
text.setText(" 紅棋走棋");
chessPlayClick=2;
}
else
{
text.setText(" 黑棋走棋");
chessPlayClick=1;
}
}//if
//該黑棋走棋的時(shí)候
else if (chessPlayClick == 1 && play[Man].getName().charAt(1) == '1')
{
Ex = play[Man].getX();
Ey = play[Man].getY();
//移動(dòng)卒、兵
if (Man > 15 && Man < 26)
{
rule.armsRule(Man,play[Man],me);
}
//移動(dòng)炮
else if (Man > 25 && Man < 30)
{
rule.cannonRule(play[Man],play,me);
}
//移動(dòng)車
else if (Man >=0 && Man < 4)
{
rule.cannonRule(play[Man],play,me);
}
//移動(dòng)馬
else if (Man > 3 && Man < 8)
{
rule.horseRule(play[Man],play,me);
}
//移動(dòng)相、象
else if (Man > 7 && Man < 12)
{
rule.elephantRule(Man,play[Man],play,me);
}
//移動(dòng)仕、士
else if (Man > 11 && Man < 16)
{
rule.chapRule(Man,play[Man],play,me);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -