Created
April 15, 2012 11:31
-
-
Save Cherubi/2392025 to your computer and use it in GitHub Desktop.
Nopeustesti
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package nopeustesti; | |
import java.awt.Container; | |
import java.awt.GridLayout; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.Random; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
/** | |
* | |
* @author Cherubi | |
*/ | |
public class Nopeustesti implements Runnable, ActionListener { | |
private JFrame kehys; | |
private int painalluksia; | |
private long aloitusAika; | |
public Nopeustesti() { | |
painalluksia = 0; | |
} | |
@Override | |
public void run() { | |
this.kehys = new JFrame("Nopeustesti"); | |
luoNappulat(kehys.getContentPane()); | |
kehys.setVisible(true); | |
} | |
private void luoNappulat(Container sisalto) { | |
sisalto.setLayout(new GridLayout(1,4)); | |
JButton nappula1 = new JButton("1"); | |
nappula1.addActionListener(this); | |
sisalto.add(nappula1); | |
JButton nappula2 = new JButton("2"); | |
nappula2.addActionListener(this); | |
sisalto.add(nappula2); | |
nappula2.setEnabled(false); | |
JButton nappula3 = new JButton("3"); | |
nappula3.addActionListener(this); | |
sisalto.add(nappula3); | |
nappula3.setEnabled(false); | |
JButton nappula4 = new JButton("4"); | |
nappula4.addActionListener(this); | |
sisalto.add(nappula4); | |
nappula4.setEnabled(false); | |
} | |
public void actionPerformed(ActionEvent ae) { | |
if (painalluksia==0) { | |
this.aloitusAika = System.currentTimeMillis(); | |
} | |
painalluksia++; | |
deaktivoiKaikki(); | |
if (painalluksia < 20) { | |
arvoUusiAktiivinen(); | |
} | |
else if (painalluksia==20) { | |
naytaPisteet(); | |
} | |
} | |
private void deaktivoiKaikki() { | |
for (int i=0; i<4; i++) { | |
kehys.getContentPane().getComponent(i).setEnabled(false); | |
} | |
} | |
private void arvoUusiAktiivinen() { | |
Random arpoja = new Random(); | |
int i = arpoja.nextInt(4); | |
kehys.getContentPane().getComponent(i).setEnabled(true); | |
} | |
private void naytaPisteet() { | |
long kulunutAika = System.currentTimeMillis() - aloitusAika; | |
JLabel pisteet = new JLabel("Pisteesi: " + (kulunutAika/20)); | |
this.kehys.getContentPane().add(pisteet); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment