Package com.edu.game;
Import the Java. The awt. BorderLayout.
Import the Java. The awt. EventQueue;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
Import the Java. The awt. Event. MouseAdapter;
Import the Java. The awt. Event. MouseEvent;
The import javax.mail. Swing. ImageIcon;
The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
The import javax.mail. Swing. JPanel;
The import javax.mail. Swing. This series;
The import javax.mail. Swing. Border. EmptyBorder;
The import javax.mail. Swing. JLabel;
Import the Java. The awt. The Font;
Public class Saolei extends JFrame {
Private JPanel contentPane.//global panel
Private JButton facebtn;//the top title
Private JLabel lbtitle;//smile button
Private Boolean start=true; Does the game start//said
Private JLabel leiNum;//show the rest of the ray number of tag
Private int num=5;//the number of the selected ray - a total of five
Private int blNum=0;//logo right by the number of ray
/* *
* to Launch the application.
*/
Public static void main (String [] args) {
EventQueue. InvokeLater (new Runnable () {
Public void the run () {
Try {
Saolei frame=new Saolei ();
Frame. SetVisible (true);
} the catch (Exception e) {
e.printStackTrace();
}
}
});
}
/* *
* Create the frame.
*/
Public Saolei () {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SetBounds (100, 100, 600, 650);
ContentPane=new JPanel ();
ContentPane. SetBorder (new EmptyBorder (5, 5, 5, 5));
ContentPane. SetLayout (null);
setContentPane(contentPane);
init();//initialized to form
}
Public void init () {
LeiNum=new JLabel (" ");
LeiNum. SetFont (new Font (" tahoma ", the Font BOLD, 18));
,34,200,15 leiNum. SetBounds (33);
LeiNum. SetText (" ray number remaining: "+ num);
ContentPane. Add (leiNum);
Facebtn=new JButton (" ");
Facebtn. SetBounds (284, 13, 40, 40).
ContentPane. Add (facebtn);
Facebtn. SetIcon (new ImageIcon (" images/face0. PNG "));
Facebtn. AddActionListener (new ActionListener () {
@ Override
Public void actionPerformed (an ActionEvent e) {
//judgment - the game state - if fail
if (! Start) {
Start=true;
//change the picture
Facebtn. SetIcon (new ImageIcon (" images/face0. PNG "));
ContentPane. RemoveAll ();//will be in the form of all the contents of the remove
ContentPane. Repaint ();//redraw
//start
init();
}
}
});
ContentPane. Add (facebtn);
Lbtitle=new JLabel (" \ u6E38 \ u620F \ u5F00 \ u59CB ");
Lbtitle. SetFont (new Font (" tahoma ", the Font. PLAIN, 19));
Lbtitle. SetBounds (357, 25, 132, 28).
//contentPane. Add (lbtitle. SetBorder (null)
ContentPane. Add (lbtitle);
Addlei ();
}
Public void addlei () {
System. Out.println (" addlei... ");
Int [] [] map=Leiqu. Initlei ();
//TODO automatically generated method stub
//add ray cycle -- - 5 line 10 columns I, j control the thunder piece of the number of x, y axis coordinate
Int y=70;
int i, j;
for (i=0; I & lt; 10; I++) {
Int x=50;
For (j=0; J & lt; 10; J++) {
//add a minefield - button
JButton lei=new JButton (" ");
//set button icon
Lei. SetIcon (new ImageIcon (" images/flag0. JPG "));
//set the position and size for the button
Lei. SetBounds (x, y, 50, 50);
ContentPane. Add (lei);
Lei. SetToolTipText (I + "-" + j);
//lei. SetBorder (null);
//mouse to monitor events
//left key judgment two-dimensional arrays of - 9 explosion not 9 is normal open
Lei. AddActionListener (new ActionListener () {
@ Override
Public void actionPerformed (an ActionEvent e) {
//TODO automatically generated method stub
if(! Start) {
return;
}
System. The out. Println (" left click ");
//judgment - what are the location of the two-dimensional array number, if it is 9-9 explosion - if not, normally open
A String of text=lei. GetToolTipText ();
//by using text 2-3 and two dimensional array binding
The split String []=text. The split (" - ");
Int x=Integer. ParseInt (split [0]).
Int y=Integer. ParseInt (split [1]).
System. The out. Println (" x "+ x);
System. The out. Println (" y "+ y);
If (map [x] [y]==9) {
Lei. SetIcon (new ImageIcon (" images/bomb0. JPG "));
Facebtn. SetIcon (new ImageIcon (" images/face2. PNG "));
Lbtitle. SetText (" game over ");
Start=false;
} else {
Lei. SetIcon (new ImageIcon (" images/" + map [x] [y] + ". JPG "));
}
}
});
//right
Lei. AddMouseListener (new MouseAdapter () {
Public void mouseClicked (MouseEvent e) {
if(! Start) {
return;
}
/right/left and single and double click
If (um participant etButton ()==MouseEvent. BUTTON3) {//right
If (num==0) {//that ray all finished the
If (blNum==5) {
Facebtn. SetIcon (new ImageIcon (" images/face1. PNG "));
Lbtitle. SetText (" win ");
return;
}
Lbtitle. SetText (" error ");
return;
}
Num -;
LeiNum. SetText (" ray number remaining: "+ num);
Lei. SetIcon (new ImageIcon (" images/flag1. JPG "));
A String of text=lei. GetToolTipText ();
//by using text 2-3 and 2 d array binding
The split String []=text. The split (" - ");
Int x=Integer. ParseInt (split [0]).
Int y=Integer. ParseInt (split [1]).
If (map [x] [y]==9) {
//the ray position right
BlNum++;
}
}
}
});
X +=50;
}
Y +=50;
}
nullnullnullnullnullnullnullnull