-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGame.java
120 lines (101 loc) · 3.75 KB
/
TestGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class TestGame extends AbstractMiniGame {
// Definierung von Titel und Schwierigkeitsgrad
private static String titel = "Testspiel";
// interner Krams für das Beispiel
private Random rand = new Random();
private int ergebnisZahl;
// JFrame-Krams
private JLabel ueberschrift = new JLabel();
private JTextField erstezahl = new JTextField();
private JTextField zahlzwei = new JTextField();
private JTextField ergebnis = new JTextField();
private JLabel gleichzeichen = new JLabel();
private JLabel operator = new JLabel();
private JButton abschickenButton = new JButton();
public TestGame(int schwierigkeit) {
// das Schwierigkeitslevel der abtrakten Mutterklasse
// initialisieren
// Frame-Initialisierung
super(titel);
this.schwierigkeit = schwierigkeit;
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 214;
int frameHeight = 156;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// Anfang Komponenten
ueberschrift.setBounds(16, 16, 155, 19);
ueberschrift.setText("Erfülle die Rechenaufgabe!");
ueberschrift.setFont(new Font("Dialog", Font.BOLD, 10));
cp.add(ueberschrift);
erstezahl.setBounds(16, 56, 25, 25);
erstezahl.setText("");
erstezahl.setEditable(false);
cp.add(erstezahl);
zahlzwei.setBounds(72, 56, 25, 25);
zahlzwei.setText("");
zahlzwei.setEditable(false);
cp.add(zahlzwei);
ergebnis.setBounds(128, 56, 65, 25);
ergebnis.setText("");
cp.add(ergebnis);
gleichzeichen.setBounds(104, 56, 19, 28);
gleichzeichen.setText("=");
gleichzeichen.setFont(new Font("Dialog", Font.BOLD, 20));
cp.add(gleichzeichen);
operator.setBounds(48, 56, 19, 28);
operator.setText("+");
operator.setFont(new Font("Dialog", Font.BOLD, 20));
cp.add(operator);
abschickenButton.setBounds(104, 88, 89, 25);
abschickenButton.setText("OK!");
abschickenButton.setMargin(new Insets(2, 2, 2, 2));
abschickenButton.addActionListener(new ActionListener() {
// hier ist der interessante Teil
Timer tm = new Timer(5000, this);
// diese Zeile erzeugt einen neuen Timer, der nicht von
// Threads abhängt und auch in ActionListenern benützt werden kann
public void actionPerformed(ActionEvent evt) {
tm.start(); // hier wird der Timer gestartet
abschickenButton_ActionPerformed(evt);
}
});
cp.add(abschickenButton);
// Ende Komponenten
setVisible(true);
}
public static void main(String[] args) {
AbstractMiniGame thisgame = new TestGame(0);
thisgame.initialisieren();
}
// Hier wird die Methode initialisieren implementiert
// und die Textfelder mit zufälligen Zahlen gefüllt.
// Man könnte auch den Operator ändern, aber naja.. ist nur ein Test.
public void initialisieren() {
int zahl1 = rand.nextInt(25);
int zahl2 = rand.nextInt(25);
ergebnisZahl = zahl1 + zahl2;
erstezahl.setText(""+zahl1);
zahlzwei.setText(""+zahl2);
}
public void abschickenButton_ActionPerformed(ActionEvent evt) {
// wenn dat Knöppsche gedrückt wurde, den Inhalt von dem
// Ergebnisfeld holen und überprüfen, obs stimmt
int spielerEingabe = Integer.parseInt(ergebnis.getText());
if(spielerEingabe == ergebnisZahl) {
System.out.println("richtige Antwort!");
ueberschrift.setText("Hurra!");
}
}
}