Java实验三

北京电子科技学院(BESTI)

              

课程:Java程序设计                         班级:1351            姓名:李泽源      学号:20135108

成绩:             指导教师:娄佳鹏              实验日期:2015.6.3

实验密级:         预习程度:             实验时间:18:00-24:00

仪器组次:10          必修/选修:                  实验序号:3

实验名称:    Java敏捷开发XP实践                                                

实验内容:

1. XP基础

2. XP核心实践

3. 相关工具

                                                                  

实验仪器:

名称

型号

数量

PC 

MacBook(win7) 

虚拟Linux系统

实验楼虚拟机 

 1

 

实验步骤:

 

(一)敏捷开发与XP:

XP准则:

1.沟通 :XP认为项目成员之间的沟通是项目成功的关键,并把沟通看作项目中间协调与合作的主要推动因素。

2.简单 :XP假定未来不能可靠地预测,在现在考虑它从经济上是不明智的,所以不应该过多考虑未来的问题而是应该集中力量解决燃眉之急。

3.反馈 :XP认为系统本身及其代码是报告系统开发进度和状态的可靠依据。系统开发状态的反馈可以作为一种确定系统开发进度和决定系统下一步开发方向的手段。

4.勇气:代表了XP认为人是软件开发中最重要的一个方面的观点。在一个软件产品的开发中人的参与贯穿其整个生命周期,是人的勇气来排除困境,让团队把局部的最优抛之脑后,达到更重大的目标。表明了XP对“人让项目取得成功”的基本信任态度。

XP软件开发的活动:编码、测试、倾听、设计。其中的编码标准,结对编程,代码集体所有,测试,重构等实践过程较为重要。

 

(二)编码标准

编程标准包含:具有说明性的名字、清晰的表达式、直截了当的控制流、可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性。

 

1、缩进

【source->Format】 或用快捷键【Ctrl+Shift+F】就可以按Eclipse规定的规范缩进

 

2、命名规则

         要体现各自的含义

         包、类、变量用名词

         方法名用动宾

         包名全部小写,如:io,awt

         类名第一个字母要大写,如:HelloWorldApp

         变量名第一个字母要小写,如:userName

         方法名第一个字母要小写:setName

标识符名字应当直观且可以拼读

一般采用英文单词或其组合,便于记忆和阅读

标识符的长度“min-length && max-information”的原则:去元音法

一般全局变量用具有说明性的名字,局部变量用短名字:单字符的名字,常见的如i,j,k等用作局部变量

 

3、代码标准:七个原则

不要预先花费太多时间

目标应该是团队中没有人辨认各自的代码

以团队为单位对某一标准达成协议,然后遵守这一标准

不是事无巨细的规则列表,而是去本报代码可交流的指导方针

编码标准开始时应该很简单,然后根据团队经验逐步优化

创建能够工作的最简单标准,然后逐步发展

只制定适合本团队的

 

 

 

(三)结对编程

结对编程是XP中的重要实践。在结对编程模式下,一对程序员肩并肩、平等地、互补地进行开发工作。

1、两个角色:

驾驶员(Driver)是控制键盘输入的人。

领航员(Navigator)起到领航、提醒的作用。

 

2、如何和为何

•     驾驶员:写设计文档,进行编码和单元测试等XP开发流程。

•     领航员:审阅驾驶员的文档、驾驶员对编码等开发流程的执行;考虑单元测试的覆盖率;思考是否需要和如何重构;帮助驾驶员解决具体的技术问题。

•     驾驶员和领航员不断轮换角色,不要连续工作超过一小时,每工作一小时休息15分钟。领航员要控制时间。

•     主动参与。任何一个任务都首先是两个人的责任,也是所有人的责任。没有“我的代码”、“你的代码”或“他/她的代码”,只有“我们的代码”。

•     只有水平上的差距,没有级别上的差异。两人结对,尽管可能大家的级别资历不同,但不管在分析、设计或编码上,双方都拥有平等的决策权利。

 

 

(四)版本控制

我的代码库:http://git.shiyanlou.com/20135108lzy

 

1、好处

•       版本控制提供项目级的 undo(撤销) 功能: 没有什么事情是终结版本, 任何错误必须很容易回滚。 假设你在使用世界上最复杂的文字处理系统。 它具备了所有的能想到的功能,就是没有支持 DELETE(删除) 键。想象你打字的时候得多么的谨慎和缓慢吧, 特别是一篇超大的文档的快临近末尾的时候, 一个不小心就要重头再来(试想你选中所有的文字, 不小心按了 DELETE 键, 因为没有撤销功能,只好重新录入)。编辑文字和版本控制相同,任何时候都需要回滚,无论是一个小时, 一天, 还是一周, 这让你的团队工作自由快速的工作, 而且对于修正错误也非常自信。

•       版本控制允许多人在同一代码上工作, 只要遵守一定的控制原则就行。 再也不会发生诸如一个人覆盖了另一个人编辑的代码,导致那个人的修改无效这样的情况。

•       版本控制系统保存了过去所作的修改的历史记录。如果你遭遇到一些惊讶的代码,通过版本控制系统可以很容易找出是谁干的, 修改了什么, 修改的时间, 如果幸运的话,还能找出原因。

•       版本控制系统还支持在主线上开发的同时发布多个软件版本。在软件发布的时候也不需要整个团队的停止工作,不需要冻结代码。

•       版本控制也是项目级的时间机器,你可以选择任何一个时间, 精确地查看项目在当时的情况。 这对研究非常有用, 也是重现以前某个有问题的发布版本的基础。

 

2、举例:Hello World.java

 

a.进入Code目录,进入shiyanlou_cs212,创建HelloWorld目录,创建并编辑HelloWorld.java文件

 

b.编译、运行

 

 

c.用 git status查看一下代码状态,显示有未跟踪的代码;

用git add <file>...添加;

用git add HelloWorld.* 把要提交的文件的信息添加到索引库中;

用git commit将依据索引库中的内容来进行文件的提交;

用git push把代码保存到远程托管服务器中

 

 

 

d.修改HelloWorld.java如下

 

并编译运行

 

 

e.用git log查看代码提交记录

 

 

 

(五)重构:

         重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更.

 

 

 

 

 

一个完整的重构流程包括:

1.      从版本控制系统代码库中Check out code

2.      读懂代码(包括测试代码)

3.      发现bad smell

4.      Refactoring

5.      运行所有的Unit Tests

6.      往代码库中Check in code

 

 

(六)实践项目

此次试验中,我们结对的游戏项目是JKJ连连看

1.代码库地址:

http://git.shiyanlou.com/20135108lzy

2.游戏界面如下

 

3.源代码

package com.sf.minesweeper.bean;

 

import javax.swing.JLabel;

    /**

     * 自定义标签(雷块)

     */

public class MineLabel extends JLabel {

 

    private static final long serialVersionUID = -7271927020145498558L;

 

    /**

     * 判断是否是雷

     *

     * */

    private boolean isMine;

    /**

     * 判断雷块是否展开

     *

     * */

    private boolean isExpanded;

    /**

     * 判断雷块是否是旗子

     *

     * */

    private boolean isFlag;

    /**

     * 判断是否是雷且未标上旗子

     * */

    private boolean isMineAndNotflag;

    /**

     * count 计算雷块周围的雷数

     *

     * */

    private int mineCount;

    /**

     * 雷块所在的行

     *

     * */

    private int rowIndex;

    /**

     * 雷块所在的列

     *

     * */

    private int colIndex;

   

   

    private int expend=0;

 

 

 

//雷区上的行列坐标

    public MineLabel(int rowIndex, int colIndex) {

        this.rowIndex = rowIndex;

        this.colIndex = colIndex;

    }

//一向是对应的一些get和set方法

    public boolean isMine() {

        return isMine;

    }

 

    public void setMine(boolean isMine) {

        this.isMine = isMine;

    }

 

