001: /* Anagram Game Application */
002:
003: package com.toy.anagrams.ui;
004:
005: import com.toy.anagrams.lib.WordLibrary;
006: import java.awt.Dimension;
007: import java.awt.Point;
008: import java.awt.Toolkit;
009: import java.awt.event.ActionListener;
010: import java.awt.event.WindowListener;
011: import javax.swing.JFrame;
012:
013: //Management imports
014: import com.toy.anagrams.agent.JMXAgent;
015: import com.toy.anagrams.mbeans.*;
016:
017: /**
018: * Main window of the Anagram Game application.
019: */
020: public class Anagrams extends JFrame {
021:
022: public static void main(String[] args) throws Exception {
023: new Anagrams().setVisible(true);
024: }
025:
026: private int wordIdx = 0;
027:
028: /** Creates new form Anagrams */
029: public Anagrams() throws Exception {
030: initComponents();
031: getRootPane().setDefaultButton(guessButton);
032: scrambledWord.setText(WordLibrary.getScrambledWord(wordIdx));
033: pack();
034: guessedWord.requestFocusInWindow();
035: // Center in the screen
036: Dimension screenSize = Toolkit.getDefaultToolkit()
037: .getScreenSize();
038: Dimension frameSize = getSize();
039: setLocation(new Point((screenSize.width - frameSize.width) / 2,
040: (screenSize.height - frameSize.width) / 2));
041:
042: //JMX Management initialization
043: initManagement();
044:
045: }
046:
047: /**
048: * JMX initialization. Creates and initializes a JMX Agent.
049: * Connect Anagrams UI components to Management.
050: */
051: private void initManagement() throws Exception {
052: JMXAgent agent = JMXAgent.getDefault();
053:
054: // When the Anagrams game is first displayed, a word is proposed to the user.
055: // We must start time computing and set the current anagram
056: agent.getAnagramsStats().startThinking();
057: agent.getAnagramsStats().setCurrentAnagram(
058: WordLibrary.getScrambledWord(wordIdx));
059: }
060:
061: /** This method is called from within the constructor to
062: * initialize the form.
063: * WARNING: Do NOT modify this code. The content of this method is
064: * always regenerated by the Form Editor.
065: */
066: private void initComponents() {//GEN-BEGIN:initComponents
067: java.awt.GridBagConstraints gridBagConstraints;
068:
069: mainPanel = new javax.swing.JPanel();
070: scrambledLabel = new javax.swing.JLabel();
071: scrambledWord = new javax.swing.JTextField();
072: guessLabel = new javax.swing.JLabel();
073: guessedWord = new javax.swing.JTextField();
074: feedbackLabel = new javax.swing.JLabel();
075: buttonsPanel = new javax.swing.JPanel();
076: guessButton = new javax.swing.JButton();
077: nextTrial = new javax.swing.JButton();
078: mainMenu = new javax.swing.JMenuBar();
079: fileMenu = new javax.swing.JMenu();
080: aboutMenuItem = new javax.swing.JMenuItem();
081: exitMenuItem = new javax.swing.JMenuItem();
082:
083: setTitle("Anagrams");
084: addWindowListener(new java.awt.event.WindowAdapter() {
085: public void windowClosing(java.awt.event.WindowEvent evt) {
086: exitForm(evt);
087: }
088: });
089:
090: mainPanel.setLayout(new java.awt.GridBagLayout());
091:
092: mainPanel.setBorder(new javax.swing.border.EmptyBorder(
093: new java.awt.Insets(12, 12, 12, 12)));
094: mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
095: scrambledLabel.setText("Scrambled Word:");
096: gridBagConstraints = new java.awt.GridBagConstraints();
097: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
098: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
099: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
100: mainPanel.add(scrambledLabel, gridBagConstraints);
101:
102: scrambledWord.setColumns(20);
103: scrambledWord.setEditable(false);
104: gridBagConstraints = new java.awt.GridBagConstraints();
105: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
106: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
107: gridBagConstraints.weightx = 1.0;
108: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
109: mainPanel.add(scrambledWord, gridBagConstraints);
110:
111: guessLabel.setDisplayedMnemonic('Y');
112: guessLabel.setLabelFor(guessedWord);
113: guessLabel.setText("Your Guess:");
114: gridBagConstraints = new java.awt.GridBagConstraints();
115: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
116: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
117: gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
118: mainPanel.add(guessLabel, gridBagConstraints);
119:
120: guessedWord.setColumns(20);
121: gridBagConstraints = new java.awt.GridBagConstraints();
122: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
123: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
124: gridBagConstraints.weightx = 1.0;
125: gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
126: mainPanel.add(guessedWord, gridBagConstraints);
127:
128: feedbackLabel.setText(" ");
129: gridBagConstraints = new java.awt.GridBagConstraints();
130: gridBagConstraints.gridx = 1;
131: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
132: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
133: gridBagConstraints.weightx = 1.0;
134: gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
135: mainPanel.add(feedbackLabel, gridBagConstraints);
136:
137: buttonsPanel.setLayout(new java.awt.GridBagLayout());
138:
139: guessButton.setMnemonic('G');
140: guessButton.setText("Guess");
141: guessButton.setToolTipText("Guess the scrambled word.");
142: guessButton
143: .addActionListener(new java.awt.event.ActionListener() {
144: public void actionPerformed(
145: java.awt.event.ActionEvent evt) {
146: guessedWordActionPerformed(evt);
147: }
148: });
149:
150: gridBagConstraints = new java.awt.GridBagConstraints();
151: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
152: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
153: gridBagConstraints.weightx = 1.0;
154: gridBagConstraints.weighty = 1.0;
155: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
156: buttonsPanel.add(guessButton, gridBagConstraints);
157:
158: nextTrial.setMnemonic('N');
159: nextTrial.setText("New Word");
160: nextTrial.setToolTipText("Fetch a new word.");
161: nextTrial
162: .addActionListener(new java.awt.event.ActionListener() {
163: public void actionPerformed(
164: java.awt.event.ActionEvent evt) {
165: nextTrialActionPerformed(evt);
166: }
167: });
168:
169: gridBagConstraints = new java.awt.GridBagConstraints();
170: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
171: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
172: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
173: gridBagConstraints.weighty = 1.0;
174: buttonsPanel.add(nextTrial, gridBagConstraints);
175:
176: gridBagConstraints = new java.awt.GridBagConstraints();
177: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
178: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
179: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
180: gridBagConstraints.weighty = 1.0;
181: mainPanel.add(buttonsPanel, gridBagConstraints);
182:
183: getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
184:
185: fileMenu.setMnemonic('F');
186: fileMenu.setText("File");
187: aboutMenuItem.setMnemonic('A');
188: aboutMenuItem.setText("About");
189: aboutMenuItem.setToolTipText("About");
190: aboutMenuItem
191: .addActionListener(new java.awt.event.ActionListener() {
192: public void actionPerformed(
193: java.awt.event.ActionEvent evt) {
194: aboutMenuItemActionPerformed(evt);
195: }
196: });
197:
198: fileMenu.add(aboutMenuItem);
199:
200: exitMenuItem.setMnemonic('E');
201: exitMenuItem.setText("Exit");
202: exitMenuItem.setToolTipText("Quit Team, Quit!");
203: exitMenuItem
204: .addActionListener(new java.awt.event.ActionListener() {
205: public void actionPerformed(
206: java.awt.event.ActionEvent evt) {
207: exitMenuItemActionPerformed(evt);
208: }
209: });
210:
211: fileMenu.add(exitMenuItem);
212:
213: mainMenu.add(fileMenu);
214:
215: setJMenuBar(mainMenu);
216:
217: }//GEN-END:initComponents
218:
219: private void aboutMenuItemActionPerformed(
220: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
221: new About(this ).setVisible(true);
222: }//GEN-LAST:event_aboutMenuItemActionPerformed
223:
224: private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed
225:
226: wordIdx = (wordIdx + 1) % WordLibrary.getSize();
227:
228: //Update statistics and values
229: try {
230: JMXAgent.getDefault().getAnagramsStats().startThinking();
231: JMXAgent.getDefault().getAnagramsStats().setCurrentAnagram(
232: WordLibrary.getScrambledWord(wordIdx));
233: } catch (Exception e) {
234: e.printStackTrace();
235: }
236:
237: feedbackLabel.setText(" ");
238: scrambledWord.setText(WordLibrary.getScrambledWord(wordIdx));
239: guessedWord.setText("");
240: getRootPane().setDefaultButton(guessButton);
241:
242: guessedWord.requestFocusInWindow();
243: }//GEN-LAST:event_nextTrialActionPerformed
244:
245: private void exitMenuItemActionPerformed(
246: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
247: System.exit(0);
248: }//GEN-LAST:event_exitMenuItemActionPerformed
249:
250: private void guessedWordActionPerformed(
251: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed
252: if (WordLibrary.isCorrect(wordIdx, guessedWord.getText())) {
253:
254: //Update statistics
255: try {
256: JMXAgent.getDefault().getAnagramsStats().stopThinking();
257: } catch (Exception e) {
258: e.printStackTrace();
259: }
260:
261: feedbackLabel.setText("Correct! Try a new word!");
262: getRootPane().setDefaultButton(nextTrial);
263: } else {
264: feedbackLabel.setText("Incorrect! Try again!");
265: guessedWord.setText("");
266: }
267:
268: guessedWord.requestFocusInWindow();
269: }//GEN-LAST:event_guessedWordActionPerformed
270:
271: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
272: System.exit(0);
273: }//GEN-LAST:event_exitForm
274:
275: // Variables declaration - do not modify//GEN-BEGIN:variables
276: private javax.swing.JMenuItem aboutMenuItem;
277: private javax.swing.JPanel buttonsPanel;
278: private javax.swing.JMenuItem exitMenuItem;
279: private javax.swing.JLabel feedbackLabel;
280: private javax.swing.JMenu fileMenu;
281: private javax.swing.JButton guessButton;
282: private javax.swing.JLabel guessLabel;
283: private javax.swing.JTextField guessedWord;
284: private javax.swing.JMenuBar mainMenu;
285: private javax.swing.JPanel mainPanel;
286: private javax.swing.JButton nextTrial;
287: private javax.swing.JLabel scrambledLabel;
288: private javax.swing.JTextField scrambledWord;
289: // End of variables declaration//GEN-END:variables
290:
291: }
|