1、 Vector Var; /规则类对象(使于调用方法) ChessRule rule; /* * 单击棋子 * chessManClick = true 闪烁棋子 并给线程响应 * chessManClick = false 吃棋子 停止闪烁 并给线程响应 */ boolean chessManClick; * 控制玩家走棋 * chessPlayClick=1 黑棋走棋 * chessPlayClick=2 红棋走棋 默认红棋 * chessPlayClick=3 双方都不能走棋 int chessPlayClick=2; /控制棋子闪烁的线程 Thread tmain; /把第一次的单击棋
2、子给线程响应 static int Man,i; ChessMainFrame()中国象棋 * 构造函数 * 初始化图形用户界面 ChessMainFrame(String Title) /获行客格引用 con = this.getContentPane(); con.setLayout(null); /实例化规则类 rule = new ChessRule(); Var = new Vector(); /创建工具栏 jmain = new JToolBar(); text = new JLabel(欢迎使用象棋对弈系统 /当鼠标放上显示信息 text.setToolTipText(信息提示
3、anew = new JButton( 新 游 戏 anew.setToolTipText(重新开始新的一局 exit = new JButton( 退 出 exit.setToolTipText(退出象棋程序程序 repent = new JButton( 悔 棋 repent.setToolTipText(返回到上次走棋的位置 /把组件添加到工具栏 jmain.setLayout(new GridLayout(0,4); jmain.add(anew); jmain.add(repent); jmain.add(exit); jmain.add(text); jmain.setBounds
4、(0,0,558,30); con.add(jmain); /添加棋子标签 drawChessMan(); /注册按扭监听 anew.addActionListener(this); repent.addActionListener(this); exit.addActionListener(this); /注册棋子移动监听 for (int i=0;i screenSize.height) frameSize.height = screenSize.height; if (frameSize.width screenSize.width) frameSize.width = screenSi
5、ze.width; this.setLocation(screenSize.width - frameSize.width) / 2 - 280 ,(screenSize.height - frameSize.height ) / 2 - 350); /设置 this.setIconImage(new ImageIcon(image红将.GIF).getImage(); this.setResizable(false); this.setTitle(Title); this.setSize(558,670); this.show(); * 添加棋子方法 public void drawChes
6、sMan() /流程控制 int i,k; /图标 Icon in; /黑色棋子 /车 in = new ImageIcon(image黑车.GIF for (i=0,k=24;2;i+,k+=456) playi = new JLabel(in); playi.setBounds(k,56,55,55); playi.setName(车1 /马image黑马.GIF for (i=4,k=81;6;i+,k+=342) 马1 /相image黑象.GIF for (i=8,k=138;10;i+,k+=228) 象1 /士image黑士.GIF for (i=12,k=195;14;i+,k+
7、=114)士1 /卒image黑卒.GIF for (i=16,k=24;21; playi.setBounds(k,227,55,55);卒1 + i); /炮image黑炮.GIF for (i=26,k=81;28;i+,k+=342) playi.setBounds(k,170,55,55);炮1 /将image黑将.GIF play30 = new JLabel(in); play30.setBounds(252,56,55,55); play30.setName(将1 /红色棋子image红车.GIF for (i=2,k=24;4;i+,k+=456) playi.setBoun
8、ds(k,569,55,55);车2image红马.GIF for (i=6,k=81;8;马2image红象.GIF for (i=10,k=138;12;i+,k+=228)象2image红士.GIF for (i=14,k=195;16;士2 /兵image红卒.GIF for (i=21,k=24;26; playi.setBounds(k,398,55,55);卒2image红炮.GIF for (i=28,k=81;30; playi.setBounds(k,455,55,55);炮2 /帅 play31 = new JLabel(in); play31.setBounds(252
9、,569,55,55); play31.setName(帅2 * 线程方法控制棋子闪烁 public void run() while (true) /单击棋子第一下开始闪烁 if (chessManClick) playMan.setVisible(false); /时间控制 try tmain.sleep(200); catch(Exception e) playMan.setVisible(true); /闪烁当前提示信息 以免用户看不见 else text.setVisible(false); tmain.sleep(250); text.setVisible(true); try t
10、main.sleep(350); 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 & playMan.getName().charA
11、t(1) = 2) Ex = playMan.getX(); Ey = playMan.getY(); /移动卒、兵 if (Man 15 & Man 25 & 30) rule.cannonRule(playMan,play,me); /移动车=0 & 4) /移动马 3 & 8) rule.horseRule(playMan,play,me); /移动相、象 7 & 12) rule.elephantRule(Man,playMan,play,me); /移动仕、士 11 & 16) rule.chapRule(Man,playMan,play,me); /移动将、帅 else if (M
12、an = 30 | Man = 31) rule.willRule(Man,playMan,play,me); /是否走棋错误(是否在原地没有动) if (Ex = playMan.getX() & Ey = playMan.getY() text.setText( 红棋走棋 chessPlayClick=2; else 黑棋走棋 chessPlayClick=1; /if /该黑棋走棋的时候 else if (chessPlayClick = 1 &1) 30) else if (Man = 30 | Man = 31) /else if /当前没有操作(停止闪烁) chessManClic
13、k=false; /if /单击棋子 else /第一次单击棋子(闪烁棋子) if (!chessManClick) for (int i=0; /被单击的棋子 if (me.getSource().equals(playi) /告诉线程让该棋子闪烁 Man=i; /开始闪烁 chessManClick=true; break; /for /第二次单击棋子(吃棋子) else if (chessManClick) /当前没有操作(停止闪烁) chessManClick=false; for (i=0; /找到被吃的棋子 /该红棋吃棋的时候 if (chessPlayClick = 2 & Ex = playMan.getX(); Ey = playMan.getY(); /卒、兵吃规则 if (Man rule.armsRule(playMan,playi); /炮吃规则 else if (Man rule.cannonRule(0,playMan,playi,play,me); /车吃规则 rule.cannonRule(1,playMan,playi,play,me); /马吃规则 rule.horseRule(playMan,playi,play,me); /相、象吃规则 rule.elephantRule(playMan,playi,play);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1