Source code: j3/KanjiCard.java
1 package j3;
2
3 import java.awt.*;
4 import java.awt.Color;
5 import java.awt.Font;
6 import java.awt.GridBagLayout;
7 import java.awt.GridBagConstraints;
8 import java.awt.Toolkit;
9 import java.awt.event.*;
10
11 import javax.swing.BorderFactory;
12 import javax.swing.BoxLayout;
13 import javax.swing.JButton;
14 import javax.swing.ButtonGroup;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JRadioButton;
19 import javax.swing.JSplitPane;
20 import javax.swing.JScrollPane;
21 import javax.swing.JTextArea;
22 import javax.swing.JTextField;
23
24 import java.util.Enumeration;
25 import java.util.Random;
26 import java.util.ResourceBundle;
27 import java.util.TreeMap;
28 import java.util.Vector;
29
30 /** KanjiCard, silly little game, I'm writing */
31 public class KanjiCard extends JFrame implements ActionListener {
32
33 protected JLabel statusBar = null;
34 protected ResourceBundle messages = null;
35 protected j3.J3 j3 = null;
36
37 /** Game info. */
38 protected static int totalGuesses = 0;
39 protected static int correctGuesses = 0;
40 protected static int currentCorrectGuessIndex = 0;
41 protected static int currentRandomCharIndex = 0;
42 protected static String[] currentKeys;
43
44 protected static ButtonGroup currentButtonGroup = new ButtonGroup();
45 protected static JLabel currentAnswerField = new JLabel();
46 protected static JLabel currentScore = new JLabel();
47 protected static int H_AREA = 3;
48 protected static int W_AREA = 8;
49 protected static JTextArea lastCorrectInfo = new JTextArea( W_AREA, H_AREA );
50
51 protected static Random random = new Random();
52
53 /** Creates a new KanjiCard, and passes some of the
54 * need info along with it:
55 * - JStatusBar statusBar -- statusBar for error/msg output
56 * - dict -- the DoubleMap implementation of the dictionary
57 */
58 public KanjiCard( JLabel statusBar, ResourceBundle messages, j3.J3 j3 ) {
59
60 // stat a bunch of stuff
61 // --------------------------------------------------
62 // CSTR related stuff, NON gui
63 // --------------------------------------------------
64 // if ( messages.getString("KanjiCard") == null )
65 // System.out.println("We have a problem.");
66
67 super( messages.getString("KanjiCard"));
68 this.statusBar = statusBar;
69 this.messages = messages;
70 this.j3 = j3;
71
72 // --------------------------------------------------
73 // CSTR related stuff, GUI only
74 // --------------------------------------------------
75 // setupGUI();
76 }
77
78
79
80 /**
81 * <code>setupGUI</code> sets up a KanjiCard GUI frame.
82 *
83 */
84 protected void setupGUI(){
85
86 statusBar.setText(messages.getString("KanjiCard"));
87
88 /** Create the new Frame and Panel to display
89 * the game in
90 */
91 final int SIZE = 600;
92
93 final JFrame newFrame = this;
94 JPanel leftScrollPane = new JPanel();
95 JPanel scrollerView = new JPanel();
96 JScrollPane rightScrollPane = new JScrollPane(scrollerView);
97 JSplitPane newSplitPane = new JSplitPane(
98 JSplitPane.HORIZONTAL_SPLIT,
99 leftScrollPane,
100 rightScrollPane );
101 newSplitPane.setDividerLocation( SIZE / 3 );
102
103 JPanel answerPanel = new JPanel();
104
105 // 0. Setup border and layout stuff
106 GridBagLayout gbl = new GridBagLayout();
107 GridBagConstraints gc = new GridBagConstraints();
108 gc.fill = GridBagConstraints.HORIZONTAL;
109
110 // 1. mark up the main Frame
111 // add the panel to the frame & setSize
112 newFrame.setSize( SIZE, SIZE / 2 );
113 newFrame.getContentPane().add( newSplitPane );
114
115 // add a nifty border
116 newSplitPane.setBorder( BorderFactory.createEtchedBorder() );
117
118
119 // 2. Setup the left pane
120 leftScrollPane.setLayout( gbl );
121
122 /** Restructuring here vitiates some of this code
123 * I need to pass around J3Dicts, NOT KanjiTrees
124 * whatever the hell those are!
125 * Since J3Dicts don't, right now, support
126 * some stuff needed for this, I'll leave the KTrees
127 */
128
129 // --------------------------------------------------
130 // Fixes start here
131 // --------------------------------------------------
132
133 Vector kanjiVector = new Vector();
134
135 for( Enumeration f = this.j3.getSpecificDictionaries(utils.charDics);
136 f.hasMoreElements(); ) {
137 kanjiVector.add( (J3Dict) f.nextElement());
138 }
139
140 TreeMap kanjiMap = new TreeMap( ((J3Dict)((Vector)kanjiVector).elementAt(0)).getL1CharacterMap());
141 int mapSize = 0;
142
143 if ( kanjiMap != null && kanjiMap.size() != 0 ) {
144 mapSize = kanjiMap.size();
145 }
146 else {
147 System.out.println("KANJICARD: Massive error! No Kanji!");
148 this.j3.statusBar.setText("No KanjiCard For You! (Something's wrong.)");
149 return;
150 }
151
152 int rightGuess = random.nextInt(4);
153 currentCorrectGuessIndex = rightGuess;
154
155 int randomChar = random.nextInt(mapSize);
156 currentRandomCharIndex = randomChar;
157
158 String[] keys = (String[]) kanjiMap.keySet().toArray( ((Object[])new String[0]) );
159 currentKeys = keys;
160
161 final int BIG_SIZE = 72;
162 gc.gridx=2; gc.gridy=0; gc.gridwidth=1; gc.gridheight=6;
163 Font charFont = new Font( "DIALOG", Font.PLAIN, BIG_SIZE );
164 JLabel charDisplay = new JLabel(); // keys[randomChar] );
165 currentAnswerField = charDisplay;
166 setupCharDisplay();
167 charDisplay.setFont( charFont );
168 charDisplay.setBorder( BorderFactory.createTitledBorder(messages.getString("qwCharacter")));
169
170 gc.insets = new Insets( 10, 10, 10, 10 );
171 gc.anchor = gc.CENTER;
172 gbl.setConstraints( charDisplay, gc );
173 leftScrollPane.add( charDisplay );
174
175 gc.gridx=0; gc.gridy=10; gc.gridwidth=5; gc.gridheight=2;
176 gc.insets = new Insets( 10, 10, 0, 0 );
177 JLabel scoreLabel = new JLabel( messages.getString("newScoreLabel"));
178 currentScore = scoreLabel;
179 gbl.setConstraints( scoreLabel, gc );
180 leftScrollPane.add( scoreLabel );
181
182 lastCorrectInfo = new JTextArea( messages.getString("initLastCorrect" ), H_AREA, W_AREA );
183 lastCorrectInfo.setForeground( Color.BLACK );
184 lastCorrectInfo.setLineWrap(true);
185 lastCorrectInfo.setEditable(false);
186 JScrollPane infoScroller = new JScrollPane(lastCorrectInfo);
187 gc.gridx=0; gc.gridy=12; gc.gridwidth=5; gc.gridheight=4;
188 gc.insets = new Insets( 5, 10, 0, 0 );
189 gbl.setConstraints( infoScroller, gc );
190 leftScrollPane.add( infoScroller );
191
192
193 // 3. Setup the right pane
194 // setup the answerPanel
195 answerPanel.setLayout( gbl );
196 answerPanel.setBorder( BorderFactory.createTitledBorder(messages.getString("qwGuesses")) );
197
198
199 if ( keys == null || keys.length == 0 ) {
200 System.out.println("Problems... Couldn\'t get any kanji to play with");
201 j3.statusBar.setText( "Quitting Game -- no Kanji." );
202 return;
203 }
204
205 // Setup the buttons
206 ButtonGroup buttonGroup = new ButtonGroup();
207 currentButtonGroup = buttonGroup;
208
209 for ( int i = 0; i < 6; i++ ){
210 // JRadioButton temp = new JRadioButton(Uchar);
211 JRadioButton temp = new JRadioButton();
212 temp.setActionCommand( Integer.toString(i) );
213 gc.gridx=2; gc.gridy=i; gc.gridwidth=5; gc.gridheight=1;
214 gbl.setConstraints( temp, gc );
215 buttonGroup.add( temp );
216 answerPanel.add( temp );
217 }
218
219 setupGuesses();
220
221 scrollerView.setLayout( gbl );
222
223 // add the stuff to the right pane
224 // rightScrollPane.setLayout( gbl );
225 gc.gridx=0; gc.gridy=0; gc.gridwidth=6; gc.gridheight=6;
226 gc.insets = new Insets( 10, 10, 10, 10 );
227 gbl.setConstraints( answerPanel, gc );
228 scrollerView.add( answerPanel );
229
230 JButton nextButton = new JButton( messages.getString("qwNext"));
231 gc.gridx=0; gc.gridy=10; gc.gridwidth=1; gc.gridheight=1;
232 gc.insets = new Insets( 10, 10, 10, 10 );
233 gbl.setConstraints( nextButton, gc );
234 scrollerView.add( nextButton );
235 nextButton.addActionListener( new goNextActionListener( this ) );
236
237 JButton quitButton = new JButton( messages.getString("qwQuit"));
238 gc.gridx=1; gc.gridy=10; gc.gridwidth=1; gc.gridheight=1;
239 gc.insets = new Insets( 10, 10, 10, 10 );
240 gbl.setConstraints( quitButton, gc );
241 scrollerView.add( quitButton );
242 quitButton.addActionListener( new ActionListener(){
243 public void actionPerformed( ActionEvent e ){
244 newFrame.setVisible(false);
245 //return;
246 }
247 });
248
249 // 4. Go!
250 // newFrame.show() goes into the cyclic method
251 }
252
253
254 // Clean this muthafucka' up!
255 //
256 // Will I need this?
257 // public KanjiCard( stuff ){}
258 /** make the panel and stuff */
259 public void actionPerformed( ActionEvent e ){
260
261 setupGUI();
262
263 // play the game.
264 playGame();
265 }
266
267
268
269 /**
270 * <code>playGame</code>
271 *
272 */
273 public void playGame(){
274 // play the game.
275 pack();
276 show();
277 }
278
279
280 /**
281 * Describe <code>setupCharDisplay</code> method here.
282 *
283 * @param answerField a <code>JTextField</code> value
284 * @param newChar a <code>String</code> value
285 */
286 public void setupCharDisplay(){
287
288 currentRandomCharIndex = random.nextInt(currentKeys.length);
289 currentCorrectGuessIndex = random.nextInt(4);
290
291 currentAnswerField.setText(currentKeys[currentRandomCharIndex]);
292 }
293
294
295
296 /**
297 * Describe <code>setupGuesses</code> method here.
298 *
299 * @param group a <code>ButtonGroup</code> value
300 * @param rightGuess an <code>int</code> value
301 * @param randomChar an <code>int</code> value
302 * @param keys a <code>String[]</code> value
303 */
304 public void setupGuesses(){
305
306 int HIDDEN_BUTTON = 5;
307
308 j3.J3Dict tempDic = (j3.J3Dict) j3.getLexica(this.j3).elementAt(0);
309
310 int count = 0;
311 for ( Enumeration e = currentButtonGroup.getElements(); e.hasMoreElements(); ) {
312 JRadioButton jb = (JRadioButton) e.nextElement();
313 if ( count == currentCorrectGuessIndex ) {
314 jb.setText( catAnswers( tempDic.queryDictionary( currentKeys[currentRandomCharIndex] )));
315 jb.setSelected(false);
316 } // end of for (int i = 0; i < answers.length; i++)
317 else if ( count == HIDDEN_BUTTON ) {
318 jb.setSelected(true);
319 jb.setVisible(false);
320 } // end of for (int i = 0; i < answers.length; i++)
321
322 else {
323 jb.setText( catAnswers( tempDic.queryDictionary( currentKeys[random.nextInt(currentKeys.length)])));
324 jb.setSelected(false);
325 }
326
327 count++;
328
329 }
330 }
331
332
333
334 /**
335 * Describe <code>goNext</code> method here.
336 * This leaves a possible error that can occuror,
337 * 1 in 36,000,000 times, do you see it?
338 * To be more precise, 1 in (dicSize)^n (I chose n=2).
339 */
340 public void goNext(){
341
342 if ( currentButtonGroup.getSelection().getActionCommand().equals(Integer.toString(currentCorrectGuessIndex))) {
343 showCorrect(Color.GREEN);
344 updateScore(true);
345 } else {
346 // how to handle this?
347 showCorrect(Color.RED);
348 updateScore(false);
349 }
350
351 // must call setupCharDisplay THEN setupGuesses!
352 setupCharDisplay();
353 setupGuesses();
354 }
355
356
357
358
359 /**
360 * Creates a new <code>updateScore</code> instance.
361 *
362 * @param gotItRight a <code>boolean</code> value
363 */
364 protected void updateScore( boolean gotItRight ){
365 totalGuesses++;
366
367 if ( gotItRight ){
368 correctGuesses++;
369 currentScore.setText( messages.getString("scoreLabel") + " " + correctGuesses + " / " + totalGuesses );
370 }
371 else {
372 currentScore.setText( messages.getString("scoreLabel") + " " + correctGuesses + " / " + totalGuesses );
373 }
374 }
375
376
377
378 /**
379 * <code>showCorrect</code> shows the correct kanji in a
380 * kanjiInfoDisplay
381 *
382 */
383 public void showCorrect( Color color ){
384
385 j3.J3Dict tempDic = (j3.J3Dict) j3.getLexica(this.j3).elementAt(0);
386
387 if ( color == null )
388 color = Color.RED;
389
390 lastCorrectInfo.setForeground( color );
391
392 String updatedInfo = new String( "[ "
393 + currentKeys[currentRandomCharIndex]
394 + " ] => "
395 + catAnswers(tempDic.queryDictionary(currentKeys[currentRandomCharIndex])));
396
397 lastCorrectInfo.setText( updatedInfo );
398 }
399
400
401
402 /**
403 * Describe <code>catAnswers</code> method here.
404 *
405 * @param rightAnswers a <code>String[]</code> value
406 * @return a <code>String</code> value
407 */
408 protected String catAnswers( String[] rightAnswers ){
409
410 String rightAnswer = new String();
411
412 if ( rightAnswers == null || rightAnswers.length == 0 )
413 return null;
414
415 for (int i = 0; i < rightAnswers.length; i++) {
416 if ( i != 0 && i != rightAnswers.length )
417 rightAnswer += ", " + rightAnswers[i];
418 else
419 rightAnswer += " " + rightAnswers[i];
420 }
421 return rightAnswer;
422 }
423 }