    public boolean isExpanded() {

        return isExpanded;

    }

 

    public void setExpanded(boolean isExpanded) {

        this.isExpanded = isExpanded;

    }

 

    public boolean isFlag() {

        return isFlag;

    }

 

    public void setFlag(boolean isFlag) {

        this.isFlag = isFlag;

    }

 

    public int getMineCount() {

        return mineCount;

    }

 

    public void setMineCount(int mineCount) {

        this.mineCount = mineCount;

    }

 

    public int getRowIndex() {

        return rowIndex;

    }

 

    public void setRowIndex(int rowIndex) {

        this.rowIndex = rowIndex;

    }

 

    public int getColIndex() {

        return colIndex;

    }

 

    public void setColIndex(int colIndex) {

        this.colIndex = colIndex;

       

       

    }

    /**

     * rightClickCount 右键点击次数

     *

     * */

    private int rightClickCount;

 

    public int getRightClickCount() {

        return rightClickCount;

    }

 

    public void setRightClickCount(int rightClickCount) {

        this.rightClickCount = rightClickCount;

    }

 

}

 

 

 

package com.sf.minesweeper.bean;

 

public class Own implements Comparable<Own>{

    private int times;

    private String name;

    private String level;

   

   

    public Own(int times, String name, String level) {

        super();

        this.times = times;

        this.name = name;

        this.level = level;

    }

    public String getLevel() {

        return level;

    }

    public void setLevel(String level) {

        this.level = level;

    }

    public int getTimes() {

        return times;

    }

    public void setTimes(int times) {

        this.times = times;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

 

    public int compareTo(Own arg0) {

        // TODO Auto-generated method stub

        int i = this.times - arg0.times;

        if(i==0){

            return this.name.hashCode() - arg0.name.hashCode();

        }

        return i;

    }

   

   

 

}

package com.sf.minesweeper.dialog;

 

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

import com.sf.minesweeper.bean.Own;

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.tools.Tools;

 

public class About extends JDialog {

      private JLabel labx,laby,labmine;

      private JTextField jTextField1,jTextField2,jTextField3;

      private JButton butyes,butno;

      SartFrame sartFrame;

      public About(SartFrame sartFrame) {

           this.sartFrame = sartFrame;

           this.setTitle("");

           this.setLocationRelativeTo(null);

           this.setModal(true);

           this.setSize(new Dimension(200,200));

           //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           this.init();

           this.setVisible(true);

      }

 

      private void init() {

           // TODO Auto-generated method stub

           JPanel jPanel = new JPanel();

            labx = new JLabel("");

            jTextField1=new JTextField(12);

            jTextField2=new JTextField(12);

            jTextField3=new JTextField(12);

            

            JLabel jLabelTotalx=new JLabel("行数:");

            JLabel jLabelTotaly=new JLabel("列数:");

            JLabel jLabelTotalMine=new JLabel("雷数:");

 

           jPanel.add(jLabelTotalx);

           jPanel.add(jTextField1);

          

           jPanel.add(jLabelTotaly);

           jPanel.add(jTextField2);

          

           jPanel.add(jLabelTotalMine);

           jPanel.add(jTextField3);

 

          

           butyes = new JButton("确定");

           butno = new JButton("取消");

           jPanel.add(butyes);

           jPanel.add(butno);

          

           butyes.addActionListener(new ActionListener() {

 

                 public void actionPerformed(ActionEvent arg0) {

                

                      try {

                            Tools.totalx=Integer.parseInt(jTextField1.getText());

                            Tools.totaly=Integer.parseInt(jTextField2.getText());

                            Tools.totalMine=Integer.parseInt(jTextField3.getText());

                            if(Tools.totalx>9&&Tools.totalx<30){

                                  if(Tools.totaly>9&&Tools.totaly<30){

                                       if(Tools.totalMine>=10&&Tools.totalMine<Tools.totalx*Tools.totaly*4/5){

                                             sartFrame.restart();

                                             About.this.dispose();

                                       }else{

                                             JOptionPane.showMessageDialog(null, "布雷有误请重试!!!\n布雷数应少于总格子数!!!", "提示消息", JOptionPane.NO_OPTION);  

                                       }

                                  }else {

                                       JOptionPane.showMessageDialog(null, "您输入的列数有误\n请重试!!!", "提示消息", JOptionPane.NO_OPTION);         

                                  }                    

                            }else {

                                  JOptionPane.showMessageDialog(null, "您输入的行数有误\n请重试!!!", "提示消息", JOptionPane.NO_OPTION);   

 

                            }         

                      } catch (Exception e2) {

                            JOptionPane.showMessageDialog(null, "必须是数字类型", "提示消息", JOptionPane.NO_OPTION);

                            return;

                      }

                     

                 }

           });

          

           butno.addActionListener(new ActionListener() {

                 public void actionPerformed(ActionEvent e) {

                      About.this.dispose();

                 }

           });

           this.add(jPanel);

          

          

          

      }

 

}package com.sf.minesweeper.dialog;

 

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.Box;

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

import com.sf.minesweeper.bean.Own;

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.tools.Tools;

 

public class ShowWin extends JDialog {

      //ublic class Hero extends JDialog{

           private JLabel jlabel1;    

           private JLabel jlabel2;

           private JLabel jlabel3;

           private JLabel jlabel4;

           private JLabel jlabel5;

           private JLabel jlabel6;

           private JLabel time1;

           private JLabel time2;

           private JLabel time3;

           private JLabel name1;

           private JLabel name2;

           private JLabel name3;

           private JButton jbutton1;

           private JButton jbutton2;

           private SartFrame sartsrame;

           private JPanel jpanel;

          

           public ShowWin(SartFrame sartsrame){

                 this.sartsrame=sartsrame;

                 this.setTitle("扫雷排行榜");

                 this.setVisible(true);

                 this.setSize(220,210);

                 this.setResizable(false);

                 this.setLocationRelativeTo(sartsrame);

                 this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

                 init();

           }

           public JLabel getJLabel1() {

                 return jlabel1;

           }

           public void setJLabel1(JLabel jlabel1) {

                 this.jlabel1 = jlabel1;

           }

           public JLabel getJLabel2() {

                 return jlabel2;

           }

           public void setJLabel2(JLabel jlabel2) {

                 this.jlabel2 = jlabel2;

           }

           public JLabel getJLabel3() {

                 return jlabel3;

           }

           public void setJLabel3(JLabel jlabel3) {

                 this.jlabel3 = jlabel3;

           }

           public JLabel getJLabel4() {

                 return jlabel4;

           }

           public void setJLabel4(JLabel jlabel4) {

                 this.jlabel4 = jlabel4;

           }

           public JLabel getJLabel5() {

                 return jlabel5;

           }

           public void setJLabel5(JLabel jlabel5) {

                 this.jlabel5 = jlabel5;

           }

           public JLabel getJLabel6() {

                 return jlabel6;

           }

           public void setJLabel6(JLabel jlabel6) {

                 this.jlabel6 = jlabel6;

           }

           public JLabel getTime1() {

                 return time1;

           }

           public void setTime1(JLabel time1) {

                 this.time1 = time1;

           }

           public JLabel getTime2() {

                 return time2;

           }

           public void setTime2(JLabel time2) {

                 this.time2 = time2;

           }

           public JLabel getTime3() {

                 return time3;

           }

           public void setTime3(JLabel time3) {

                 this.time3 = time3;

           }

           public JLabel getName1() {

                 return name1;

           }

           public void setName1(JLabel name1) {

                 this.name1 = name1;

           }

           public JLabel getName2() {

                 return name2;

           }

           public void setName2(JLabel name2) {

                 this.name2 = name2;

           }

           public JLabel getName3() {

                 return name3;

           }

           public void setName3(JLabel name3) {

                 this.name3 = name3;

           }

