-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGameModeButton.java
More file actions
235 lines (196 loc) · 6.14 KB
/
GameModeButton.java
File metadata and controls
235 lines (196 loc) · 6.14 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
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
public class GameModeButton {
private GameMode gameMode;
private String name;
private JPanel table;
private JFrame frame;
private JButton gameButton;
private JTextPane highScoreBox;
private JTextPane totalGamesBox;
private JCheckBox checkBox;
private JButton gameinformationButton;
private int x_pos;
private int y_pos;
private boolean favorite;
private StartMenu menu;
public GameModeButton(GameMode myGM, JPanel myTable, JFrame myFrame, StartMenu myMenu) {
gameMode = myGM;
name = myGM.getName();
table = myTable;
menu = myMenu;
frame = myFrame;
gameButton = new JButton(name);
gameButton.setName(name);
gameButton.addActionListener(new ChooseGameListener());
highScoreBox = new JTextPane();
highScoreBox.setEditable(false);
highScoreBox.setBackground(Color.GREEN);
highScoreBox.setFont(new Font("Arial", Font.PLAIN, 12));
totalGamesBox = new JTextPane();
totalGamesBox.setEditable(false);
totalGamesBox.setBackground(Color.GREEN);
totalGamesBox.setFont(new Font("Arial", Font.PLAIN, 12));
refreshScores();
checkBox= new JCheckBox();
checkBox.addActionListener(new CheckFavoritesListener());
checkBox.setName(name);
checkBox.setBackground(Color.GREEN);
checkBox.setSelected(favorite);
gameinformationButton = new JButton("?");
gameinformationButton.setName(name);
gameinformationButton.addActionListener(new ShowDescriptionListener());
gameinformationButton.setMargin(new Insets(1,1,1,1));
gameinformationButton.setFont(new Font("Arial", Font.BOLD, 15));
}
public void setPosition(int my_x_pos, int my_y_pos) {
x_pos = my_x_pos;
y_pos = my_y_pos;
gameButton.setBounds(x_pos, y_pos, 120, 60);
highScoreBox.setBounds(x_pos + 21, y_pos + 60, 80, 30);
totalGamesBox.setBounds(x_pos + 120, y_pos, 100, 90);
checkBox.setBounds(x_pos + 1, y_pos + 60, 20, 30);
gameinformationButton.setBounds(x_pos + 100, y_pos+ 60, 20, 30);
addButtons();
}
private void addButtons() {
table.add(checkBox);
table.add(gameButton);
table.add(highScoreBox);
table.add(totalGamesBox);
table.add(gameinformationButton);
}
public boolean getFavorite() { return favorite; }
private class ChooseGameListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
menu.startGame(gameMode);
//gameMode.execute(table ,frame);
}
}
private class ShowDescriptionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
//JButton button = (JButton) e.getSource();
JDialog ruleFrame = new JDialog(frame, true);
ruleFrame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
ruleFrame.setSize(400, 100);
JEditorPane rulesTextPane = new JEditorPane("text/html", "");
rulesTextPane.setEditable(false);
String rulesText;
rulesText = gameMode.getDesc();
rulesTextPane.setText(rulesText);
JScrollPane scroll = new JScrollPane(rulesTextPane);
ruleFrame.add(scroll);
ruleFrame.setVisible(true);
}
}
private class CheckFavoritesListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
JCheckBox myBox = (JCheckBox) e.getSource();
if( myBox.isSelected()) {
favorite = true;
} else {
favorite = false;
}
updateFavoriteInformation();
}
}
private String getGameInformation(String name) {
BufferedReader reader;
List<String> lines = new ArrayList<String>();
String highScores = "-,-";
try {
reader = new BufferedReader(new FileReader("SavedScores.txt"));
while(reader.ready()) {
lines.add(reader.readLine());
}
reader.close();
} catch(IOException i) {
i.printStackTrace();
}
for(int x = 0; x < lines.size(); x++)
{
List<String> games = Arrays.asList(lines.get(x).split(":"));
if(name.equals(games.get(0))) {
if(games.get(1).equals("0")) {
favorite = false;
} else {
favorite = true;
}
highScores = games.get(2);
break;
}
}
return highScores;
}
public void refreshScores() {
String[] highScores = getGameInformation(name).split(",");
highScoreBox.setText("Score: " + highScores[0] + "\n Time: " + highScores[1]);
int percentage = 0;
if(!highScores[2].equals("0")) {
percentage = (int)((float)(Integer.parseInt(highScores[3]) / (float)Integer.parseInt(highScores[2]) * 100));
}
totalGamesBox.setText("Games: " + highScores[2] + "\nWins: " + highScores[3] + "\n% Wins: " + percentage + "%"+ "\nLast Score: " + highScores[4] + "\nLast Time: " + highScores[5]);
}
private void updateFavoriteInformation() {
BufferedReader reader;
List<String> lines = new ArrayList<String>();
String savedLines = "";
try {
reader = new BufferedReader(new FileReader("SavedScores.txt"));
while(reader.ready()) {
lines.add(reader.readLine());
}
reader.close();
} catch(IOException i) {
i.printStackTrace();
}
for(int x = 0; x < lines.size(); x++)
{
List<String> games = Arrays.asList(lines.get(x).split(":"));
if(name.equals(games.get(0))) {
if(favorite) {
games.set(1, "1");
} else {
games.set(1, "0");
}
savedLines = savedLines + games.get(0) + ":" + games.get(1) + ":" + games.get(2) + "\n";
} else {
savedLines = savedLines + lines.get(x) + "\n";
}
}
try {
PrintWriter writer = new PrintWriter("SavedScores.txt");
writer.print(savedLines);
writer.close();
} catch(IOException i) {
i.printStackTrace();
}
}
}