/
gui.java
266 lines (193 loc) · 6.15 KB
/
gui.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
import java.awt.Color;
import java.awt.Font;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
public class gui implements KeyListener{
JFrame myFrame;
boolean markPos[] = new boolean[10];
boolean markAns[]= new boolean[10];
JLabel JAntwort[];
JLabel JPosition[];
JLabel JPosNr[] ;
JLabel JAntNr[] ;
JLabel JFrage;
int aktAntwort = -1;
int aktPosition = -1;
logik myLogik;
void setLogik(logik refLogik){
myLogik = refLogik;
//init
myLogik.updateGui();
}
gui(){
this.initWindow();
}
void initWindow(){
try{
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
// handle exception
}
catch (ClassNotFoundException e) {
// handle exception
}
catch (InstantiationException e) {
// handle exception
}
catch (IllegalAccessException e) {
// handle exception
}
myFrame = new JFrame("Chrono");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.getContentPane().setLayout(null);
JFrage = new JLabel("FRAGE",JLabel.CENTER);
JFrage.setFont(new Font("Courier New",Font.BOLD, 18));
JFrage.setForeground(Color.BLACK);
JFrage.setBackground(Color.GRAY);
JFrage.setOpaque(true);
JFrage.setBounds(200,20,400,40);
myFrame.add(JFrage);
JAntwort = new JLabel[10];
JPosition = new JLabel[10];
JPosNr = new JLabel[11];
JAntNr = new JLabel[10];
JPosNr[8] = new JLabel("9" + " >",JLabel.CENTER);
//JPosNr[9] = new JLabel("10" + " >",JLabel.CENTER);
JPosNr[8].setFont(new Font("Courier New",Font.BOLD, 18));
JPosNr[8].setForeground(Color.BLACK);
//JPosNr[8].setBackground(Color.GRAY);
JPosNr[8].setOpaque(true);
JPosNr[8].setBounds(5,475,45,40);
myFrame.add(JPosNr[8]);
//JPosNr[9].setBounds(5,525,45,40);
//myFrame.add(JPosNr[9]);
for(int j = 0; j < 8; ++j ){
JAntwort[j] = new JLabel("Antwort " + (j + 1),JLabel.CENTER);
JAntwort[j].setFont(new Font("Courier New",Font.BOLD, 18));
JAntwort[j].setForeground(Color.BLACK);
JAntwort[j].setBounds(450,100 +(j * 50),300,40);
// Hintergrund
JAntwort[j].setBackground(Color.GRAY);
JAntwort[j].setOpaque(true);
// Rahmen
Border blackline = BorderFactory.createLineBorder(Color.black);
JAntwort[j].setBorder(blackline);
myFrame.add(JAntwort[j]);
JPosition[j] = new JLabel("Position " + (j + 1),JLabel.CENTER);
JPosition[j].setFont(new Font("Courier New",Font.BOLD, 18));
JPosition[j].setForeground(Color.BLACK);
JPosition[j].setBackground(Color.GRAY);
JPosition[j].setOpaque(true);
JPosition[j].setBounds(50,100 +(j * 50),300,40);
myFrame.add(JPosition[j]);
JPosNr[j] = new JLabel("" + (j + 1) + " >",JLabel.CENTER);
JPosNr[j].setFont(new Font("Courier New",Font.BOLD, 18));
JPosNr[j].setForeground(Color.BLACK);
//JPosNr[j].setBackground(Color.GRAY);
JPosNr[j].setOpaque(true);
JPosNr[j].setBounds(5,75 +(j * 50),45,40);
myFrame.add(JPosNr[j]);
JAntNr[j] = new JLabel("" + (char)(j+65) + ")");
JAntNr[j].setFont(new Font("Courier New",Font.BOLD, 18));
JAntNr[j].setForeground(Color.BLACK);
//JAntNr[j].setBackground(Color.GRAY);
JAntNr[j].setOpaque(true);
JAntNr[j].setBounds(425,100 +(j * 50),45,40);
myFrame.add(JAntNr[j]);
}
myFrame.addKeyListener(this);
myFrame.setSize(800,600);
myFrame.setLocation(200, 200);
myFrame.setVisible(true);
}
void checkStatus(){
// check status markPosition
for (int j = 0; j < 8; j++){
if (markAns[j] == true)
JAntwort[j].setBackground(Color.YELLOW);
else
JAntwort[j].setBackground(Color.GRAY);
}
// check status markAnswer
for (int j = 0; j < 9; j++){
if(markPos[j] == true)
JPosNr[j].setForeground(Color.YELLOW);
else
JPosNr[j].setForeground(Color.BLACK);
}
}
public void keyPressed(KeyEvent k) {
System.out.println(k.getKeyChar() + " " + k.getKeyCode());
if ((k.getKeyCode() > 64)&&(k.getKeyCode() < 73)){
System.out.println("char: " + (k.getKeyCode()-65));
if (markAns[k.getKeyCode()-65] == false){
markAns[k.getKeyCode()-65] = true;
aktAntwort = k.getKeyCode()-65;
}
else{
markAns[k.getKeyCode()-65] = false;
aktAntwort = -1;
}
}
else if ((k.getKeyCode() > 48)&&(k.getKeyCode() < 58)){
System.out.println("nr: " + (k.getKeyCode()-49));
if(markPos[k.getKeyCode()-49] == false){
markPos[k.getKeyCode()-49] = true;
aktPosition = k.getKeyCode()-49;
}
else {
markPos[k.getKeyCode()-49] = false;
aktPosition = -1;
}
}
else if ((k.getKeyCode() > 96)&&(k.getKeyCode() < 106)){
System.out.println("nr :" + (k.getKeyCode()-96));
if (markPos[k.getKeyCode()-97] == false){
markPos[k.getKeyCode()-97] = true;
aktPosition = k.getKeyCode()-97;
}
else{
markPos[k.getKeyCode()-97] = false;
aktPosition = -1;
}
}
else if (k.getKeyCode() == 96){
myLogik.spiel[1].solve();
myLogik.updateGui();
}
// quick test key = 't'
else if (k.getKeyCode() == 84){
System.out.println("Test Solve");
myLogik.action(0,0);
myLogik.action(1,1);
myLogik.action(2,2);
myLogik.action(3,3);
myLogik.action(4,4);
myLogik.action(5,5);
myLogik.action(6,6);
//myLogik.action(7,7);
this.checkStatus();
myLogik.updateGui();
}
this.checkStatus();
if (k.getKeyCode()== 32){
if ( (aktAntwort != -1) && (aktPosition != -1) ){
System.out.println("Action: " + aktAntwort+" " +aktPosition );
myLogik.action(aktAntwort,aktPosition);
markAns[aktAntwort] = false;
markPos[aktPosition] = false;
aktAntwort = -1;
aktPosition = -1;
}
this.checkStatus();
myLogik.updateGui();
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
}