-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoller.java
366 lines (325 loc) · 10.6 KB
/
Roller.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Arrays;
import java.util.List;
public class Roller extends AbstractMiniGame {
// Anfang Attribute
private static String title = "Roller";
private JLabel jLabel1 = new JLabel();
private JButton btnStop = new JButton();
private JButton btnStart = new JButton();
private JPanel jPanel1 = new JPanel(null, true);
private JTextField z1_1 = new JTextField();
private JTextField z1_2 = new JTextField();
private JTextField z1_3 = new JTextField();
private JPanel jPanel2 = new JPanel(null, true);
private JPanel jPanel3 = new JPanel(null, true);
private JTextField z3_1 = new JTextField();
private JTextField z3_3 = new JTextField();
private JTextField z3_2 = new JTextField();
private JPanel jPanel4 = new JPanel(null, true);
private JTextField z4_1 = new JTextField();
private JTextField z4_3 = new JTextField();
private JTextField z4_2 = new JTextField();
private JTextField z2_1 = new JTextField();
private JTextField z2_3 = new JTextField();
private JTextField z2_2 = new JTextField();
private JTextField txtri4 = new JTextField();
private JTextField txtri3 = new JTextField();
private JTextField txtri2 = new JTextField();
private JTextField txtri1 = new JTextField();
private int sto = 0;
private int durchgaenge1 = 0;
private int durchgaenge2 = 0;
private int durchgaenge3 = 0;
private int durchgaenge4 = 0;
private Random rand = new Random();
private List<String> text = new ArrayList<String>(40);
private List<Integer> erg = new ArrayList<Integer>(4);
// Ende Attribute
private ActionListener t1 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
z1_1.setText(text.get(0*10+2+durchgaenge1));
z1_2.setText(text.get(0*10+1+durchgaenge1));
z1_3.setText(text.get(0*10+0+durchgaenge1));
durchgaenge1++;
if (durchgaenge1 == 8) {
durchgaenge1 = 0;
}
}
};
private Timer tm1 = new Timer(700 - 35*schwierigkeit, t1);
private ActionListener t2 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
z2_1.setText(text.get(1*10+2+durchgaenge2));
z2_2.setText(text.get(1*10+1+durchgaenge2));
z2_3.setText(text.get(1*10+0+durchgaenge2));
durchgaenge2++;
if (durchgaenge2 == 8) {
durchgaenge2 = 0;
}
}
};
private Timer tm2 = new Timer(700 - 35*schwierigkeit, t2);
private ActionListener t3 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
z3_1.setText(text.get(2*10+2+durchgaenge3));
z3_2.setText(text.get(2*10+1+durchgaenge3));
z3_3.setText(text.get(2*10+0+durchgaenge3));
durchgaenge3++;
if (durchgaenge3 == 8) {
durchgaenge3 = 0;
}
}
};
private Timer tm3 = new Timer(700 - 35*schwierigkeit, t3);
private ActionListener t4 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
z4_1.setText(text.get(3*10+2+durchgaenge4));
z4_2.setText(text.get(3*10+1+durchgaenge4));
z4_3.setText(text.get(3*10+0+durchgaenge4));
durchgaenge4++;
if (durchgaenge4 == 8) {
durchgaenge4 = 0;
}
}
};
private Timer tm4 = new Timer(700 - 35*schwierigkeit, t4);
public Roller(int schwierigkeit) {
// Frame-Initialisierung
super(title);
this.schwierigkeit = schwierigkeit;
int frameWidth = 488;
int frameHeight = 378;
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
jLabel1.setBounds(24, 144, 68, 65);
jLabel1.setText(" >");
jLabel1.setFont(new Font("Fixedsys", Font.BOLD, 20));
jLabel1.setForeground(Color.GREEN);
cp.add(jLabel1);
btnStop.setBounds(352, 192, 81, 81);
btnStop.setText("Stop");
btnStop.setMargin(new Insets(2, 2, 2, 2));
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnStop_ActionPerformed(evt);
}
});
btnStop.setFont(new Font("Fixedsys", Font.BOLD, 12));
btnStop.setForeground(Color.GREEN);
btnStop.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnStop.setBackground(Color.BLACK);
btnStop.setBorder(new javax.swing.border.LineBorder(Color.GREEN, 2));
cp.add(btnStop);
txtri1.setBounds(104, 48, 33, 49);
txtri1.setEditable(false);
txtri1.setBackground(Color.BLACK);
txtri1.setFont(new Font("Fixedsys", Font.PLAIN, 12));
txtri1.setForeground(Color.GREEN);
cp.add(txtri1);
txtri2.setBounds(168, 48, 33, 49);
txtri2.setEditable(false);
txtri2.setBackground(Color.BLACK);
txtri2.setFont(new Font("Fixedsys", Font.PLAIN, 12));
txtri2.setForeground(Color.GREEN);
cp.add(txtri2);
txtri3.setBounds(232, 48, 33, 49);
txtri3.setEditable(false);
txtri3.setBackground(Color.BLACK);
txtri3.setFont(new Font("Fixedsys", Font.PLAIN, 12));
txtri3.setForeground(Color.GREEN);
cp.add(txtri3);
txtri4.setBounds(296, 48, 33, 49);
txtri4.setEditable(false);
txtri4.setBackground(Color.BLACK);
txtri4.setFont(new Font("Fixedsys", Font.PLAIN, 12));
txtri4.setForeground(Color.GREEN);
cp.add(txtri4);
btnStart.setBounds(352, 88, 81, 81);
btnStart.setText("Start");
btnStart.setMargin(new Insets(2, 2, 2, 2));
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnStart_ActionPerformed(evt);
}
});
btnStart.setFont(new Font("Fixedsys", Font.BOLD, 12));
btnStart.setForeground(Color.GREEN);
btnStart.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnStart.setBackground(Color.BLACK);
btnStart.setBorder(new javax.swing.border.LineBorder(Color.GREEN, 2));
cp.add(btnStart);
jPanel1.setBounds(96, 104, 49, 153);
jPanel1.setOpaque(false);
cp.add(jPanel1);
jPanel2.setBounds(160, 104, 49, 153);
jPanel2.setOpaque(false);
cp.add(jPanel2);
jPanel3.setBounds(224, 104, 49, 153);
jPanel3.setOpaque(false);
cp.add(jPanel3);
jPanel4.setBounds(288, 104, 49, 153);
jPanel4.setOpaque(false);
cp.add(jPanel4);
z1_1.setBounds(0, 16, 49, 33);
z1_1.setBackground(Color.BLACK);
z1_1.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z1_1.setForeground(Color.GREEN);
jPanel1.add(z1_1);
z1_2.setBounds(0, 56, 49, 33);
z1_2.setBackground(Color.BLACK);
z1_2.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z1_2.setForeground(Color.GREEN);
jPanel1.add(z1_2);
z1_3.setBounds(0, 96, 49, 33);
z1_3.setBackground(Color.BLACK);
z1_3.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z1_3.setForeground(Color.GREEN);
jPanel1.add(z1_3);
z2_1.setBounds(160, 120, 49, 33);
z2_1.setBackground(Color.BLACK);
z2_1.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z2_1.setForeground(Color.GREEN);
cp.add(z2_1);
z2_3.setBounds(160, 200, 49, 33);
z2_3.setBackground(Color.BLACK);
z2_3.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z2_3.setForeground(Color.GREEN);
cp.add(z2_3);
z2_2.setBounds(160, 160, 49, 33);
z2_2.setBackground(Color.BLACK);
z2_2.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z2_2.setForeground(Color.GREEN);
cp.add(z2_2);
z3_1.setBounds(0, 16, 49, 33);
z3_1.setBackground(Color.BLACK);
z3_1.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z3_1.setForeground(Color.GREEN);
jPanel3.add(z3_1);
z3_3.setBounds(0, 96, 49, 33);
z3_3.setBackground(Color.BLACK);
z3_3.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z3_3.setForeground(Color.GREEN);
jPanel3.add(z3_3);
z3_2.setBounds(0, 56, 49, 33);
z3_2.setBackground(Color.BLACK);
z3_2.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z3_2.setForeground(Color.GREEN);
jPanel3.add(z3_2);
z4_1.setBounds(0, 16, 49, 33);
z4_1.setBackground(Color.BLACK);
z4_1.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z4_1.setForeground(Color.GREEN);
jPanel4.add(z4_1);
z4_3.setBounds(0, 96, 49, 33);
z4_3.setBackground(Color.BLACK);
z4_3.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z4_3.setForeground(Color.GREEN);
jPanel4.add(z4_3);
z4_2.setBounds(0, 56, 49, 33);
z4_2.setBackground(Color.BLACK);
z4_2.setFont(new Font("Fixedsys", Font.PLAIN, 12));
z4_2.setForeground(Color.GREEN);
jPanel4.add(z4_2);
cp.setBackground(Color.BLACK);
}
public void btnStop_ActionPerformed(ActionEvent evt) {
sto++;
if (sto == 1) {
tm1.stop();
}
else if (sto == 2) {
tm2.stop();
}
else if (sto == 3) {
tm3.stop();
}
else if (sto == 4) {
tm4.stop();
btnStop.setEnabled(false);
if(erg.get(0) == Double.parseDouble(z1_2.getText()) &&
erg.get(1) == Double.parseDouble(z2_2.getText()) &&
erg.get(2) == Double.parseDouble(z3_2.getText()) &&
erg.get(3) == Double.parseDouble(z4_2.getText())) {
guiWinScreen.setVisible(true);
this.erfolg = true;
} else {
guiLoseScreen.setVisible(true);
this.erfolg = false;
}
this.versuche = 1;
this.fertig = true;
}
}
public void btnStart_ActionPerformed(ActionEvent evt) {
// wenn das Spiel gestartet wurde, soll man es nur noch stoppen können
btnStart.setEnabled(false);
tm1.setInitialDelay(0);
tm1.start();
tm2.setInitialDelay(0);
tm2.start();
tm3.setInitialDelay(0);
tm3.start();
tm4.setInitialDelay(0);
tm4.start();
}
public static void main(String[] args) {
AbstractMiniGame roller = new Roller(0);
roller.initialisieren();
roller.setVisible(true);
}
public void initialisieren() {
// die Initialisierungsfunktion der Mutterklasse aufrufen
super.initialisieren();
// den Startbutton wieder klickbar machen
btnStart.setEnabled(true);
// die richtigen Ergebnisse für den Roller initialisieren
erg = Arrays.asList(rand.nextInt(30)+20, rand.nextInt(30)+20,
rand.nextInt(30)+20, rand.nextInt(30)+20);
// die Textfelder, die das richtige Ergebnis anzeigen, füllen
txtri1.setText(" "+erg.get(0));
txtri2.setText(" "+erg.get(1));
txtri3.setText(" "+erg.get(2));
txtri4.setText(" "+erg.get(3));
// das sind Zahlen, die die einzelnen Zahlen, die durchlaufen, varriieren
List<Integer> add = Arrays.asList( 1, 0, -2, 4, -3, -4, 3, -1, 1, 0,
1, 4, -2, 0, -3, -4, 4, -1, 1, 4,
1, 3, -2, 4, -3, -4, 0, -1, 1, 3,
1, -2, 0, 4, -3, -4, 3, -1, 1, -2);
text = new ArrayList<String>();
for(int i = 0; i < erg.size(); i++) {
for(int j = 0; j < 10; j++) {
text.add(" "+(erg.get(i) + add.get(i*10+j)));
}
}
z1_1.setText("");
z1_2.setText("");
z1_3.setText("");
z2_1.setText("");
z2_2.setText("");
z2_3.setText("");
z3_1.setText("");
z3_2.setText("");
z3_3.setText("");
z4_1.setText("");
z4_2.setText("");
z4_3.setText("");
durchgaenge1 = 0;
durchgaenge2 = 0;
durchgaenge3 = 0;
durchgaenge4 = 0;
// zurück auf Start :)
sto = 0;
}
}