001: /*
002: * Copyright (c) 2007, Sun Microsystems, Inc.
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * * Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in
012: * the documentation and/or other materials provided with the distribution.
013: * * Neither the name of Sun Microsystems, Inc. nor the names of its
014: * contributors may be used to endorse or promote products derived
015: * from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
021: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
022: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
023: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: /* Anagram Game Application */
031:
032: package com.toy.anagrams.ui;
033:
034: import com.toy.anagrams.lib.WordLibrary;
035: import java.awt.Dimension;
036: import java.awt.Point;
037: import java.awt.Toolkit;
038: import java.awt.event.ActionListener;
039: import java.awt.event.WindowListener;
040: import javax.swing.JFrame;
041:
042: /**
043: * Main window of the Anagram Game application.
044: */
045: public class Anagrams extends JFrame {
046:
047: public static void main(String[] args) {
048: new Anagrams().setVisible(true);
049: }
050:
051: private int wordIdx = 0;
052:
053: /** Creates new form Anagrams */
054: public Anagrams() {
055: initComponents();
056: getRootPane().setDefaultButton(guessButton);
057: scrambledWord.setText(WordLibrary.getScrambledWord(wordIdx));
058: pack();
059: guessedWord.requestFocusInWindow();
060: // Center in the screen
061: Dimension screenSize = Toolkit.getDefaultToolkit()
062: .getScreenSize();
063: Dimension frameSize = getSize();
064: setLocation(new Point((screenSize.width - frameSize.width) / 2,
065: (screenSize.height - frameSize.width) / 2));
066: }
067:
068: /** This method is called from within the constructor to
069: * initialize the form.
070: * WARNING: Do NOT modify this code. The content of this method is
071: * always regenerated by the Form Editor.
072: */
073: private void initComponents() {//GEN-BEGIN:initComponents
074: java.awt.GridBagConstraints gridBagConstraints;
075:
076: mainPanel = new javax.swing.JPanel();
077: scrambledLabel = new javax.swing.JLabel();
078: scrambledWord = new javax.swing.JTextField();
079: guessLabel = new javax.swing.JLabel();
080: guessedWord = new javax.swing.JTextField();
081: feedbackLabel = new javax.swing.JLabel();
082: buttonsPanel = new javax.swing.JPanel();
083: guessButton = new javax.swing.JButton();
084: nextTrial = new javax.swing.JButton();
085: mainMenu = new javax.swing.JMenuBar();
086: fileMenu = new javax.swing.JMenu();
087: aboutMenuItem = new javax.swing.JMenuItem();
088: exitMenuItem = new javax.swing.JMenuItem();
089:
090: setTitle("Anagrams");
091: addWindowListener(new java.awt.event.WindowAdapter() {
092: public void windowClosing(java.awt.event.WindowEvent evt) {
093: exitForm(evt);
094: }
095: });
096:
097: mainPanel.setLayout(new java.awt.GridBagLayout());
098:
099: mainPanel.setBorder(new javax.swing.border.EmptyBorder(
100: new java.awt.Insets(12, 12, 12, 12)));
101: mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
102: scrambledLabel.setText("Scrambled Word:");
103: gridBagConstraints = new java.awt.GridBagConstraints();
104: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
105: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
106: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
107: mainPanel.add(scrambledLabel, gridBagConstraints);
108:
109: scrambledWord.setColumns(20);
110: scrambledWord.setEditable(false);
111: gridBagConstraints = new java.awt.GridBagConstraints();
112: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
113: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
114: gridBagConstraints.weightx = 1.0;
115: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
116: mainPanel.add(scrambledWord, gridBagConstraints);
117:
118: guessLabel.setDisplayedMnemonic('Y');
119: guessLabel.setLabelFor(guessedWord);
120: guessLabel.setText("Your Guess:");
121: gridBagConstraints = new java.awt.GridBagConstraints();
122: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
123: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
124: gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
125: mainPanel.add(guessLabel, gridBagConstraints);
126:
127: guessedWord.setColumns(20);
128: gridBagConstraints = new java.awt.GridBagConstraints();
129: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
130: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
131: gridBagConstraints.weightx = 1.0;
132: gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
133: mainPanel.add(guessedWord, gridBagConstraints);
134:
135: feedbackLabel.setText(" ");
136: gridBagConstraints = new java.awt.GridBagConstraints();
137: gridBagConstraints.gridx = 1;
138: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
139: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
140: gridBagConstraints.weightx = 1.0;
141: gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
142: mainPanel.add(feedbackLabel, gridBagConstraints);
143:
144: buttonsPanel.setLayout(new java.awt.GridBagLayout());
145:
146: guessButton.setMnemonic('G');
147: guessButton.setText("Guess");
148: guessButton.setToolTipText("Guess the scrambled word.");
149: guessButton
150: .addActionListener(new java.awt.event.ActionListener() {
151: public void actionPerformed(
152: java.awt.event.ActionEvent evt) {
153: guessedWordActionPerformed(evt);
154: }
155: });
156:
157: gridBagConstraints = new java.awt.GridBagConstraints();
158: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
159: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
160: gridBagConstraints.weightx = 1.0;
161: gridBagConstraints.weighty = 1.0;
162: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
163: buttonsPanel.add(guessButton, gridBagConstraints);
164:
165: nextTrial.setMnemonic('N');
166: nextTrial.setText("New Word");
167: nextTrial.setToolTipText("Fetch a new word.");
168: nextTrial
169: .addActionListener(new java.awt.event.ActionListener() {
170: public void actionPerformed(
171: java.awt.event.ActionEvent evt) {
172: nextTrialActionPerformed(evt);
173: }
174: });
175:
176: gridBagConstraints = new java.awt.GridBagConstraints();
177: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
178: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
179: gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
180: gridBagConstraints.weighty = 1.0;
181: buttonsPanel.add(nextTrial, gridBagConstraints);
182:
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
185: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
186: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
187: gridBagConstraints.weighty = 1.0;
188: mainPanel.add(buttonsPanel, gridBagConstraints);
189:
190: getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
191:
192: fileMenu.setMnemonic('F');
193: fileMenu.setText("File");
194: aboutMenuItem.setMnemonic('A');
195: aboutMenuItem.setText("About");
196: aboutMenuItem.setToolTipText("About");
197: aboutMenuItem
198: .addActionListener(new java.awt.event.ActionListener() {
199: public void actionPerformed(
200: java.awt.event.ActionEvent evt) {
201: aboutMenuItemActionPerformed(evt);
202: }
203: });
204:
205: fileMenu.add(aboutMenuItem);
206:
207: exitMenuItem.setMnemonic('E');
208: exitMenuItem.setText("Exit");
209: exitMenuItem.setToolTipText("Quit Team, Quit!");
210: exitMenuItem
211: .addActionListener(new java.awt.event.ActionListener() {
212: public void actionPerformed(
213: java.awt.event.ActionEvent evt) {
214: exitMenuItemActionPerformed(evt);
215: }
216: });
217:
218: fileMenu.add(exitMenuItem);
219:
220: mainMenu.add(fileMenu);
221:
222: setJMenuBar(mainMenu);
223:
224: }//GEN-END:initComponents
225:
226: private void aboutMenuItemActionPerformed(
227: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
228: new About(this ).setVisible(true);
229: }//GEN-LAST:event_aboutMenuItemActionPerformed
230:
231: private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed
232: wordIdx = (wordIdx + 1) % WordLibrary.getSize();
233:
234: feedbackLabel.setText(" ");
235: scrambledWord.setText(WordLibrary.getScrambledWord(wordIdx));
236: guessedWord.setText("");
237: getRootPane().setDefaultButton(guessButton);
238:
239: guessedWord.requestFocusInWindow();
240: }//GEN-LAST:event_nextTrialActionPerformed
241:
242: private void exitMenuItemActionPerformed(
243: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
244: System.exit(0);
245: }//GEN-LAST:event_exitMenuItemActionPerformed
246:
247: private void guessedWordActionPerformed(
248: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed
249: if (WordLibrary.isCorrect(wordIdx, guessedWord.getText())) {
250: feedbackLabel.setText("Correct! Try a new word!");
251: getRootPane().setDefaultButton(nextTrial);
252: } else {
253: feedbackLabel.setText("Incorrect! Try again!");
254: guessedWord.setText("");
255: }
256:
257: guessedWord.requestFocusInWindow();
258: }//GEN-LAST:event_guessedWordActionPerformed
259:
260: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
261: System.exit(0);
262: }//GEN-LAST:event_exitForm
263:
264: // Variables declaration - do not modify//GEN-BEGIN:variables
265: private javax.swing.JMenuItem aboutMenuItem;
266: private javax.swing.JPanel buttonsPanel;
267: private javax.swing.JMenuItem exitMenuItem;
268: private javax.swing.JLabel feedbackLabel;
269: private javax.swing.JMenu fileMenu;
270: private javax.swing.JButton guessButton;
271: private javax.swing.JLabel guessLabel;
272: private javax.swing.JTextField guessedWord;
273: private javax.swing.JMenuBar mainMenu;
274: private javax.swing.JPanel mainPanel;
275: private javax.swing.JButton nextTrial;
276: private javax.swing.JLabel scrambledLabel;
277: private javax.swing.JTextField scrambledWord;
278: // End of variables declaration//GEN-END:variables
279:
280: }
|