📄 saolei.java
字号:
//*********************************/
//扫雷
//
//作者:Zou_Ke
//
//时间:2003.10.15
//*********************************/
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.math.*;
import javax.swing.Timer;
import java.util.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.net.*;
class saolei extends JFrame
{
int x;int y;int mineSum; //行数、列数、雷数
JLabel Lminenum;
JLabel Ltime=new JLabel("000");
JButton btnface=null;
JPanel minepanel=null;
JButton[][] btnmine=null; //按钮数组
int[][] back=null; //后台状态
int btnwidth=22; //按钮宽度
int btnheight=21; //按钮高度
Vector vec=new Vector();
int ttime=0;int leftmines;
Timer jishiqi=null;
Font font1=new Font("宋体",Font.PLAIN,12);
Color color1=new Color(128,128,128);
int LocationX = 100;
int LocationY = 100;
public saolei(int xx,int yy,int LeiSum)
{
x=xx;y=yy;mineSum=LeiSum;leftmines=mineSum;
setTitle("扫雷");
setBounds(LocationX,LocationY,btnwidth*y,btnheight*x+94);
setResizable(false);
Lminenum=new JLabel(""+mineSum);
Lminenum.setForeground(Color.red);
Lminenum.setFont(new Font("Impact",Font.BOLD,15));
//Lminenum.setHorizontalAlignment(JLabel.CENTER);
Ltime.setForeground(Color.red);Ltime.setFont(new Font("Impact",Font.BOLD,15));
//Ltime.setHorizontalTextPosition(JLabel.LEFT);
jishiqi=new Timer(1000,
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ttime++;
if(ttime<10) Ltime.setText("00"+ttime);
else if(ttime<100) Ltime.setText("0"+ttime);
else Ltime.setText(""+ttime);
}
});
Container con=getContentPane();
JMenuBar menubar=new JMenuBar();
JMenu jm1=new JMenu("游戏");jm1.setFont(font1);
JMenu jm2=new JMenu("帮助");jm2.setFont(font1);
JMenuItem j1=new JMenuItem("开局");j1.setFont(font1);
JMenuItem j2=new JMenuItem("初级");j2.setFont(font1);
JMenuItem j3=new JMenuItem("中级");j3.setFont(font1);
JMenuItem j4=new JMenuItem("高级");j4.setFont(font1);
JMenuItem j5=new JMenuItem("自定义…");j5.setFont(font1);
JMenuItem j6=new JMenuItem("退出");j6.setFont(font1);
JMenuItem j7=new JMenuItem("英雄榜");j7.setFont(font1);
JMenuItem j8=new JMenuItem("关于扫雷");j8.setFont(font1);
JMenuItem j9=new JMenuItem("帮助");j9.setFont(font1);
j1.addActionListener(new MenuSelect());
j2.addActionListener(new MenuSelect());
j3.addActionListener(new MenuSelect());
j4.addActionListener(new MenuSelect());
j5.addActionListener(new MenuSelect());
j6.addActionListener(new MenuSelect());
j7.addActionListener(new MenuSelect());
j8.addActionListener(new MenuSelect());
j9.addActionListener(new MenuSelect());
jm1.add(j1);jm1.add(j2);jm1.add(j3);jm1.add(j4);jm1.add(j5);
jm1.add(j6);jm2.add(j7);jm2.add(j9);jm2.add(j8);
menubar.add(jm1);
menubar.add(jm2);
setJMenuBar(menubar);
URL ready = saolei.class.getResource("ready.gif");
btnface=new JButton(new ImageIcon(ready));
btnface.setBorder(new BevelBorder(BevelBorder.RAISED,Color.white,Color.gray));
btnface.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
new saolei(x,y,mineSum);
}
});
JPanel optionpanel=new JPanel();
optionpanel.setBorder(new EtchedBorder());
optionpanel.setLayout(new FlowLayout(FlowLayout.CENTER,30,0));
optionpanel.add(Lminenum);optionpanel.add(btnface);optionpanel.add(Ltime);
//optionpanel.setBackground(color1);
minepanel=new JPanel();
minepanel.setBorder(new EtchedBorder());
minepanel.setLayout(new GridLayout(x,y,0,0));
//minepanel.setBackground(color1);
btnmine=new JButton[x+2][y+2];
back=new int[x+2][y+2];
//初始化地雷
for(int i=0;i<x+2;i++)
{
for(int j=0;j<y+2;j++)
{
btnmine[i][j]=new JButton();
if(i!=0&&j!=0&&i!=x+1&&j!=y+1)
{
btnmine[i][j].setBorder(new BevelBorder(BevelBorder.RAISED,Color.white,Color.gray));
btnmine[i][j].addMouseListener(new MouthAct());
minepanel.add(btnmine[i][j]);
}
back[i][j]=0;
}
}
//随机布雷
int t=0;
while(t<mineSum)
{
int i=1+(int)(Math.random()*(x-1));
int j=1+(int)(Math.random()*(y-1));
if(back[i][j]==0)
{
back[i][j]=1;
t++;
}
}
con.add(optionpanel,BorderLayout.NORTH);
con.add(minepanel,BorderLayout.CENTER);
show();
}
public static void main(String []args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception err){}
saolei sl=new saolei(9,9,10);
sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//扫描地雷
public int count(int coux,int couy)
{
int sum=0;
for(int i=coux-1;i<=coux+1;i++)
{
for(int j=couy-1;j<=couy+1;j++)
{
if(back[i][j]==1) sum++;
}
}
btnmine[coux][couy].setBorder(new BevelBorder(BevelBorder.LOWERED));
URL leinumber = saolei.class.getResource(""+sum+".gif");
btnmine[coux][couy].setIcon(new ImageIcon(leinumber));
return sum;
}
public void scan(Vector vec)
{
while(vec.size()>0)
{
int []postemp=(int [])vec.get(0);
for(int i=postemp[0]-1;i<=postemp[0]+1;i++)
{
for(int j=postemp[1]-1;j<=postemp[1]+1;j++)
{
if(i>0&&i<x+1&&j>0&&j<y+1)
{
if(btnmine[i][j].getIcon()==null)
{
if(count(i,j)==0)
{
int []tmp=new int[]{i,j};
if(!vec.contains(tmp)) vec.add(tmp);
}
}
}
}
}
vec.remove(0);
}
}
//鼠标事件
class MouthAct implements MouseListener
{
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e)
{
URL btnimg = saolei.class.getResource("relax.gif");
btnface.setIcon(new ImageIcon(btnimg));
int count=0;
for(int i=1;i<x+1;i++)
{
for(int j=1;j<y+1;j++)
{
String s=""+btnmine[i][j].getIcon();
if(s.endsWith("WarningS.gif")||btnmine[i][j].getIcon()==null) count++;
}
}
if(count==mineSum)
{
jishiqi.stop();
URL winimg = saolei.class.getResource("win.gif");
btnface.setIcon(new ImageIcon(winimg));
JOptionPane.showMessageDialog(null,"你赢了!");
for(int i=1;i<x+1;i++)
{
for(int j=1;j<y+1;j++)
{
if(back[i][j]==1)
{
URL warning = saolei.class.getResource("WarningS.gif");
btnmine[i][j].setIcon(new ImageIcon(warning));
}
}
}
if(x*y==81) new Record(0,ttime);
else if(x*y==256) new Record(1,ttime);
else if(x*y==480) new Record(2,ttime);
}
}
public void mousePressed(MouseEvent e)
{
if(!jishiqi.isRunning()) jishiqi.start();
URL worry = saolei.class.getResource("worry.gif");
btnface.setIcon(new ImageIcon(worry));
JButton btntmp=(JButton)e.getSource();
int posx=0;int posy=0;
for(int i=1;i<=x;i++)
{
for(int j=1;j<=y;j++)
{
if(btntmp==btnmine[i][j])
{
posx=i;posy=j;
break;
}
}
}
if(e.getButton()==3)
{
if(btntmp.getIcon()==null)
{
if(leftmines>0)
{
URL WarningS = saolei.class.getResource("WarningS.gif");
btntmp.setIcon(new ImageIcon(WarningS));
leftmines--;
Lminenum.setText(""+leftmines);
}
}
else if(btntmp.getIcon().toString().endsWith("WarningS.gif"))
{
btntmp.setIcon(null);
leftmines++;
Lminenum.setText(""+leftmines);
}
}
if(e.getButton()==1&&btntmp.getIcon()==null)
{
if(back[posx][posy]==1)
{
jishiqi.stop();
for(int i=1;i<x+1;i++)
{
for(int j=1;j<y+1;j++)
if(back[i][j]==1)
{
URL MineS = saolei.class.getResource("MineS.gif");
((JButton)btnmine[i][j]).setIcon(new ImageIcon(MineS));
((JButton)btnmine[i][j]).setBorder(new BevelBorder(BevelBorder.LOWERED));
}
}
URL WrongS = saolei.class.getResource("WrongS.gif");
btntmp.setIcon(new ImageIcon(WrongS));
URL Cry = saolei.class.getResource("Cry.gif");
btnface.setIcon(new ImageIcon(Cry));
try
{
Robot rob=new Robot();
BufferedImage bi=rob.createScreenCapture(new Rectangle(saolei.this.getX(),saolei.this.getY()+94,y*btnwidth,x*btnheight));
JLabel label=new JLabel(new ImageIcon(bi));
getContentPane().add(label,BorderLayout.CENTER);
}
catch(Exception err){}
String msg="<html><font face='隶书' color=red size=6>你踩雷了!</font></html>";
JOptionPane.showMessageDialog(null,msg);
getContentPane().remove(minepanel);
}
else
{
if(count(posx,posy)==0) vec.add(new int[]{posx,posy});
scan(vec);
}
}
}
}
//菜单事件
class MenuSelect implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String choice=((JMenuItem)e.getSource()).getText();
choice=choice.trim();
if(choice.equals("退出"))
{
System.exit(0);
}
if(choice.equals("初级"))
{
dispose();
new saolei(9,9,10);
}
if(choice.equals("中级"))
{
dispose();
new saolei(16,16,40);
}
if(choice.equals("高级"))
{
dispose();
new saolei(16,30,99);
}
if(choice.equals("开局"))
{
dispose();
new saolei(x,y,mineSum);
jishiqi.start();
}
if(choice.equals("自定义…"))
{
new InputInfoDialog(saolei.this);
dispose();
new saolei(x,y,mineSum);
}
if(choice.equals("英雄榜"))
{
new Record();
}
if(choice.equals("帮助"))
{
try
{
Runtime r=Runtime.getRuntime();
Process p=r.exec("hh.exe winmine.chm");
}catch(Exception err){}
}
if(choice.equals("关于扫雷"))
{
new AboutMe(" 扫 雷 ");
}
}
}
class InputInfoDialog extends JDialog
{
protected JTextField rowNum;
protected JTextField colNum;
protected JTextField mineNum;
protected JLabel rowTxt;
protected JLabel colTxt;
protected JLabel mineTxt;
protected JButton btnOK;
protected JButton btnCancel;
public InputInfoDialog(JFrame f)
{
super(f,"请输入…",true);
setBounds(200,100,300,250);
rowTxt=new JLabel("行数");
rowTxt.setBounds(20,20,60,30);rowTxt.setFont(font1);
colTxt=new JLabel("列数");
colTxt.setBounds(20,70,60,30);colTxt.setFont(font1);
mineTxt=new JLabel("雷数");
mineTxt.setBounds(20,120,60,30);mineTxt.setFont(font1);
rowNum=new JTextField(""+x);
rowNum.setBounds(100,20,100,30);
colNum=new JTextField(""+y);
colNum.setBounds(100,70,100,30);
mineNum=new JTextField(""+mineSum);
mineNum.setBounds(100,120,100,30);
btnOK=new JButton("确定");
btnOK.setBounds(50,180,80,30);btnOK.setFont(font1);
btnCancel=new JButton("取消");
btnCancel.setBounds(150,180,80,30);btnCancel.setFont(font1);
Container con=getContentPane();
con.setLayout(null);
con.add(rowTxt);con.add(colTxt);con.add(mineTxt);
con.add(rowNum);con.add(colNum);con.add(mineNum);
con.add(btnOK);con.add(btnCancel);
btnOK.addActionListener(new BtnEvent());
btnCancel.addActionListener(new BtnEvent());
this.setVisible(true);
}
class BtnEvent implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource()==btnOK)
{
String rows=rowNum.getText().trim();
String cols=colNum.getText().trim();
String mines=mineNum.getText().trim();
if(!rows.equals("")&&Integer.parseInt(rows)>9) x=Integer.parseInt(rows);
if(!cols.equals("")&&Integer.parseInt(cols)>9) y=Integer.parseInt(cols);
if(!mines.equals("")&&Integer.parseInt(mines)>10) mineSum=Integer.parseInt(mines);
InputInfoDialog.this.dispose();
}
else
InputInfoDialog.this.dispose();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -