-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSettings.java
More file actions
297 lines (265 loc) · 11.6 KB
/
Settings.java
File metadata and controls
297 lines (265 loc) · 11.6 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
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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.ArrayList;
import java.awt.Image.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import java.util.HashMap;
public class Settings {
//PROPERTIES
private SettingsPanel settingsPanel = new SettingsPanel();
//map that holds the settings for easy access
private static HashMap<String, String> settingsMap = new HashMap<>();
//{gray, red, green, brown}
private static Color[] boardColors = {
new Color(79, 76, 69), new Color(79, 8, 17), new Color(3, 150, 8), new Color(145, 84, 17)
};
private static BufferedReader reader;
private static PrintWriter writer;
//METHODS
//getter for port number
public static int getPortNumber() {
loadSettings();
return Integer.parseInt(settingsMap.get("port"));
}
//getter for color
public static Color getBoardColor() {
loadSettings();
return boardColors[Integer.parseInt(settingsMap.get("color"))];
}
//getter for dark mode
public static boolean isDark() {
loadSettings();
return Boolean.parseBoolean(settingsMap.get("dark"));
}
//getter for filter
public static boolean filterOn() {
loadSettings();
return Boolean.parseBoolean(settingsMap.get("filter"));
}
//read the settings from the file and load it onto the map
private static void loadSettings() {
reader = Utility.getReader("settings.txt");
for (int i = 0; i<4; i++) {
String[] strLine = Utility.readLine(reader).trim().split(" : ");
settingsMap.put(strLine[0], strLine[1]);
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//write any changed settings to the file
private static void saveSettings() {
writer = Utility.getWriter("settings.txt");
writer.println("port : " + settingsMap.get("port"));
writer.println("color : " + settingsMap.get("color"));
writer.println("dark : " + settingsMap.get("dark"));
writer.println("filter : " + settingsMap.get("filter"));
writer.close();
}
//reset settings by changing map and writing to the file
public static void setDefaultSettings() {
settingsMap.clear();
settingsMap.put("port", "3302");
settingsMap.put("color", "0");
settingsMap.put("dark", "true");
settingsMap.put("filter", "true");
saveSettings();
}
public JPanel getsettingsPanel() {
return settingsPanel;
}
//CONSTRUCTOR
public Settings() {
loadSettings();
//user settings to select buttons on the settings screen
settingsPanel.setPreferredSize(Utility.panelDimensions);
settingsPanel.portField.setText(settingsMap.get("port"));
settingsPanel.boardColorButtons[Integer.parseInt(settingsMap.get("color"))].setSelected(true);
settingsPanel.darkButtons[settingsMap.get("dark").equals("true") ? 0 : 1].setSelected(true);
settingsPanel.filterButtons[settingsMap.get("filter").equals("true") ? 0 : 1].setSelected(true);
}
//class that handles the ui for the settings screen
private class SettingsPanel extends JPanel implements ActionListener {
//PROPERTIES
private Timer settingsTimer = new Timer(1000 / 60, this);
private JButton backButton = new JButton("BACK");
private JButton saveButton = new JButton("SAVE");
private JButton defaultButton = new JButton("RESET TO DEFAULT");
JTextField portField = new JTextField();
JRadioButton[] boardColorButtons = new JRadioButton[4];
JRadioButton[] darkButtons = new JRadioButton[2];
JRadioButton[] filterButtons = new JRadioButton[2];
private String strfileName = "Assets/AboutPanel.png";
private JLabel portLabel = new JLabel("Change port number(Enter number above 1000): ");
private JLabel boardColorLabel = new JLabel("Change Board Color: ");
private JLabel darkLabel = new JLabel("Toggle Dark Mode:");
private JLabel filterLabel = new JLabel("Profanity Filter(for chat):");
private JLabel titleLabel = new JLabel("SETTINGS");
private ButtonGroup boardGroup = new ButtonGroup();
private ButtonGroup darkGroup = new ButtonGroup();
private ButtonGroup filterGroup = new ButtonGroup();
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == settingsTimer) {
repaint();
} else if (evt.getSource() == backButton) {
Utility.changePanel(new MainMenu().getMenuPanel());
} else if (evt.getSource() == saveButton) {
//success is based on whether the user entered a number or not
boolean blnSuccess = false;
String strPort = portField.getText();
int intPort = 0;
try {
intPort = Integer.parseInt(strPort);
blnSuccess = true;
} catch (NumberFormatException e) {
portField.setText("ENTER A NUMBER");
blnSuccess = false;
}
//save port to file if successful
if (blnSuccess) {
settingsMap.put("port", intPort + "");
saveSettings();
}
} else if (evt.getSource() == defaultButton) {
setDefaultSettings();
//relaunch panel to show dark mode change
Utility.changePanel(new Settings().settingsPanel);
} else {
//check if any of the board radio buttons were clicked
for (int i = 0; i<boardColorButtons.length; i++) {
if (evt.getSource() == boardColorButtons[i]) {
settingsMap.put("color", i + "");
System.out.println("COLOR");
saveSettings();
break;
}
}
//check if any of the dark mode radio buttons were clicked
for (int i = 0; i<2; i++) {
if (evt.getSource() == darkButtons[i]) {
settingsMap.put("dark", (i == 0) + "");
saveSettings();
Utility.changePanel(new Settings().settingsPanel);
break;
}
}
//check if any of the filter radio buttons were clicked
for (int i = 0; i<2; i++) {
if (evt.getSource() == darkButtons[i]) {
settingsMap.put("filter", (i == 0) + "");
saveSettings();
break;
}
}
}
}
//set properties for labels
private void initLabels() {
titleLabel.setSize(400, 100);
titleLabel.setLocation(440, 10);
Utility.setLabelStyle(titleLabel, 28);
titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
titleLabel.setVerticalAlignment(SwingConstants.CENTER);
portLabel.setSize(400, 50);
portLabel.setLocation(175, 100);
Utility.setLabelStyle(portLabel, 16);
portLabel.setVerticalAlignment(SwingConstants.CENTER);
portField.setSize(200, 25);
portField.setLocation(580, 113);
boardColorLabel.setSize(400, 50);
boardColorLabel.setLocation(175, 150);
Utility.setLabelStyle(boardColorLabel, 16);
boardColorLabel.setVerticalAlignment(SwingConstants.CENTER);
darkLabel.setSize(400, 50);
darkLabel.setLocation(175, 300);
Utility.setLabelStyle(darkLabel, 16);
darkLabel.setVerticalAlignment(SwingConstants.CENTER);
filterLabel.setSize(400, 50);
filterLabel.setLocation(175, 400);
Utility.setLabelStyle(filterLabel, 16);
filterLabel.setVerticalAlignment(SwingConstants.CENTER);
add(titleLabel);
add(portLabel);
add(portField);
add(boardColorLabel);
add(filterLabel);
add(darkLabel);
}
//set properties for radio buttons
private void initRadioButtons() {
String[] strColorOptions = {
"Gray", "Red", "Green", "Brown"
};
boolean isDark = Settings.isDark();
for (int i = 0; i<4; i++) {
boardColorButtons[i] = new JRadioButton(strColorOptions[i]);
boardColorButtons[i].setSize(100, 25);
boardColorButtons[i].setLocation(270 + (i * 150), 210);
boardColorButtons[i].setForeground(isDark ? Color.WHITE : Color.BLACK);
boardColorButtons[i].setBackground(isDark ? Color.BLACK : Color.WHITE);
boardColorButtons[i].setFont(Utility.getFont().deriveFont(Font.PLAIN, 16));
boardColorButtons[i].addActionListener(this);
boardGroup.add(boardColorButtons[i]);
add(boardColorButtons[i]);
}
String[] strOptions = {
"YES", "NO"
};
for (int i = 0; i<2; i++) {
darkButtons[i] = new JRadioButton(strOptions[i]);
darkButtons[i].setSize(100, 25);
darkButtons[i].setLocation(320 + (i * 150), 315);
darkButtons[i].setForeground(isDark ? Color.WHITE : Color.BLACK);
darkButtons[i].setBackground(isDark ? Color.BLACK : Color.WHITE);
darkButtons[i].setFont(Utility.getFont().deriveFont(Font.PLAIN, 16));
darkButtons[i].addActionListener(this);
darkGroup.add(darkButtons[i]);
add(darkButtons[i]);
}
for (int i = 0; i<2; i++) {
filterButtons[i] = new JRadioButton(strOptions[i]);
filterButtons[i].setSize(100, 25);
filterButtons[i].setLocation(370 + (i * 150), 415);
filterButtons[i].setForeground(isDark ? Color.WHITE : Color.BLACK);
filterButtons[i].setBackground(isDark ? Color.BLACK : Color.WHITE);
filterButtons[i].setFont(Utility.getFont().deriveFont(Font.PLAIN, 16));
filterButtons[i].addActionListener(this);
filterGroup.add(filterButtons[i]);
add(filterButtons[i]);
}
}
//set properties for JButtons
private void initButtons() {
backButton.setSize(110, 45);
backButton.setLocation(30, 30);
backButton.addActionListener(this);
Utility.setButtonStyle(backButton, 12);
defaultButton.setSize(200, 75);
defaultButton.setLocation(540, 600);
defaultButton.addActionListener(this);
Utility.setButtonStyle(defaultButton, 14);
saveButton.setSize(200, 25);
saveButton.setLocation(780, 113);
saveButton.addActionListener(this);
Utility.setButtonStyle(saveButton, 12);
add(backButton);
add(defaultButton);
add(saveButton);
}
//CONSTRUCTOR
public SettingsPanel() {
super(null); //transfers constructor from JPanel
setBackground(Settings.isDark() ? Color.BLACK : Color.WHITE);
initLabels();
initButtons();
initRadioButtons();
settingsTimer.start();
}
}
}