-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHammer.java
More file actions
161 lines (133 loc) · 4.3 KB
/
Hammer.java
File metadata and controls
161 lines (133 loc) · 4.3 KB
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
public class Hammer extends AbstractMiniGame {
// Anfang Attribute
private static String title = "Hammer";
private JButton btnStart = new JButton();
private List<JButton> buttons = new ArrayList<JButton>();
private int kill = 0;
private int win = 0;
private Timer tm1;
private ActionListener t1 = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int rand = (int) ((Math.random() * 6) + 0);
kill++;
if (kill == 6 - schwierigkeit) { // hier Schwierigleit verändern
guiLoseScreen.setVisible(true);
tm1.stop();
erfolg = false;
versuche = 1;
fertig = true;
} else if (win == 5 + schwierigkeit) { // hier Schwierigleit
// verändern
guiWinScreen.setVisible(true);
tm1.stop();
erfolg = true;
versuche = 1;
fertig = true;
} else {
buttons.get(rand).setVisible(true);
}
}
};
public Hammer(int schwierigkeit) {
super(title);
this.schwierigkeit = schwierigkeit;
tm1 = new Timer(750+(160-10*schwierigkeit), t1);
int frameWidth = 404;
int frameHeight = 386;
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);
// Sieben Buttons werden initialisiert
for (int i = 0; i < 7; i++) {
buttons.add(new JButton());
buttons.get(i).setText("0");
buttons.get(i).setMargin(new Insets(2, 2, 2, 2));
buttons.get(i).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
buttons_ActionPerformed(evt);
}
});
// um die Buttons beim generischen ActionListener zu unterscheiden
buttons.get(i).setActionCommand("" + i);
buttons.get(i).setBackground(Color.BLACK);
buttons.get(i).setBorder(
new javax.swing.border.LineBorder(Color.GREEN, 3));
buttons.get(i).setFont(new Font("Fixedsys", Font.PLAIN, 12));
buttons.get(i).setForeground(Color.GREEN);
buttons.get(i).setCursor(new Cursor(Cursor.HAND_CURSOR));
buttons.get(i).setVisible(false);
cp.add(buttons.get(i));
}
buttons.get(0).setBounds(112, 120, 129, 41);
buttons.get(1).setBounds(144, 24, 57, 65);
buttons.get(2).setBounds(40, 136, 57, 97);
buttons.get(3).setBounds(216, 64, 129, 49);
buttons.get(4).setBounds(272, 128, 57, 97);
buttons.get(5).setBounds(272, 128, 57, 97);
buttons.get(6).setBounds(168, 184, 81, 49);
btnStart.setBounds(48, 256, 289, 73);
btnStart.setText("Start");
btnStart.setMargin(new Insets(2, 2, 2, 2));
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
btnStart_ActionPerformed(evt);
}
});
btnStart.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnStart.setBackground(Color.BLACK);
btnStart.setBorder(new javax.swing.border.LineBorder(Color.RED, 2));
btnStart.setFont(new Font("Fixedsys", Font.PLAIN, 12));
btnStart.setForeground(Color.GREEN);
cp.add(btnStart);
cp.setBackground(Color.BLACK);
// Ende Komponenten
}
public static void main(String[] args) {
AbstractMiniGame reflKey = new Hammer(10);
reflKey.initialisieren();
reflKey.setVisible(true);
} // end of main
private void buttons_ActionPerformed(ActionEvent evt) {
int btnNummer = Integer.parseInt(evt.getActionCommand());
int hTmp = Integer.parseInt(buttons.get(btnNummer).getText());
hTmp++;
buttons.get(btnNummer).setText("" + hTmp);
if (hTmp == 5) {
buttons.get(btnNummer).setVisible(false);
buttons.get(btnNummer).setText("0");
kill--;
win++;
}
}
private void btnStart_ActionPerformed(ActionEvent evt) {
// wenn das Spiel gestartet wurde, soll es nicht nochmal gestartet
// werden
btnStart.setEnabled(false);
tm1.setInitialDelay(0);
tm1.start();
}
public void initialisieren() {
// die Initialisierungsfunktion der Mutterklasse aufrufen
super.initialisieren();
// den Startbutton wieder klickbar machen
btnStart.setEnabled(true);
// die Buttons und die Zähler zurücksetzen
for (int i = 0; i < buttons.size(); i++) {
buttons.get(i).setText("0");
buttons.get(i).setVisible(false);
}
// zurück auf Start :)
kill = 0;
win = 0;
}
}