import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.*;
/**
* This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
* Builder, which is free for non-commercial use. If Jigloo is being used
* commercially (ie, by a corporation, company or business for any purpose
* whatever) then you should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details. Use of Jigloo implies
* acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
* PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
* ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ReactionGame extends javax.swing.JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel jPanel1;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JButton jButton1;
private JPanel jPanel4;
private JButton jButton2;
private JButton jButton4;
private JButton jButton3;
private JPanel jPanel3;
private JLabel jLabel2;
private JLabel jLabel1;
private JPanel jPanel2;
private JMenu jMenu1;
private JMenuBar jMenuBar1;
private JButton jButtonMenuItem2;
private JButton jButtonMenuItem1;
private JTextField[] jTextFieldMenuItem=new JTextField[10];
private ReactionUnit[] units=new ReactionUnit[16];
int number=0;
int sum=0;
int score=0;
int total=0;//the total number of the appeared circle
int best=0;
int click=0;//How many times you press the circle
int time=0;
int lost=0;//lost<total means you forget to press the circle before it disappeared and tries will plus 1
boolean pause;//if you press the pause button
int highest[]={0,0,0,0,0,0,0,0,0,0};
public static void main(String[] args) {
ReactionGame inst = new ReactionGame();
inst.setVisible(true);
}
public ReactionGame() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
BorderLayout jPanel1Layout = new BorderLayout();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(jPanel1Layout);
{
jPanel2 = new JPanel();
GridLayout jPanel2Layout = new GridLayout(1, 1);
jPanel2Layout.setColumns(1);
jPanel2Layout.setHgap(5);
jPanel2Layout.setVgap(5);
jPanel1.add(jPanel2, BorderLayout.NORTH);
jPanel2.setLayout(jPanel2Layout);
jPanel2.setPreferredSize(new java.awt.Dimension(400, 26));
{
jLabel1 = new JLabel();
jPanel2.add(jLabel1);
jLabel1.setText("Tries: 0");
}
{
jLabel2 = new JLabel();
jPanel2.add(jLabel2);
jLabel2.setText("Score: 0");
}
{
jLabel3 = new JLabel();
jPanel2.add(jLabel3);
jLabel3.setText("Sum: 0");
}
{
jLabel4 = new JLabel();
jPanel2.add(jLabel4);
jLabel4.setText("Avg: 0");
}
{
jLabel5 = new JLabel();
jPanel2.add(jLabel5);
jLabel5.setText("Best: 0");
}
}
{
jPanel3 = new JPanel();
jPanel1.add(jPanel3, BorderLayout.SOUTH);
jPanel3.setPreferredSize(new java.awt.Dimension(400, 36));
{
jButton1 = new JButton();
jPanel3.add(jButton1);
jButton1.setText("Start");
jButton1.addActionListener(this);
}
{
jButton2 = new JButton();
jPanel3.add(jButton2);
jButton2.setText("Stop");
jButton2.addActionListener(this);
}
{
jButton3 = new JButton();
jPanel3.add(jButton3);
jButton3.setText("Pause");
jButton3.addActionListener(this);
}
{
jButton4 = new JButton();
jPanel3.add(jButton4);
jButton4.setText("Resume");
jButton4.addActionListener(this);
}
}
{
jPanel4 = new JPanel();
GridLayout jPanel4Layout = new GridLayout(1, 1);
jPanel4Layout.setColumns(4);
jPanel4Layout.setRows(4);
jPanel4Layout.setHgap(1);
jPanel4Layout.setVgap(1);
jPanel1.add(jPanel4, BorderLayout.CENTER);
jPanel4.setLayout(jPanel4Layout);
for (int i = 0; i < 16; i++) {
ReactionUnit a=new ReactionUnit();
units[i]=a;
units[i].addMouseListener(new MouseReactionListener());
jPanel4.add(a);
}
}
}
pack();
setTitle("Reaction Game");
setLocation(400, 200);
{
jMenuBar1 = new JMenuBar();
setJMenuBar(jMenuBar1);
jMenuBar1.setPreferredSize(new java.awt.Dimension(400, 24));
{
jMenu1 = new JMenu();
jMenuBar1.add(jMenu1);
jMenu1.setText("high score");
{
for(int i=0;i<10;i++)
{
jTextFieldMenuItem[i]=new JTextField();
jMenu1.add(jTextFieldMenuItem[i]);
jTextFieldMenuItem[i].setEditable(false);
}
}
{
jButtonMenuItem1 = new JButton();
jMenu1.add(jButtonMenuItem1);
jButtonMenuItem1.setText("Show High Score");
jButtonMenuItem1.addActionListener(this);
}
{
jButtonMenuItem2 = new JButton();
jMenu1.add(jButtonMenuItem2);
jButtonMenuItem2.setText("Clean High Score");
jButtonMenuItem2.addActionListener(this);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getbest(int h)//get the top 10
{
for(int i=0;i<10;i++)
{ if(h>highest[i])
{
for(int j=9;j>i;j--)
highest[j]=highest[j-1];
highest[i]=h;
break;
}
}
}
public void showbest()//print the top 10
{
for(int i=0;i<10;i++)
jTextFieldMenuItem[i].setText((i+1)+":"+highest[i]);
}
private void updateLabels(int j,int k){
jLabel1.setText("Tries: "+(total-click));
jLabel2.setText("Score: "+j);
jLabel3.setText("Sum: "+k);
if(total==0)
jLabel4.setText("Avg: 0");
else
jLabel4.setText("Avg: "+(k/total));
best=best>=j?best:j;
jLabel5.setText("Best: "+best);
}
private class GameThread extends Thread{
public void run() {
while((total-click)<5)//tries is from 0 to 4, everyone has 5 chance
{
synchronized (this)
{
if (pause) //when you press pause button, the thread will wait until be notified.
{
try
{
wait();
} catch (Exception e)
{
}
}
else
{
try
{
Random r=new Random();
number=r.nextInt(16);
units[number].startMeasure();
total++;
time=r.nextInt(1000)+1000;
sleep(time);
if(lost<total)//when you forget to press
{
score=0;
sum+=score;
lost++;
updateLabels(score,sum);
}
}
catch(InterruptedException ie)
{
}
}
}
}
time=0;
getbest(sum);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jButtonMenuItem1)
{
showbest();
}
if(e.getSource()==jButtonMenuItem2)
{
for(int i=0;i<10;i++)
{
highest[i]=0;
}
}
if (e.getSource() == jButton1)
{
time++;
if(time==1)//you can't press the start button when the game is running
{
number=0;
sum=0;
score=0;
total=0;
best=0;
click=0;
updateLabels(0,0);
new GameThread().start();
}
}
if (e.getSource() == jButton2)
{
time=0;
units[number].getScore();
updateLabels(score,sum);
total=6;
click=0;
new GameThread().start();
}
if (e.getSource() == jButton3)
{
synchronized (new GameThread())
{
pause = true;
}
}
if (e.getSource() == jButton4)
{
GameThread thread = new GameThread();
thread.start();
synchronized (thread)
{
pause = false;
thread.notify();
}
}
}
public class MouseReactionListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
lost++;
// System.out.println(e.getX()+","
评论0