-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.java
More file actions
190 lines (182 loc) · 6.7 KB
/
Copy pathSettings.java
File metadata and controls
190 lines (182 loc) · 6.7 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
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
import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Settings extends JPanel {
GameFrame gf;
JLabel title = Label.title("Settings");
ButtonGroup radioGroup1 = new ButtonGroup();
ButtonGroup radioGroup2 = new ButtonGroup();
JPanel radios = new JPanel();
JPanel buttons = new JPanel();
BoxLayout bl = new BoxLayout(radios, BoxLayout.Y_AXIS);
JRadioButton theme1 = new JRadioButton("Lord Of The Rings", true);
JRadioButton theme2 = new JRadioButton("Dark Souls");
JRadioButton theme3 = new JRadioButton("Elden Ring");
JRadioButton level1 = new JRadioButton("Easy", true);
JRadioButton level2 = new JRadioButton("Normal");
JRadioButton level3 = new JRadioButton("Hard");
JLabel board = new JLabel("3x4 board");
JLabel timer = new JLabel("30 seconds timer");
JButton apply = new JButton("Apply Changes");
JButton cancel = new JButton("Cancel");
ImageIcon soundIcon = resizeIcon("icons\\volume.png", 50, 50);
ImageIcon noSoundIcon = resizeIcon("icons\\mute.png", 50, 50);
JLabel sound = new JLabel(soundIcon);
File musicFile;
Clip clip;
boolean musicOn = true;
int nbcards = 3;
String[] modes = {"Easy", "Normal", "Hard"};
String theme = "LOTR";
ButtonModel selectedDiff;
ButtonModel selectedTheme;
public Settings(GameFrame gf) {
this.gf = gf;
gf.game = new Game(gf, modes[nbcards-3], theme);
setLayout(new BorderLayout(50, 50));
radios.setLayout(bl);
selectedDiff = level1.getModel();
selectedTheme = theme1.getModel();
playMusic();
ActionListener diffListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src == level1)
nbcards = 3;
else if(src == level2)
nbcards = 4;
else
nbcards = 5;
timer.setText((nbcards+1)*10+" seconds timer");
board.setText(nbcards+"x4 board");
Sounds.playRadioClickSound();
}
};
ActionListener themeListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
Sounds.playRadioClickSound();
if(src == theme3)
theme = "elden ring";
else if(src == theme2)
theme = "dark souls";
else
theme = "LOTR";
}
};
ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
Sounds.playClickSound();
if(src == apply) {
applyChanges();
} else {
radioGroup1.setSelected(selectedDiff, true);
radioGroup2.setSelected(selectedTheme, true);
}
gf.cl.show(gf.getContentPane(),"main menu");
}
};
radioGroup1.add(level1);
radioGroup1.add(level2);
radioGroup1.add(level3);
radioGroup2.add(theme1);
radioGroup2.add(theme2);
radioGroup2.add(theme3);
level1.addActionListener(diffListener);
level2.addActionListener(diffListener);
level3.addActionListener(diffListener);
theme1.addActionListener(themeListener);
theme2.addActionListener(themeListener);
theme3.addActionListener(themeListener);
apply.addActionListener(buttonListener);
cancel.addActionListener(buttonListener);
sound.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if(musicOn) {
sound.setIcon(noSoundIcon);
clip.stop();
} else {
sound.setIcon(soundIcon);
clip.start();
}
musicOn = !musicOn;
Sounds.playRadioClickSound();
}
});
radios.setBorder(BorderFactory.createEmptyBorder(50,300,50,100));
radios.add(Label.settings("Mode :"));
radios.add(level1);
radios.add(level2);
radios.add(level3);
radios.add(Box.createVerticalStrut(50));
radios.add(board);
radios.add(timer);
radios.add(Box.createVerticalStrut(50));
radios.add(Label.settings("Theme :"));
radios.add(theme1);
radios.add(theme2);
radios.add(theme3);
radios.add(Box.createVerticalStrut(50));
radios.add(sound);
buttons.add(apply);
buttons.add(cancel);
add(title, "North");
add(radios, "Center");
add(buttons, "South");
}
public void applyChanges() {
ButtonModel oldDiff = selectedDiff;
ButtonModel oldTheme = selectedTheme;
selectedDiff = radioGroup1.getSelection();
selectedTheme = radioGroup2.getSelection();
if (oldTheme != selectedTheme || oldDiff != selectedDiff) {
if (oldTheme != selectedTheme)
playMusic();
gf.getContentPane().remove(gf.game);
gf.game = new Game(gf, modes[nbcards - 3], theme);
gf.getContentPane().add("game", gf.game);
revalidate();
repaint();
}
}
private static ImageIcon resizeIcon(String filePath, int width, int height) {
try {
BufferedImage img = ImageIO.read(new File(filePath));
Image resizedImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
return new ImageIcon(resizedImg);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
void playMusic() {
if (clip != null && clip.isOpen()) {
clip.stop();
clip.close();
}
musicFile = new File("soundtracks\\"+theme+".wav");
try {
AudioInputStream audioIn = AudioSystem.getAudioInputStream(musicFile);
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}