package niit.com.jse;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GameText
{
JButton go;
JLabel score;
JLabel time;
JLabel choose;
JLabel score1;
JLabel time1;
JButton b[]=new JButton[10];
JFrame frame;
JPanel p1;
JPanel p2;
JComboBox box;
Timer t;
Timer t1;
Timer t2;
Timer t3;
int ran=0;
boolean flog=true;
public GameText()
{
frame=new JFrame("小游戏");
go=new JButton("Start Game");
score=new JLabel("Score");
time=new JLabel("Time");
choose=new JLabel("Class");
score1=new JLabel("0");
time1=new JLabel("0");
box=new JComboBox();
box.addItem("Easy");
box.addItem("Middle");
box.addItem("Difficilt");
p1=new JPanel();
p1.setLayout(new GridLayout(1,6));
p1.add(score);
p1.add(score1);
p1.add(time);
p1.add(time1);
p1.add(choose);
p1.add(box);
frame.add(p1,BorderLayout.NORTH);
p2=new JPanel();
p2.setLayout(new GridLayout(3,3,5,5));
for(int i=0;i<9;i++)
{
b[i]=new JButton();
b[i].setBackground(Color.blue);
p2.add(b[i]);
b[i].addActionListener(new goListener());
}
frame.add(p2);
frame.add(go,BorderLayout.SOUTH);
t=new Timer(1000,new timeListener());
t1=new Timer(500,new goListener());
t2=new Timer(300,new goListener());
t3=new Timer(100,new goListener());
//System.out.println(Speed);
go.addActionListener(new goListener());
box.addActionListener(new goListener());
}
public void go()
{
frame.setResizable(false);
frame.setBounds(300, 250, 400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class goListener implements ActionListener
{
public void suanfa()
{
ran=(int)(Math.random()*10);
for(int i=0;i<9;i++)
{
b[i].setIcon(null);
}
if(ran<9)
{
b[ran].setIcon(new ImageIcon("m.jpg"));
}
}
public void actionPerformed(ActionEvent e)
{
Object obj=e.getSource();
Object obj1=box.getSelectedItem();
if(obj==go)
{
t.start();
t1.start();
t2.start();
t3.start();
flog=true;
time1.setText("0");
score1.setText("0");
}
if(flog==true)
{
if(obj==b[ran])
{
score1.setText(""+(Integer.parseInt(score1.getText())+1));
}
}
if(obj1.toString().equals("Easy"))
{
if(obj==t1)
{
suanfa();
}
}
if(obj1.toString().equals("Middle"))
{
if(obj==t2)
{
suanfa();
}
}
if(obj1.toString().equals("Difficilt"))
{
if(obj==t3)
{
suanfa();
}
}
}
}
class timeListener implements ActionListener
{
int a;
public void actionPerformed(ActionEvent arg0)
{
a=Integer.parseInt(time1.getText());
a+=1;
time1.setText(""+a);
if(time1.getText().equals(""+30))
{
t.stop();
t1.stop();
t2.stop();
t3.stop();
flog=false;
}
}
}
public static void main(String[] aa)
{
GameText gt=new GameText();
gt.go();
}
}