           public void init(){

                 HeroListener heroListener = new HeroListener();

                 jlabel1 = new JLabel(" 级别");

                 jlabel2 = new JLabel(" 成绩");

                 jlabel3 = new JLabel(" 玩家");

                 jlabel4 = new JLabel(" 初级:");

                 jlabel5 = new JLabel(" 中级:");

                 jlabel6 = new JLabel(" 高级:");

                 time1 = new JLabel(""+Tools.time1);

                 time2 = new JLabel(""+Tools.time2);

                 time3 = new JLabel(""+Tools.time3);

                 name1 = new JLabel(" "+Tools.name1);

                 name2 = new JLabel(" "+Tools.name2);

                 name3 = new JLabel(" "+Tools.name3);

                 jbutton1=new JButton("确定");

                 jbutton1.addActionListener(heroListener);

                 jbutton2=new JButton("重新设置");

                 jbutton2.addActionListener(heroListener);

                 jpanel=new JPanel();

                 Box box1 = Box.createVerticalBox();

                 box1.add(jlabel1);

                 box1.add(Box.createVerticalStrut(10));

                 box1.add(jlabel4);

                 box1.add(Box.createVerticalStrut(10));

                 box1.add(jlabel5);

                 box1.add(Box.createVerticalStrut(10));

                 box1.add(jlabel6);

                 Box box2 = Box.createVerticalBox();

                 box2.add(jlabel2);

                 box2.add(Box.createVerticalStrut(10));

                 box2.add(time1);

                 box2.add(Box.createVerticalStrut(10));

                 box2.add(time2);

                 box2.add(Box.createVerticalStrut(10));

                 box2.add(time3);

                 Box box3 = Box.createVerticalBox();

                 box3.add(jlabel3);

                 box3.add(Box.createVerticalStrut(10));

                 box3.add(name1);

                 box3.add(Box.createVerticalStrut(10));

                 box3.add(name2);

                 box3.add(Box.createVerticalStrut(10));

                 box3.add(name3);

                 Box box4 = Box.createHorizontalBox();

                 box4.add(jbutton1);

                 box4.add(Box.createHorizontalStrut(20));

                 box4.add(jbutton2);

                 Box box5 = Box.createHorizontalBox();

                 box5.add(box1);

                 box5.add(box2);

                 box5.add(box3);

                 Box box6 = Box.createVerticalBox();

                 box6.add(Box.createVerticalStrut(20));

                 box6.add(box5);

                 box6.add(Box.createVerticalStrut(20));

                 box6.add(box4);

                 jpanel.add(box6);

                 this.add(jpanel);

           }

           class HeroListener implements ActionListener{

                 public void actionPerformed(ActionEvent e){

                      String command = e.getActionCommand();

                      if(command.equals("确定")){

                            dispose();

                      }

                      else {

                            Tools.time1 = 999;

                            Tools.time2 = 999;

                            Tools.time3 = 999;

                            Tools.name1 = " 匿名";

                            Tools.name2 = " 匿名";

                            Tools.name3 = " 匿名";             

                            time1.setText(""+Tools.time1);

                            name1.setText(Tools.name1);                    

                            time2.setText(""+Tools.time2);

                            name2.setText(Tools.name2);               

                            time3.setText(""+Tools.time3);

                            name3.setText(Tools.name3);

                            //dispose();

                      }

                 }

           }

      }

 

     

package com.sf.minesweeper.dialog;

 

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.TreeSet;

 

import javax.swing.JButton;

import javax.swing.JDialog;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

 

import com.sf.minesweeper.bean.Own;

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.tools.Tools;

 

public class Win extends JDialog {

      SartFrame sartFrame;

      private JTextField text;

      TreeSet<Own> LOWER = new TreeSet<Own>();

      TreeSet<Own> MIDDLE = new TreeSet<Own>();

      TreeSet<Own> HEIGHT = new TreeSet<Own>();

     

      public Win(SartFrame sartFrame){

           this.sartFrame = sartFrame;

           this.setTitle("提示框");

           this.setLocationRelativeTo(null);

           this.setSize(200, 150);

           this.init();

           this.setVisible(true);

      }

 

      private void init() {

           // TODO Auto-generated method stub

           /**

            * 存放记入

            *

            */

           JPanel panel = new JPanel();

           panel.setLayout(new GridLayout(4,1));

           JLabel label = new JLabel("请留下你的大名");

          

           panel.add(label);

          

           text = new JTextField();

           panel.add(text);

           //times = sartFrame.getTimer().getTimes();

           JLabel labTime = new JLabel("你所使用的时间:"+Tools.time);

           panel.add(labTime);

          

           JButton butys = new JButton("保存");

           butys.addActionListener(new ActionListener() {    

                 @Override

                 public void actionPerformed(ActionEvent e) {

 

                      // TODO Auto-generated method stub

                      if(Tools.currentLevel.equals("初级")){

                            if(Tools.time1>=Tools.time){

                                  Tools.time1 = Tools.time;

                                  Tools.name1=text.getText();

                            }

                      }

                      if(Tools.currentLevel.equals("中级")){                 

                            if(Tools.time2>=Tools.time){

                                  Tools.time2= Tools.time;

                                  Tools.name2=text.getText();

                            }

                      }

                      if(Tools.currentLevel.equals("高级")){                 

                            if(Tools.time3>=Tools.time){

                                  Tools.time3 = Tools.time;

                                  Tools.name3=text.getText();

                            }

                      }

                     

                      Win.this.dispose();

                     

                 }

           });

           panel.add(butys);

           this.add(panel);

          

      }

      public JTextField getText() {

           return text;

      }

 

}

 

package com.sf.minesweeper.frame;

 

import java.applet.Applet;

import java.applet.AudioClip;

import java.awt.BorderLayout;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

 

import javax.swing.ImageIcon;

import javax.swing.JApplet;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.Timer;

 

import java.net.*;

import java.applet.AudioClip;

 

//import lable.MineLabel;

//import javax.swing.JPanel;

 

//import panel.FacePanel;

//import panel.MinePanel;

 

import com.sf.minesweeper.listener.MouseListener;

import com.sf.minesweeper.menu.MineMenu;

 

import com.sf.minesweeper.panel.MineField;

import com.sf.minesweeper.panel.MineState;

import com.sf.minesweeper.timer.Timers;

import com.sf.minesweeper.bean.MineLabel;

 

import com.sf.minesweeper.tools.Tools;

 

public class SartFrame extends JFrame {

 

      /**

       *

       */

      private static final long serialVersionUID = -1585043387266273492L;

      /**

       * @param args

       */

 

      private MineState mineState; // 记雷数

      private MineField mineField; // 布置labble和雷数

      private MineMenu mineMenu;

      private Timer timer;

      private Timers timers;

      /**

       * 游戏是否开始

       */

      private boolean isStart;

      JLabel jLabel_start = new JLabel(); // 开始图片

 

 

      public SartFrame() {

           this.setTitle("扫雷");

           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           this.setLocationRelativeTo(null);

           setIconImage(Tools.iicon); // 利用tools来做的作法

 

           this.setResizable(false); // 这样让窗口不会可放大

          

          

           // ..................状态栏.....................

           mineState = new MineState(this);

           this.add(mineState, BorderLayout.NORTH);

 

           // ...................雷区......................

           mineField = new MineField(this);

           this.add(mineField, BorderLayout.CENTER);

 

           jLabel_start.setIcon(Tools.start);

           this.add(jLabel_start, BorderLayout.CENTER);

 

           // ....................菜单栏................

           mineMenu = new MineMenu(this);

           this.setJMenuBar(mineMenu);

          

           // .....................时间................

           Tools.time = 0;

           timers = new Timers(mineState);

           timer = new Timer(1000, timers);

 

           // ...................声音...................

//         AudioClip s1=loadSound("alarm1.wav");                           //AudioClip类的对象s1通过方法 loadSound()装载声音

//         public AudioClip loadSound(String filename){                        //返回一个AudioClip对象

//            URL url=null;                                                                   //因为newAudioClip()的参数为URL型

//            try{

//             url=new URL("file:"+filename);                                //指定文件,“file:"不能少

//            }

//            catch(MalformedURLException e){ }

//            return Applet.newAudioClip(url);                           //通过newAudioClip( )方法装载声音,此方法为JDK后添加的方法,太老的JDK里可能没有

//         }

//

//        

//        

//        

           pack();

           this.setVisible(true);

      }

 

 

      // 重新布局

      public void restart() {

 

           this.remove(mineState);

 

           this.remove(mineField);

 

           this.remove(jLabel_start);

          

          

 

           // ..................状态栏.....................

           mineState = new MineState(this);

           this.add(mineState, BorderLayout.NORTH);

          

 

          

           // ...................雷区......................

           mineField = new MineField(this);

           this.add(mineField, BorderLayout.CENTER);

 

          

           // .....................时间................

           Tools.time = 0;

           Timers timers = new Timers(mineState);

           timer = new Timer(1000, timers);

          

          

           pack();

           validate();//刷新窗体

      }

 

 

 

      public static long getSerialversionuid() {

           return serialVersionUID;

      }

 

 

 

      public MineState getMineState() {

           return mineState;

      }

 

 

 

      public MineField getMineField() {

           return mineField;

      }

 

 

 

      public MineMenu getMineMenu() {

           return mineMenu;

      }

 

 

 

      public Timer getTimer() {

           return timer;

      }

 

 

 

      public Timers getTimers() {

           return timers;

      }

 

     

     

     

 

      public boolean isStart() {

           return isStart;

      }

 

      public void setStart(boolean isStart) {

           this.isStart = isStart;

      }

      public static void main(String[] args) {

           new SartFrame();

          

      }

     

     

     

     

 

     

     

}package com.sf.minesweeper.frame;

 

import java.applet.Applet;

import java.applet.AudioClip;

import java.awt.BorderLayout;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

 

import javax.swing.ImageIcon;

import javax.swing.JApplet;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.Timer;

 

import java.net.*;

import java.applet.AudioClip;

 

//import lable.MineLabel;

//import javax.swing.JPanel;

 

//import panel.FacePanel;

//import panel.MinePanel;

 

import com.sf.minesweeper.listener.MouseListener;

import com.sf.minesweeper.menu.MineMenu;

 

import com.sf.minesweeper.panel.MineField;

import com.sf.minesweeper.panel.MineState;

import com.sf.minesweeper.timer.Timers;

import com.sf.minesweeper.bean.MineLabel;

 

import com.sf.minesweeper.tools.Tools;

 

public class SartFrame extends JFrame {

 

      /**

       *

       */

      private static final long serialVersionUID = -1585043387266273492L;

      /**

       * @param args

       */

 

      private MineState mineState; // 记雷数

      private MineField mineField; // 布置labble和雷数

      private MineMenu mineMenu;

      private Timer timer;

      private Timers timers;

      /**

       * 游戏是否开始

       */

      private boolean isStart;

      JLabel jLabel_start = new JLabel(); // 开始图片

 

 

      public SartFrame() {

           this.setTitle("扫雷");

           this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

           this.setLocationRelativeTo(null);

           setIconImage(Tools.iicon); // 利用tools来做的作法

 

           this.setResizable(false); // 这样让窗口不会可放大

          

          

           // ..................状态栏.....................

           mineState = new MineState(this);

           this.add(mineState, BorderLayout.NORTH);

 

           // ...................雷区......................

           mineField = new MineField(this);

           this.add(mineField, BorderLayout.CENTER);

 

           jLabel_start.setIcon(Tools.start);

           this.add(jLabel_start, BorderLayout.CENTER);

 

           // ....................菜单栏................

           mineMenu = new MineMenu(this);

           this.setJMenuBar(mineMenu);

          

           // .....................时间................

           Tools.time = 0;

           timers = new Timers(mineState);

           timer = new Timer(1000, timers);

 

           // ...................声音...................

//         AudioClip s1=loadSound("alarm1.wav");                           //AudioClip类的对象s1通过方法 loadSound()装载声音

//         public AudioClip loadSound(String filename){                        //返回一个AudioClip对象

//            URL url=null;                                                                   //因为newAudioClip()的参数为URL型

//            try{

//             url=new URL("file:"+filename);                                //指定文件,“file:"不能少

//            }

//            catch(MalformedURLException e){ }

//            return Applet.newAudioClip(url);                           //通过newAudioClip( )方法装载声音,此方法为JDK后添加的方法,太老的JDK里可能没有

//         }

//

//        

//        

//        

           pack();

           this.setVisible(true);

      }

 

 

      // 重新布局

      public void restart() {

 

           this.remove(mineState);

 

           this.remove(mineField);

 

           this.remove(jLabel_start);

          

          

 

           // ..................状态栏.....................

           mineState = new MineState(this);

           this.add(mineState, BorderLayout.NORTH);

          

 

          

           // ...................雷区......................

           mineField = new MineField(this);

           this.add(mineField, BorderLayout.CENTER);

 

          

           // .....................时间................

           Tools.time = 0;

           Timers timers = new Timers(mineState);

           timer = new Timer(1000, timers);

          

          

           pack();

           validate();//刷新窗体

      }

 

 

 

      public static long getSerialversionuid() {

           return serialVersionUID;

      }

 

 

 

      public MineState getMineState() {

           return mineState;

      }

 

 

 

      public MineField getMineField() {

           return mineField;

      }

 

 

 

      public MineMenu getMineMenu() {

           return mineMenu;

      }

 

 

 

      public Timer getTimer() {

           return timer;

      }

 

 

 

      public Timers getTimers() {

           return timers;

      }

 

     

     

     

 

      public boolean isStart() {

           return isStart;

      }

 

      public void setStart(boolean isStart) {

           this.isStart = isStart;

      }

      public static void main(String[] args) {

           new SartFrame();

          

      }

     

     

     

     

 

     

     

}package com.sf.minesweeper.listener;

 

import java.awt.event.InputEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

 

import javax.swing.JOptionPane;

 

import com.sf.minesweeper.bean.MineLabel;

import com.sf.minesweeper.dialog.Win;

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.panel.MineField;

import com.sf.minesweeper.tools.Tools;

 

public class MouseListener extends MouseAdapter {

      /**

       * 标记雷区点击次数,第一次游戏开始

       */

      private int mousePressedCount;            // 鼠标点击的次数

      private int expendedCount;                 //雷块展开的数量

      // MineField mineField;

     

      SartFrame sartframe;

      boolean isDouble ;

      int temp = Tools.totalMine;// 临时雷数

      boolean isStart;// 游戏是否开始

     

     

      public MouseListener(SartFrame sartframe) {

           this.sartframe = sartframe;

      }

   

      public void mousePressed(MouseEvent arg0) {

          

           MineLabel mineLabel = (MineLabel) arg0.getSource();

           int d = arg0.getModifiersEx();               // 返回此事件的修饰符掩码

           int d1 = arg0.getModifiers();

           // 获取当前点击的雷块和其行列索引

           int rowIndex = mineLabel.getRowIndex();

           int colIndex = mineLabel.getColIndex();

           // 记录雷区点击次数

           mousePressedCount++;

 

           // 如果是第一次点击雷区,则开始布雷

           if (mousePressedCount == 1) {

                 sartframe.getMineField().buildMine(rowIndex, colIndex);

           }

          

          

           // .............................1双键按下.............................

           if (d == InputEvent.BUTTON1_DOWN_MASK + InputEvent.BUTTON3_DOWN_MASK) {

                 // 1 周围图片切换

                 for (int x = Math.max(rowIndex - 1, 0); x <= Math.min(rowIndex + 1,

                            Tools.totalx - 1); x++) {

                      for (int y = Math.max(colIndex - 1, 0); y <= Math.min(

                                  colIndex + 1, Tools.totaly - 1); y++) {

                            mineLabel = sartframe.getMineField().getMineLabel()[x][y];

                            if (!mineLabel.isExpanded() && !mineLabel.isFlag())

                                  mineLabel.setIcon(Tools.mineCount[0]);

                      }

                 }

                 // 2 表情变惊讶

                 sartframe.getMineState().getNewGame().setIcon(Tools.iiface2);

                 // 3 记录双键点击标志

                 isDouble = true;

 

                 // ..............................2右键按下  且雷块未展开....................................

           } else if (d1 == InputEvent.BUTTON3_MASK && !mineLabel.isExpanded()) {

                 // 右键点击次数

                 int clickCount = mineLabel.getRightClickCount();

                 clickCount++;

                 // 第一次,雷块>>>旗子

                 if (clickCount == 1) {

                      mineLabel.setIcon(Tools.iiflag);

                      mineLabel.setRightClickCount(clickCount);

                      mineLabel.setFlag(true);

                      temp--;

                      sartframe.getMineState().setTotalMineG(temp);

                 }

 

                 // 第二次,旗子>>>问号

                 if (clickCount == 2) {

                      mineLabel.setIcon(Tools.iiask0);

                      mineLabel.setRightClickCount(clickCount);

                      mineLabel.setFlag(false);

                      temp++;

                      sartframe.getMineState().setTotalMineG(temp);

 

                 }

                 // 第三次,问号>>>雷块

                 if (clickCount == 3) {

                      mineLabel.setIcon(Tools.iiblank);

                      mineLabel.setRightClickCount(0);

                 }

                 // **************************3左键按下************************************

 

           } else if (d1 == InputEvent.BUTTON1_MASK) {

                 // 未展开且不是旗子(图片陷进去)

                 if (!mineLabel.isExpanded() && !mineLabel.isFlag()) {

                      // 问号

                      if (mineLabel.getRightClickCount() == 2) {

                            mineLabel.setIcon(Tools.iiask1);

                            // 雷块

                      } else {

                            mineLabel.setIcon(Tools.mineCount[0]);

                      }

                      // 表情>>惊讶

                      sartframe.getMineState().getNewGame().setIcon(Tools.iiface2);

                 }

 

                 if (mineLabel.isExpanded()) {

                      sartframe.getMineState().getNewGame().setIcon(Tools.iiface0);

                 }

 

           }

 

      }

 

      /**

       * 鼠标释放

       */

      public void mouseReleased(MouseEvent arg0) {

 

           MineLabel mineLabel = (MineLabel) arg0.getSource();

          

           int rowIndex = mineLabel.getRowIndex();

           int colIndex = mineLabel.getColIndex();

 

           int i = arg0.getModifiers();

          

          

           // ***************双键释放*********************************

           if (isDouble) {

                 // 2 表情惊讶还原

                 sartframe.getMineState().getNewGame().setIcon(Tools.iiface0);

                 doubleReleased(rowIndex, colIndex, mineLabel.getMineCount());

                 isDouble = false;

                 isMind();

 

                

                

                 // ***************左键释放********************************

           } else if (i == InputEvent.BUTTON1_MASK) {

                 if (!mineLabel.isExpanded() && !mineLabel.isFlag()) {

                      /**

                       * 鼠标第一次点击雷区时计时器开始计时

                       */

 

                      if (!sartframe.isStart()) {

                            sartframe.getTimer().start();

                            sartframe.setStart(true); // 游戏开始

                      }

                      // 踩到雷,展开所有雷

 

                      if (mineLabel.isMine() && !mineLabel.isFlag()) {

                            openMine(rowIndex, colIndex);

 

                            /**

                             * 踩到雷时计时器停止计时,游戏结束,

                             */

                            sartframe.getMineState().getNewGame()

                                       .setIcon(Tools.iiface3);

 

                            sartframe.getTimer().stop();

                            sartframe.setStart(false);

 

                            // 没有踩到雷,展开安全雷区

                      } else {

                            sartframe.getMineState().getNewGame()

                                       .setIcon(Tools.iiface0);

                            open(rowIndex, colIndex);

                            isMind();

                      }

                 } else if (mineLabel.isExpanded()) {

                      sartframe.getMineState().getNewGame().setIcon(Tools.iiface0);

                 }

           }

      }

 

 

     

      public void doubleReleased(int rowIndex, int colIndex, int count) {

           /**

            * flagBeside 周围8个位置的旗子数

            *

            */

           int flagBeside = 0;

           for (int x = Math.max(rowIndex - 1, 0); x <= Math.min(rowIndex + 1,

                      Tools.totalx - 1); x++) {

                 for (int y = Math.max(colIndex - 1, 0); y <= Math.min(colIndex + 1,

                            Tools.totaly - 1); y++) {

                      if (sartframe.getMineField().getMineLabel()[x][y].isFlag()) {

                            flagBeside++;

                      }

 

                 }

           }

 

           // 1 当前雷块未展开或者旗子数与数字不相等,图片还原

           if (!sartframe.getMineField().getMineLabel()[rowIndex][colIndex]

                      .isExpanded()

                      || sartframe.getMineField().getMineLabel()[rowIndex][colIndex]

                                  .getMineCount() != flagBeside) {

                 doublePressedBeside(rowIndex, colIndex, 2);

           }

 

           // 2 已经展开且周围雷数和旗子相等,判断周围8个方向是否有踩到雷(是雷,但没有旗子)

           if (sartframe.getMineField().getMineLabel()[rowIndex][colIndex]

                      .isExpanded()

                      && sartframe.getMineField().getMineLabel()[rowIndex][colIndex]

                                  .getMineCount() == flagBeside) {

                 boolean isBobm = false;

                 for (int x = Math.max(rowIndex - 1, 0); x <= Math.min(rowIndex + 1,

                            Tools.totalx - 1); x++) {

                      for (int y = Math.max(colIndex - 1, 0); y <= Math.min(

                                  colIndex + 1, Tools.totaly - 1); y++) {

                            if (sartframe.getMineField().getMineLabel()[x][y].isMine()

                                       && !sartframe.getMineField().getMineLabel()[x][y]

                                                  .isFlag()) {

                                  isBobm = true;

                                  break;

                            } else if (!sartframe.getMineField().getMineLabel()[x][y]

                                       .isExpanded()) {

                                  open(x, y);

                            }

                      }

                 }

                 // 踩到雷

                 if (isBobm) {

                      openMine(rowIndex, colIndex);

                 }

 

           }

 

      }

 

     

      public void doublePressedBeside(int rowIndex, int colIndex, int doubleType) {

 

           doublePressed(rowIndex, colIndex, doubleType);

 

           for (int x = Math.max(rowIndex - 1, 0); x <= Math.min(rowIndex + 1,

                      Tools.totalx - 1); x++) {

                 for (int y = Math.max(colIndex - 1, 0); y <= Math.min(colIndex + 1,

                            Tools.totaly - 1); y++) {

                      doublePressed(x, y, doubleType);

                 }

           }

 

      }

     

      public void doublePressed(int i, int j, int doubleType) {

           MineLabel minelabel = sartframe.getMineField().getMineLabel()[i][j];

 

           if (!minelabel.isExpanded() && !minelabel.isFlag()) {

 

                 if (doubleType == 1) {

 

                      if (minelabel.getRightClickCount() == 2) {

 

                            minelabel.setIcon(Tools.iiask1);

 

                      } else {

                            minelabel.setIcon(Tools.mineCount[0]);

 

                      }

                 } else {

                      if (minelabel.getRightClickCount() == 2) {

                            minelabel.setIcon(Tools.iiask0);

                      } else {

 

                            minelabel.setIcon(Tools.iiblank);

 

                      }

                 }

           }

      }

 

