-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
119 lines (94 loc) · 3.33 KB
/
main.java
File metadata and controls
119 lines (94 loc) · 3.33 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
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class main extends JFrame {
private JLabel[] reels;
private JButton spinButton;
private Timer timer;
private Random random;
int geld = 100;
public main() {
setTitle("Spilomat");
setSize(800, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initializeReels();
initializeSpinButton();
setLayout(new BorderLayout());
JPanel reelsPanel = new JPanel(new GridLayout(1, 3, 20, 0));
for (JLabel reel : reels) {
reelsPanel.add(reel);
}
add(reelsPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(spinButton);
add(buttonPanel, BorderLayout.SOUTH);
initializeTimer();
random = new Random();
setVisible(true);
}
private void initializeReels() {
reels = new JLabel[3];
for (int i = 0; i < reels.length; i++) {
reels[i] = new JLabel("0");
reels[i].setHorizontalAlignment(JLabel.CENTER);
reels[i].setFont(new Font("Arial", Font.BOLD, 50));
}
}
private void initializeSpinButton() {
spinButton = new JButton("Spin");
spinButton.setFont(new Font("Arial", Font.PLAIN, 20));
spinButton.addActionListener(e -> startAnimation());
}
private void initializeTimer() {
timer = new Timer(100, e -> updateReels());
}
private void startAnimation() {
spinButton.setEnabled(false);
timer.start();
Timer stopTimer = new Timer(2000, e -> stopAnimation());
stopTimer.setRepeats(false);
stopTimer.start();
}
private void stopAnimation() {
timer.stop();
spinButton.setEnabled(true);
displayResult();
}
private void updateReels() {
for (JLabel reel : reels) {
int randomNumber = random.nextInt(9) + 1;
reel.setText(String.valueOf(randomNumber));
}
}
private void displayResult() {
int[] numbers = new int[3];
for (int i = 0; i < reels.length; i++) {
numbers[i] = Integer.parseInt(reels[i].getText());
}
int gewinn = evaluateNumbers(numbers);
geld += gewinn;
String resultText = generateResultText(numbers, gewinn);
JOptionPane.showMessageDialog(this, resultText, "Ergebnis", JOptionPane.INFORMATION_MESSAGE);
}
private int evaluateNumbers(int[] numbers) {
if (numbers[0] == numbers[1] && numbers[1] == numbers[2]) {
return 500;
} else {
return -10;
}
}
private String generateResultText(int[] numbers, int gewinn) {
StringBuilder resultText = new StringBuilder("Spilomat Ergebnis:\n");
resultText.append("Gewählte Zahlen: ").append(numbers[0]).append(" - ").append(numbers[1]).append(" - ").append(numbers[2]).append("\n");
if (gewinn > 0) {
resultText.append("Gewonnen" + gewinn + "Geld \n");
} else if (gewinn < 0) {
resultText.append("Veloren" + gewinn + "Geld \n");
}
resultText.append(geld).append(" Geld");
return resultText.toString();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new main());
}
}