//............................主界面设置 负责人:费能超.............................
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Random;
import java.util.Timer;
import java.awt.Image;
public class Game extends JFrame{
//定义主函数
Image[] images=new Image[2];
Point spotloc=new Point(0,0);
boolean mousepressflag = false;
ImageIcon imgIcon1=new ImageIcon("12.jpg");
ImageIcon imgIcon2=new ImageIcon("1.jpg");
private long minute=0,second=0;
private ExButton[][] btn;
private JLabel label;
private JButton restart;
private JButton a;
private JButton b;
private JButton c;
private JButton d;
private int minesRemained;
private boolean thisTry=true;
private JLabel timeUsed=new JLabel ();
private Random rand=new Random();
private final int ROWS,COLUMNS;
private final int MINES;
//........................主界面设置 负责人:费能超...........................
//......................定义类及参数设置....................................
public Game(int rows,int columns,int mines)
{
super("扫雷");
this.ROWS=rows;
this.COLUMNS=columns;
this.MINES=mines;
minesRemained=MINES;
Timer timer=new Timer();
timer.schedule(new MyTimer(), 0, 1000);
Container container=getContentPane();
container.setLayout(new BorderLayout());
JPanel jpanel=new JPanel();
jpanel.setLayout(new GridLayout(ROWS,COLUMNS));
restart=new JButton("重新开始");
a=new JButton("初级");
b=new JButton("中级");
c=new JButton("高级");
d=new JButton("自定义");
JPanel jpanel2=new JPanel();
jpanel2.setLayout(new FlowLayout());
jpanel2.add(timeUsed);
jpanel2.add(restart);
jpanel2.add(a);
jpanel2.add(b);
jpanel2.add(c);
jpanel2.add(d);
ButtonListener restartHandler=new ButtonListener();
restart.addActionListener(restartHandler);
a.addActionListener(restartHandler);
b.addActionListener(restartHandler);
c.addActionListener(restartHandler);
d.addActionListener(restartHandler);
container.add(jpanel2,BorderLayout.SOUTH);
btn=new ExButton[ROWS+2][COLUMNS+2];
for(int i=0;i<=ROWS+1;i++)
{
for(int j=0;j<=COLUMNS+1;j++)
{
btn[i][j]=new ExButton();
btn[i][j].addMouseListener(new MouseClickHandler());
btn[i][j].setIndex(i,j);
btn[i][j].setVisited(false);
}
}
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
jpanel.add(btn[i][j]);
container.add(jpanel,BorderLayout.CENTER);
JPanel jpanel3=new JPanel ();
JPanel pan=new JPanel ();
label=new JLabel();
label.setText("第12组 JAVA实训 剩余雷数 "+MINES);
jpanel3.add(label);
container.add(jpanel3,BorderLayout.NORTH );
this.addMines();
this.addMinesAround();
}
public boolean loadImages(){
images[0]=Toolkit.getDefaultToolkit().getImage("res/0.jpg");
return true;
}
private void addMines()
{
for(int i=1;i<=MINES;i++)
{
int raInt1=rand.nextInt(ROWS);
int raInt2=rand.nextInt(COLUMNS);
if((raInt1==0)||(raInt2==0)||btn[raInt1][raInt2].getMine())
i--;
else
btn[raInt1][raInt2].setMine(true);
}
}
//.....................................定义类及参数设置....................................
//......................雷数设置及空白处理 负责人:汪迎欢..................................
private void addMinesAround()
{
for(int i=1;i<=ROWS;i++)
{
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
{
btn[i][j-1].addMinesAround();
btn[i][j+1].addMinesAround();
btn[i-1][j-1].addMinesAround();
btn[i-1][j].addMinesAround();
btn[i-1][j+1].addMinesAround();
btn[i+1][j-1].addMinesAround();
btn[i+1][j].addMinesAround();
btn[i+1][j+1].addMinesAround();
}
}
}
}
private void checkEmpty(ExButton button)
{
button.setVisited(true);
int x=button.getRowNumber();
int y=button.getColumnNumber();
button.setBackground(Color.green);
if((button.getMinesAround()==0)&&(x>=1)&&(x<=ROWS)
&&(y>=1)&&(y<=COLUMNS))
{
if(!btn[x][y-1].getVisited())
checkEmpty(btn[x][y-1]);
if(!btn[x][y+1].getVisited())
checkEmpty(btn[x][y+1]);
if(!btn[x-1][y].getVisited())
checkEmpty(btn[x-1][y]);
if(!btn[x+1][y].getVisited())
checkEmpty(btn[x+1][y]);
if(!btn[x-1][y-1].getVisited())
checkEmpty(btn[x-1][y-1]);
if(!btn[x-1][y+1].getVisited())
checkEmpty(btn[x-1][y+1]);
if(!btn[x+1][y-1].getVisited())
checkEmpty(btn[x+1][y-1]);
if(!btn[x+1][y+1].getVisited())
checkEmpty(btn[x+1][y+1]);
}
else if(button.getMinesAround()>0)
button.setText(""+button.getMinesAround());
}
//.......................雷数设置及空白处理 负责人:汪迎欢..................................
//.............................主界面设置 负责人:费能超......................................
public static void main(String args[])
{
String rows,columns,mines;
int rowNumber,columnNumber,mineNumber;
rowNumber=6;
columnNumber=6;
mineNumber=4;
Game frame=new Game(rowNumber,columnNumber,mineNumber);
frame.setTitle("扫雷");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(220, 80);
frame.setSize(500, 500 );
frame.setVisible(true);
}
//.........................主界面设置 负责人:费能超.......................................
//.......................鼠标选择设置 负责人:时良荣.....................................
private class MouseClickHandler extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
ExButton eventButton=new ExButton();
eventButton=(ExButton)event.getSource();
eventButton.setVisited(true);
if(event.isMetaDown())
{
if(eventButton.getText()==".")
{
minesRemained++;
eventButton.setText("");
eventButton.setIcon(imgIcon2);
}
else
{
if((eventButton.getBackground()==Color.red)||
(eventButton.getText()!=""))
{
}
else
{
minesRemained--;
eventButton.setIcon(imgIcon1);
eventButton.setText(".");
}
}
label.setText("第12组 JAVA实训 剩余雷数 "+minesRemained);
if(minesRemained==0)
{
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine()&&btn[i][j].getText()!=".")
thisTry=false;
if(!btn[i][j].getMine()&&btn[i][j].getText()==".")
thisTry=false;
}
if(thisTry)
{
JOptionPane.showMessageDialog(null, "恭喜您!" + " 扫雷成功");
JOptionPane.showMessageDialog(null, ""+ timeUsed.getText());
}
else
JOptionPane.showMessageDialog(null, "你选的雷数超过了雷的总数");
}
}
else if(event.isAltDown())
{
}
else
{
if(eventButton.getText()==".")
{
}
else if(eventButton.getMine())
{
JOptionPane.showMessageDialog(null, "很遗憾!" +"游戏结束" );
for(int i=1;i<=ROWS;i++)
for(int j=1;j<=COLUMNS;j++)
{
if(btn[i][j].getMine())
btn[i][j].setBackground(Color.red);
}
JOptionPane.showMessageDialog(null, "已用时间: 0"+minute+":"+second);
}
else
{
if(eventButton.getMinesAround()==0)
{
checkEmpty(eventButton);
}
else