      public void openMine(int i, int j) {

           for (int m = 0; m < Tools.totalx; m++) {

                 for (int n = 0; n < Tools.totaly; n++) {

                      MineLabel mineLabel = sartframe.getMineField().getMineLabel()[m][n];

                      //是雷的情况  且不是旗子

                      if (mineLabel.isMine() && !mineLabel.isFlag()) {

 

                            if (i == m && j == n) {

 

                                  mineLabel.setIcon(Tools.iiblood);

                            } else {

                                  mineLabel.setIcon(Tools.iimine);

                            }

                           

                      } else if (!mineLabel.isMine() && mineLabel.isFlag()) {

                            mineLabel.setIcon(Tools.iierror);

                            sartframe.getMineState().getNewGame()

                            .setIcon(Tools.iiface3);

                            sartframe.getTimer().stop();

                            sartframe.setStart(false);

                           

                      }

                      mineLabel.removeMouseListener(sartframe.getMineField()

                                  .getMouseListener());

                 }

           }

      }

 

 

     

      private void open(int rowIndex, int colIndex) {

 

           MineLabel mineLabel = sartframe.getMineField().getMineLabel()[rowIndex][colIndex];

           if (!mineLabel.isExpanded() && !mineLabel.isFlag()) {

                 int count = mineLabel.getMineCount();

 

                 mineLabel.setIcon(Tools.mineCount[count]);

 

                 mineLabel.setExpanded(true);

                 expendedCount++;

 

                 if (count == 0) {

                      for (int x = Math.max(rowIndex - 1, 0); x <= Math.min(

                                  rowIndex + 1, Tools.totalx - 1); x++) {

                            for (int y = Math.max(colIndex - 1, 0); y <= Math.min(

                                       colIndex + 1, Tools.totaly - 1); y++) {

                                  open(x, y);

                            }

                      }

                 }

           }

 

      }

 

 

      public void isMind() {

           // TODO Auto-generated method stubt;

           if (Tools.totalx * Tools.totaly - expendedCount == Tools.totalMine) {

 

                 for (int g = 0; g < Tools.totalx; g++)

                      for (int h = 0; h < Tools.totaly; h++) {

                            if (sartframe.getMineField().getMineLabel()[g][h].isMine()

                                       && !sartframe.getMineField().getMineLabel()[g][h]

                                                  .isFlag()) {

                                  sartframe.getMineField().getMineLabel()[g][h]

                                             .setIcon(Tools.iiflag);

                            }

                            // 移除监听

                            sartframe.getMineField().getMineLabel()[g][h]

                                       .removeMouseListener(sartframe.getMineField()

                                                  .getMouseListener());

 

                      }

                 sartframe.getMineState().getNewGame().setIcon(Tools.iiface4);

 

                 sartframe.getMineState().setTotalMineG(0);

 

                 sartframe.getTimer().stop();

                 new Win(sartframe);

                

 

                 //成功后弹出英雄记录版

                

                  

                 sartframe.setStart(false);

           }

 

      }

 

 

}package com.sf.minesweeper.listener;

 

import java.awt.event.InputEvent;

import java.awt.event.MouseEvent;

 

import com.sf.minesweeper.bean.MineLabel;

 

public class MouseMotionListener implements java.awt.event.MouseMotionListener{

 

      public void mouseDragged(MouseEvent e) {

           // 获取当前点击的雷块和其行列索引

           MineLabel mineLabel = (MineLabel) e.getSource();

           int rowIndex = mineLabel.getRowIndex();

           int colIndex = mineLabel.getColIndex();

           // 获取点击参数

           int d = e.getModifiersEx();

           int i = e.getModifiers();

 

           if (d == InputEvent.BUTTON1_DOWN_MASK + InputEvent.BUTTON3_DOWN_MASK) {

 

           } else if (i == InputEvent.BUTTON3_MASK && !mineLabel.isExpanded()) {

 

           } else if (i == InputEvent.BUTTON1_MASK) {

 

           }

      }

 

      public void mouseMoved(MouseEvent e) {

           // 获取当前点击的雷块和其行列索引

           MineLabel mineLabel = (MineLabel) e.getSource();

           int rowIndex = mineLabel.getRowIndex();

           int colIndex = mineLabel.getColIndex();

           // 获取点击参数

           int d = e.getModifiersEx();

           int i = e.getModifiers();

 

           if (d == InputEvent.BUTTON1_DOWN_MASK + InputEvent.BUTTON3_DOWN_MASK) {

 

           } else if (i == InputEvent.BUTTON3_MASK && !mineLabel.isExpanded()) {

 

           } else if (i == InputEvent.BUTTON1_MASK) {

 

           }

      }

 

}package com.sf.minesweeper.menu;

 

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

 

import com.sf.minesweeper.bean.MineLabel;

import com.sf.minesweeper.dialog.ShowWin;

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.listener.MineMenuListener;

import com.sf.minesweeper.tools.Tools;

 

public class MineMenu extends JMenuBar {

      /**

       *

       */

 

      private static final long serialVersionUID = -5997735339754706915L;

      //SartFrame sartFrame;

      public MineMenu(SartFrame sartFrame){

           this.sartFrame=sartFrame;

          

           MineMenuListener listener=new MineMenuListener(sartFrame);

          

           JMenu fileMenu = new JMenu("游戏(G)");

           fileMenu.setMnemonic('G');

          

           JMenuItem newGameItem = new JMenuItem("开局(N)");

           newGameItem.setMnemonic('N');

           fileMenu.add(newGameItem);

           newGameItem.addActionListener(listener);

          

           JMenuItem lowItem = new JMenuItem("初级(B)");

           lowItem.setMnemonic('B');

           fileMenu.add(lowItem);

           lowItem.addActionListener(listener);

          

           JMenuItem midItem = new JMenuItem("中级(I)");

           midItem.setMnemonic('I');

           fileMenu.add(midItem);

           midItem.addActionListener(listener);

          

           JMenuItem highItem = new JMenuItem("高级(E)");

           highItem.setMnemonic('E');

           fileMenu.add(highItem);

           highItem.addActionListener(listener);

          

           JMenuItem orderItem = new JMenuItem("自定义(C)");

           orderItem.setMnemonic('C');

           fileMenu.add(orderItem);

           orderItem.addActionListener(listener);

 

           JMenuItem heroMenu = new JMenuItem("扫雷英雄榜(T)");

           //JMenu heroMenu = new JMenu("扫雷英雄榜(T)");

           heroMenu.setMnemonic('T');

          

           heroMenu.addActionListener(new ActionListener(){

                 public void actionPerformed(ActionEvent e) {

                      System.out.println("sfgd");

                      new ShowWin(getSartFrame());

                 }

                  

              });

           fileMenu.add(heroMenu);

 

          

           JMenuItem exitItem = new JMenuItem("退出(X)");       

           exitItem.addActionListener(new ActionListener() {    //加一个系统退出的处理监听

                 public void actionPerformed(ActionEvent e) {

                      System.exit(0);

 

                 }

           });

           fileMenu.add(exitItem);

          

           JMenu aboutMenu = new JMenu("帮助(H)");

           aboutMenu.setMnemonic('H');

          

 

           JMenu aboutItem = new JMenu("版权信息(A)");

           aboutMenu.add(aboutItem);

           JMenuItem about2 = new JMenuItem("版本号为20135108 所有权归李泽源所有");

           aboutItem.add(about2);

          

           JMenuItem weiGuaItem = new JMenuItem("此处并没有外挂(W)");

           aboutMenu.add(weiGuaItem);

 

           weiGuaItem.addActionListener(new ActionListener() {

                 public void actionPerformed(ActionEvent e) {

                      // TODO Auto-generated method stub

                      if (MineMenu.this.sartFrame.isStart()) {

                            // 判断每一个雷块是否是雷

                            for (MineLabel[] mineLabel : MineMenu.this.sartFrame

                                       .getMineField().getMineLabel()) {

                                  for (MineLabel m : mineLabel) {

                                       if (m.isMine()) {

                                             m.setIcon(Tools.iihole);

                                       }

                                  }

                            }

                      }

                 }

           });

     

          

           aboutMenu.add(weiGuaItem);

          

           this.add(fileMenu);

           this.add(aboutMenu);

          

          

      }

 

