Created
June 12, 2012 08:32
-
-
Save Cherubi/2916200 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
import java.awt.event.*; | |
public class AjastimenKuuntelija implements ActionListener { | |
private Nopeustesti ohjelma; | |
public AjastimenKuuntelija(Nopeustesti ohjelma) { | |
this.ohjelma = ohjelma; | |
} | |
public void actionPerformed(ActionEvent tapahtuma) { | |
ohjelma.seuraavaVari(); | |
} | |
} |
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
import javax.swing.*; | |
public class Main { | |
public static void main(String[] args) { | |
Nopeustesti ohjelma = new Nopeustesti(); | |
SwingUtilities.invokeLater(ohjelma); | |
} | |
} |
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
import java.awt.event.*; | |
import javax.swing.*; | |
public class NappaimistonKuuntelija implements KeyListener { | |
private Nopeustesti ohjelma; | |
public NappaimistonKuuntelija(Nopeustesti ohjelma) { | |
this.ohjelma = ohjelma; | |
} | |
public void keyTyped(KeyEvent e) { | |
} | |
public void keyPressed(KeyEvent e) { | |
int koodi = e.getKeyCode(); | |
if (koodi == KeyEvent.VK_N) { | |
ohjelma.uusiPeli(); | |
} | |
} | |
public void keyReleased(KeyEvent e) { | |
} | |
} |
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
import java.awt.event.*; | |
import javax.swing.*; | |
public class NappulanKuuntelija implements ActionListener { | |
private Nopeustesti ohjelma; | |
public NappulanKuuntelija(Nopeustesti ohjelma) { | |
this.ohjelma = ohjelma; | |
} | |
public void actionPerformed(ActionEvent e) { | |
JButton nappula = ((JButton)e.getSource()); | |
ohjelma.valitseVari(Integer.parseInt(nappula.getName())); | |
} | |
} |
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
import java.awt.*; | |
import javax.swing.*; | |
public class Nopeustesti implements Runnable { | |
private JFrame ikkuna; | |
private Timer ajastin; | |
private Container nappulat; | |
private JLabel pistenaytto; | |
private int oikeaVari; | |
private int valittuVari; | |
private int pisteet; | |
public void run() { | |
ikkuna = new JFrame("Nopeustesti"); | |
ikkuna.setPreferredSize(new Dimension(300, 150)); | |
luoKomponentit(); | |
oikeaVari = -1; | |
naytaTilanne(); | |
ikkuna.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
ikkuna.pack(); | |
ikkuna.setVisible(true); | |
ikkuna.addKeyListener(new NappaimistonKuuntelija(this)); | |
} | |
private void luoKomponentit() { | |
Container pohja = ikkuna.getContentPane(); | |
pohja.setLayout(new BoxLayout(pohja, BoxLayout.Y_AXIS)); | |
pohja.setBackground(Color.BLACK); | |
nappulat = new Container(); | |
nappulat.setLayout(new FlowLayout()); | |
for (int i = 0; i < 4; i++) { | |
JButton nappula = luoNappula("" + i); | |
nappulat.add(nappula); | |
} | |
pohja.add(nappulat); | |
pistenaytto = new JLabel("Pisteet: 0"); | |
pistenaytto.setAlignmentX(Component.CENTER_ALIGNMENT); | |
pistenaytto.setBackground(Color.BLACK); | |
pistenaytto.setForeground(Color.WHITE); | |
pohja.add(pistenaytto); | |
JLabel ohje = new JLabel("F2 aloittaa uuden pelin"); | |
ohje.setAlignmentX(Component.CENTER_ALIGNMENT); | |
ohje.setBackground(Color.BLACK); | |
ohje.setForeground(Color.WHITE); | |
pohja.add(ohje); | |
} | |
private JButton luoNappula(String nimi) { | |
JButton nappula = new JButton(" "); | |
nappula.setName(nimi); | |
nappula.addActionListener(new NappulanKuuntelija(this)); | |
nappula.setAlignmentX(Component.CENTER_ALIGNMENT); | |
nappula.setBackground(Color.BLACK); | |
nappula.setOpaque(true); | |
nappula.setBorderPainted(false); | |
nappula.setFocusPainted(false); | |
nappula.setFocusable(false); | |
return nappula; | |
} | |
private void naytaTilanne() { | |
Color[] varit = {Color.BLUE, Color.RED, Color.GREEN, Color.YELLOW}; | |
for (int i = 0; i < 4; i++) { | |
JButton nappula = (JButton)nappulat.getComponent(i); | |
if (i == oikeaVari) { | |
nappula.setBackground(varit[i]); | |
} else { | |
nappula.setBackground(Color.DARK_GRAY); | |
} | |
} | |
pistenaytto.setText("Pisteet: " + pisteet); | |
} | |
private void arvoVari() { | |
do { | |
oikeaVari = (int)(Math.random() * 4); | |
} while (oikeaVari == valittuVari); | |
valittuVari = -1; | |
ajastin = new Timer(1000 - 5 * pisteet, new AjastimenKuuntelija(this)); | |
ajastin.start(); | |
naytaTilanne(); | |
} | |
public void uusiPeli() { | |
pisteet = 0; | |
arvoVari(); | |
} | |
public void valitseVari(int vari) { | |
if (valittuVari == -1) { | |
valittuVari = vari; | |
} | |
} | |
public void seuraavaVari() { | |
ajastin.stop(); | |
if (oikeaVari == valittuVari) { | |
pisteet++; | |
arvoVari(); | |
} else { | |
String viesti = "Peli päättyi!\nPisteet: " + pisteet; | |
JOptionPane.showMessageDialog(ikkuna, viesti); | |
oikeaVari = -1; | |
naytaTilanne(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment