-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumbleFrame.java
More file actions
282 lines (231 loc) · 9.23 KB
/
JumbleFrame.java
File metadata and controls
282 lines (231 loc) · 9.23 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
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class JumbleFrame extends JFrame {
String lang="English";// start off without a language
// all the languages that have been loaded up. Maybe only need
// one LanguageMap at a time, but can change that later
Set<LanguageMap> languageMaps = new HashSet<LanguageMap>();
public JumbleFrame() {
super("Let's solve the Jumble!");// title the bar
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// ALWAYS DO THIS
addMenuBar();// just wanted to show you how to make a menu bar
addSplashPanel();// start screen you can add your trademark images
pack();// make sure the frame is just big enough to show all components
}
private void addSplashPanel() {
// Things are typically added to the content pane
// Opening Screen could include animations, etc.
// it is a type of JPanel
this.getContentPane().add(new OpeningScreen());
}
private void addMenuBar() {
// built-in Object that behaves like typical menu bar. This behavior
// could change based on the OS of the machine it is running on.
JMenuBar jmb = new JMenuBar();
this.setJMenuBar(jmb);
// this File menu is just like most applications have
JMenu fileMenu = new JMenu("File");
jmb.add(fileMenu);
// Menu Items are added to Menus which are on the MenuBar
// You can add menu items to menu items...
JMenuItem newPuzzle = new JMenuItem("New Jumble");
// when the user selects the newPuzzle Menu Item, it tells
// everyone in its ActionListenerList to do their actionPerformed event
// We know they all have actionPerformed, because they implement ActionListener
// We know they implement ActionListener because they couldn't have been added to
// the list unless they did...
newPuzzle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// when someone clicks on the newPuzzle MenuItem this method is called
if(lang==null) {// check to see if they have already loaded up a language
JOptionPane.showMessageDialog(null,
"Select a language first!");
return;
}
// here might be a good time to change the device
// keyboard to match the language :)
loadJumblePanel();
}
});
fileMenu.add(newPuzzle);// adds to the File Menu
// a menu for choosing the language of the puzzle
JMenu languageMenu = new JMenu("Language");
jmb.add(languageMenu);
// menu for English
JMenu englishMenu = new JMenu("English");
// this adds a menu to a menu. Only do this if you want submenus
languageMenu.add(englishMenu);
// now I'll make a menu item for a type of English
JMenuItem kingsEnglishMenuItem = new JMenuItem("King's English");
kingsEnglishMenuItem.addActionListener(new ActionListener() {
// When user selects this menu Item (or any...) languageSelected is called
@Override
public void actionPerformed(ActionEvent arg0) {
languageSelected("English");
}
});
englishMenu.add(kingsEnglishMenuItem);
// Made one for kings english, lets make one for Aussie
JMenuItem aussieMenuItem = new JMenuItem("Australian!");
aussieMenuItem.addActionListener(new ActionListener() {
// When user selects this menu Item (or any...) languageSelected is called
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Crikey mate! Haven't finished that yet... \nToo many shrimp on the barbie!"+
"\nTry again next version!");
}
});
englishMenu.add(aussieMenuItem);
// Obviously, you can add many languages this way. Because these
// JMenuItems have no ActionListeners associated with them, nothing
// happens when you select them
languageMenu.add(new JMenuItem("Spanish"));
languageMenu.add(new JMenuItem("Farsi"));
}
protected void loadJumblePanel() {
// Here is where you would load up the JumblePanel/JumbleView
Dimension d=this.getContentPane().getComponent(0).getPreferredSize();
this.getContentPane().removeAll();
//LanguageMap map = getCurrentLanguage();
LanguageMap map = new LanguageMap("English", null);
JumblePanel jp = new JumblePanel(map);
jp.setPreferredSize(d);
jp.loadDictionary();
this.getContentPane().add(jp);
pack();
validate();
Font font = new Font("Comic Sans MS", Font.PLAIN, 50);
JTextField textBox = new JTextField(20);
textBox.setSize(400, 100);
textBox.setLocation(0, 0);
textBox.setFont(font);
textBox.setText("Input");
JTextField textBox2 = new JTextField(20);
textBox2.setSize(400, 100);
textBox2.setLocation(0, 225);
textBox2.setFont(font);
textBox2.setText("Input");
JTextField textBox3 = new JTextField(20);
textBox3.setSize(400, 100);
textBox3.setLocation(0, 450);
textBox3.setFont(font);
textBox3.setText("Input");
JTextField textBox4 = new JTextField(20);
textBox4.setSize(400, 100);
textBox4.setLocation(0, 675);
textBox4.setFont(font);
textBox4.setText("Input");
jp.add(textBox);
jp.add(textBox2);
jp.add(textBox3);
jp.add(textBox4);
JTextField answerBox = new JTextField();
answerBox.setSize(1000, 200);
answerBox.setLocation(500, 0);
answerBox.setFont(font);
answerBox.setText("Answer will appear here");
JTextField answerBox2 = new JTextField();
answerBox2.setSize(1000, 200);
answerBox2.setLocation(500, 225);
answerBox2.setFont(font);
answerBox2.setText("Answer will appear here");
JTextField answerBox3 = new JTextField();
answerBox3.setSize(1000, 200);
answerBox3.setLocation(500, 450);
answerBox3.setFont(font);
answerBox3.setText("Answer will appear here");
JTextField answerBox4 = new JTextField();
answerBox4.setSize(1000, 200);
answerBox4.setLocation(500, 675);
answerBox4.setFont(font);
answerBox4.setText("Answer will appear here");
JTextField answerBox5 = new JTextField();
answerBox5.setSize(1000, 180);
answerBox5.setLocation(500, 900);
answerBox5.setFont(font);
answerBox5.setText("Multi-word answer");
jp.add(answerBox);
jp.add(answerBox2);
jp.add(answerBox3);
jp.add(answerBox4);
jp.add(answerBox5);
JButton button = new JButton();
button.setFont(font);
button.setText("Solve");
button.setSize(400, 100);
button.setLocation(0, 850);
jp.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tempOne = textBox.getText().toLowerCase();
String tempTwo = textBox2.getText().toLowerCase();
String tempThree = textBox3.getText().toLowerCase();
String tempFour = textBox4.getText().toLowerCase();
String one = jp.solve(alphabetize(tempOne));
String two = jp.solve(alphabetize(tempTwo));
String three = jp.solve(alphabetize(tempThree));
String four = jp.solve(alphabetize(tempFour));
System.out.println(one + "one");
System.out.println(two + "two");
System.out.println(three + "three");
System.out.println(four + "four");
answerBox.setText(one);
answerBox2.setText(two);
answerBox3.setText(three);
answerBox4.setText(four);
}
});
JButton multiButton = new JButton();
multiButton.setFont(font);
multiButton.setText("Solve Final");
multiButton.setSize(400, 100);
multiButton.setLocation(0, 975);
jp.add(multiButton);
multiButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String temp = JOptionPane.showInputDialog("Input the scrambled multi-word");
String multi = temp.toLowerCase();
String multiSolution = jp.multisolve(multi);
answerBox5.setText(multiSolution);
}
});
repaint();
}
public String alphabetize(String a){
char[] chars = a.toCharArray();
Arrays.sort(chars);
String alphabetized = new String(chars);
alphabetized.toLowerCase();
return alphabetized;
}
private LanguageMap getCurrentLanguage() {
// TODO Auto-generated method stub
return null;
}
protected void languageSelected(String string) {
this.lang=string;
// check to see if any LanguageMaps have this language
for(LanguageMap lm:this.languageMaps) {
if(lm.getLanguage().equals(string))
return;// this language has already been loaded
}
// new language, so construct a new LanguageMap and add it
// to the languageMaps ... This involves reading from a file,
// so you don't want to repeat this and you don't want to load
// up languages that may not be needed
}
}