      private SartFrame sartFrame;

 

      public SartFrame getSartFrame(){

           return sartFrame;

      }

 

}package com.sf.minesweeper.panel;

 

import java.awt.BorderLayout;

 

import java.awt.Color;

import java.awt.GridLayout;

 

import javax.swing.BorderFactory;

import javax.swing.JPanel;

import javax.swing.border.Border;

 

import com.sf.minesweeper.bean.MineLabel;

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.listener.MouseListener;

import com.sf.minesweeper.tools.Tools;

 

 

 

 

public class MineField extends JPanel {

     

      SartFrame sartFrame;

      MouseListener mouseListener;

      private MineLabel mineLabel[][];

      public  MineField(SartFrame sartFrame){

          

           this.sartFrame=sartFrame;

           this.setLayout(new BorderLayout());

          

           mineLabel=new MineLabel[Tools.totalx][Tools.totaly];

 

          

           JPanel jPanel2=new JPanel();

           jPanel2.setLayout(new GridLayout(Tools.totalx,Tools.totaly));

 

           mouseListener=new MouseListener(sartFrame);

          

           for(int i=0;i<Tools.totalx;i++){

                 for(int j=0;j<Tools.totaly;j++){

 

                      mineLabel[i][j]=new MineLabel(i,j);

                      mineLabel[i][j].setIcon(Tools.iiblank);

 

                      jPanel2.add(mineLabel[i][j]);

                     

                      mineLabel[i][j].addMouseListener(mouseListener);

 

                 }

           }

 

     

           this.add(jPanel2);         

      }

 

     

     

     

      /**

       * 布雷及算周围雷数

       */

      public void buildMine(int rowx, int coly) {

           /**

            * 布雷(去除当前点击位置,使第一次永远不会点到雷)

            */

 

           for (int i = 0; i < Tools.totalMine; i++) {

                

                 int x = (int) (Math.random() * Tools.totalx);

                 int y = (int) (Math.random() * Tools.totaly);

                 //System.out.println(y);

 

                 if(x==rowx && y==coly){

                      i--;

                 }

                 else if(mineLabel[x][y].isMine()){

                      i--;

                 }else{

                      mineLabel[x][y].setMine(true);

                 }

           }

     

          

           /**

            * 算周围雷数

            */

           for (int i = 0; i < Tools.totalx; i++) {

                 for (int j = 0; j < Tools.totaly; j++) {

                       int count = 0;

                      if (!mineLabel[i][j].isMine()) {

                   

                            // 计算雷块周围八个方向雷数:方法1

                           

                           

//                          /**

//                          * 上

//                          */

//                    if (i > 0) {

//                               if (mineLabel[i - 1][j].isMine()) {

//                                     count++;

//                               }

//                          }

//

//                          /**

//                          * 左上

//                          */

//                          if (i > 0 && j>0) {

//                               if (mineLabel[i - 1][j-1].isMine()) {

//                                     count++;

//                               }

//                          }

//                         

//                          /**

//                          * 右上

//                          */

//                    if (i > 0&&j+1< Tools.totaly) {

//                               if (mineLabel[i - 1][j+1].isMine()) {

//                                     count++;

//                               }

//                          }

//

//                          /**

//                          * 左

//                          */

//                          if (j>0) {

//                               if (mineLabel[i][j-1].isMine()) {

//                                     count++;

//                               }

//                          }

//                         

//                          /**

//                          * 右

//                          */

//                    if (j+1< Tools.totaly) {

//                               if (mineLabel[i][j+1].isMine()) {

//                                     count++;

//                               }

//                          }

//

//                          /**

//                          * 左下

//                          */

//                          if (i+1< Tools.totalx&&j>0) {

//                               if (mineLabel[i + 1][j-1].isMine()) {

//                                     count++;

//                               }

//                          }

//                         

//                          /**

//                          * 下

//                          */

//                    if (i+1< Tools.totalx) {

//                               if (mineLabel[i + 1][j].isMine()) {

//                                     count++;

//                               }

//                          }

//

//                          /**

//                          * 右下

//                          */

//                          if (i+1< Tools.totalx && j+1< Tools.totaly) {

//                               if (mineLabel[i + 1][j+1].isMine()) {

//                                     count++;

//                               }

//                          }

//                         

                           

                           

                            for (int x = Math.max(i - 1, 0); x <= Math.min(i + 1,

                                       Tools.totalx - 1); x++) {

                                  for (int y = Math.max(j - 1, 0); y <= Math.min(j + 1,

                                             Tools.totaly - 1); y++) {

                                       if (mineLabel[x][y].isMine())

                                             count++;

                                  }

                            }

                            mineLabel[i][j].setMineCount(count);

 

                           

                           

                      }

                 }

           }

          

          

      }

 

 

 

 

      public MineLabel[][] getMineLabel() {

           return mineLabel;

      }

 

 

 

 

      public void setMineLabel(MineLabel[][] mineLabel) {

           this.mineLabel = mineLabel;

      }

 

 

 

 

      public MouseListener getMouseListener() {

           return mouseListener;

      }

     

     

 

}package com.sf.minesweeper.panel;

 

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

 

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.border.Border;

 

import com.sf.minesweeper.frame.SartFrame;

import com.sf.minesweeper.tools.Tools;

 

public class MineState extends JPanel {

 

      private JLabel newGame;

 

      private JLabel usedtimeG, usedtimeS, usedtimeB;

 

      private JLabel totalBobmG, totalBobmS, totalBobmB;

     

      Box b;

      SartFrame sartFrame;

      public MineState(SartFrame sartFrame){

      this.sartFrame=sartFrame;

          

           this.setLayout(new BorderLayout());

          

           b = Box.createHorizontalBox();

           b.setBackground(Color.darkGray);

           Border borderOut = BorderFactory.createEmptyBorder(5, 10, 10, 10);

           Border borderIn = BorderFactory.createLoweredBevelBorder();

            Border borderGroup = BorderFactory.createCompoundBorder(borderOut,

                      borderIn);

           b.setBorder(borderGroup);

          

           setBackground(Color.LIGHT_GRAY);

          

           newGame = new JLabel();

           newGame = new JLabel();

           newGame.setIcon(Tools.iiface0);

           newGame.addMouseListener(new MouseAdapter() {

                 public void mousePressed(MouseEvent arg0) {

                      newGame.setIcon(Tools.iiface1);

 

                 }

 

                 public void mouseReleased(MouseEvent arg0) {

                       newGame.setIcon(Tools.iiface0);

                       MineState.this.sartFrame.restart();

                 }

           });

           newGame.setIcon(Tools.iiface0);

          

           totalBobmG = new JLabel();

           totalBobmS = new JLabel();

           totalBobmB = new JLabel();

          

           setTotalMineG(Tools.totalMine);

          

           usedtimeS = new JLabel();

           usedtimeS.setIcon(Tools.timeCount[0]);

          

            usedtimeG = new JLabel();

           usedtimeG.setIcon(Tools.timeCount[0]);

          

           usedtimeB = new JLabel();

           usedtimeB.setIcon(Tools.timeCount[0]);

          

           //b.add(Box.createHorizontalStrut(10));

           b.add(totalBobmB);

           b.add(totalBobmS);

           b.add(totalBobmG);

          

           b.add(Box.createVerticalStrut(35));

           b.add(Box.createGlue());

          

           b.add(newGame);

           b.add(Box.createVerticalStrut(35));

           b.add(Box.createGlue());

          

          

           b.add(usedtimeB);

           b.add(usedtimeS);

           b.add(usedtimeG);

          

          

           this.add(b,BorderLayout.CENTER);

          

          

      }

     

      /**

       * 根据当前旗子数计算剩余雷数

       *

       * @param count

       */

      public void setTotalMineG(int count){

          

           int g = count%10;

           int s = count/10%10;

           int b = count/100;

           if(count<0){

                 totalBobmG.setIcon(Tools.timeCount[-g]);

                 totalBobmS.setIcon(Tools.timeCount[-s]);

                 totalBobmB.setIcon(Tools.timeCount[10]);

                

           }else{

                 totalBobmG.setIcon(Tools.timeCount[g]);

                 totalBobmS.setIcon(Tools.timeCount[s]);

                 totalBobmB.setIcon(Tools.timeCount[b]);

           }

      }

 

      public JLabel getNewGame() {

           return newGame;

      }

 

      public JLabel getUsedtimeG() {

           return usedtimeG;

      }

 

      public JLabel getUsedtimeS() {

           return usedtimeS;

      }

 

      public JLabel getUsedtimeB() {

           return usedtimeB;

      }

 

      public JLabel getTotalBobmG() {

           return totalBobmG;

      }

 

      public JLabel getTotalBobmS() {

           return totalBobmS;

      }

 

      public JLabel getTotalBobmB() {

           return totalBobmB;

      }

 

      public Box getB() {

           return b;

      }

     

     

}package com.sf.minesweeper.timer;

 

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

import com.sf.minesweeper.panel.MineState;

import com.sf.minesweeper.tools.Tools;

 

public class Timers implements ActionListener{

 

      private int times;

      MineState mineState;

      public Timers(MineState mineState){

           this.mineState = mineState;

      }

      @Override

      public void actionPerformed(ActionEvent arg0) {

           // TODO Auto-generated method stub

           Tools.time++;

           if(Tools.time>999){

                 Tools.time=999;

           }else{

                 int g = Tools.time%10;

                 int s = Tools.time/10%10;

                 int b = Tools.time/100;

                

                 mineState.getUsedtimeG().setIcon(Tools.timeCount[g]);

                 mineState.getUsedtimeS().setIcon(Tools.timeCount[s]);

                 mineState.getUsedtimeB().setIcon(Tools.timeCount[b]);

                

           }

      }

     

     

 

}package com.sf.minesweeper.tools;

 

import java.awt.Image;

 

import javax.swing.ImageIcon;

 

public class Tools {

     

      public static ImageIcon start = new ImageIcon("./image/start.png");

      /**

       * totalx,totaly,totalBobm 分别对应雷区的总行数,总列数和总雷数

       */

      public static int totalx = 9;

      public static int totaly = 9;

      public static int totalMine = 10;

     

     

      public static int time = 0;

      public static int time1= 999;

      public static int time2= 999;

      public static int time3= 999;

      public static String name1="匿名";

      public static String name2="匿名";

      public static String name3="匿名";

     

 

      /**

       * 游戏等级

       */

      public static final String LOWER_LEVEL = "初级";

      public static final String MIDDLE_LEVEL = "中级";

      public static final String HEIGHT_LEVEL = "高级";

      public static final String CUSTOM_LEVEL = "自定义";

      /**

       * 游戏当前等级

       */

      public static String currentLevel = LOWER_LEVEL;

 

 

      /**

       *  用来存放0-8数字标签图片

       *

       */

 

      public static ImageIcon mineCount[];

 

      /**

       *  用来存放 d0-d10时间标签图片

       *

       */

 

      public static ImageIcon timeCount[];

      static {

 

           mineCount = new ImageIcon[9];

           for (int i = 0; i <= 8; i++) {

                 mineCount[i] = new ImageIcon("./image/" + i + ".gif");

           }

 

           timeCount = new ImageIcon[11];

           for (int i = 0; i < 10; i++) {

                 timeCount[i] = new ImageIcon("./image/d" + i + ".gif");

           }

           timeCount[10] = new ImageIcon("./image/d10.gif");

      }

 

      /**

       * 分别对应face0-face9表情标签图片

       *

       */

 

      public static ImageIcon iiface0 = new ImageIcon("./image/face0.gif");

 

      public static ImageIcon iiface1 = new ImageIcon("./image/face1.gif");

 

      public static ImageIcon iiface2 = new ImageIcon("./image/face2.gif");

 

      public static ImageIcon iiface3 = new ImageIcon("./image/face3.gif");

 

      public static ImageIcon iiface4 = new ImageIcon("./image/face4.gif");

 

      public static ImageIcon iiface5 = new ImageIcon("./image/face5.gif");

 

      public static ImageIcon iiface6 = new ImageIcon("./image/face6.gif");

 

      public static ImageIcon iiface7 = new ImageIcon("./image/face7.gif");

 

      public static ImageIcon iiface8 = new ImageIcon("./image/face8.gif");

 

      public static ImageIcon iiface9 = new ImageIcon("./image/face9.gif");

 

      /**

       * 分别对应 mine-mine3雷表情标签图片

       *

       */

 

      public static ImageIcon iimine = new ImageIcon("./image/mine.gif");

 

      public static ImageIcon iimine0 = new ImageIcon("./image/mine0.gif");

 

      public static ImageIcon iimine1 = new ImageIcon("./image/mine1.gif");

 

      public static ImageIcon iimine2 = new ImageIcon("./image/mine2.gif");

 

      public static ImageIcon iimine3 = new ImageIcon("./image/mine3.gif");

 

      /**

       * 分别对应 ask-ask2问号表情标签图片

       *

       */

 

      public static ImageIcon iiask = new ImageIcon("./image/ask.gif");

 

      public static ImageIcon iiask0 = new ImageIcon("./image/ask0.gif");

 

      public static ImageIcon iiask1 = new ImageIcon("./image/ask1.gif");

 

      public static ImageIcon iiask2 = new ImageIcon("./image/ask2.gif");

 

      public static ImageIcon iiconTemp = new ImageIcon("./image/icon.gif");

 

      public static Image iicon = iiconTemp.getImage();

 

      public static ImageIcon iiblank = new ImageIcon("./image/blank.gif");

 

      public static ImageIcon iiblood = new ImageIcon("./image/blood.gif");

 

      public static ImageIcon iierror = new ImageIcon("./image/error.gif");

 

      /**

       * iiflag 对应旗子表情标签图片

       *

       */

 

      public static ImageIcon iiflag = new ImageIcon("./image/flag.gif");

 

      /**

       * iihole 对应点表情标签图片

       *

       */

 

      public static ImageIcon iihole = new ImageIcon("./image/hole.gif");

 

      /**

       * 皮肤

       */

      public static final String metal = "javax.swing.plaf.metal.MetalLookAndFeel";

 

      public static final String motif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";

 

      public static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

 

}

4.总结与体会

PSP时间:

                                                                

步骤

耗时

百分比

需求分析

40

11%

设计

50

14%

代码实现

180

50%

测试

60

17%

分析总结

30

8%

 

体会:通过结对项目,我认识到了合作的重要性,紧密的合作能够提高我们的能力。代码测试过程中出现很多错误,但经过互相的合作和探讨,加以改进,便成功运行

通过本次实验,学会了如何使用github来管理代码。如果是开源的项目,通过网站托管方式进行统一管理,当然是非常棒的,并且有很多功能,如果不能开源,仅搭建内部Git服务器进行配置管理,团队管理部分的功能就需要自己开发,相比其他配置工具,在这方面没有太大的优势,当然在各大开源网站上已经有大量这方面的实现共享出来。

同时通过结对项目,我认识到了合作的重要性,紧密的合作能够提高我们的能力。代码测试过程中出现很多错误,但经过互相的合作和探讨,加以改进,便成功运行

 结伴伙伴:20135106 宋世超

 

posted on 2015-06-04 22:14  作业你跟考试私奔吧  阅读(292)  评论(0编辑  收藏  举报

导航