import javax.swing.*; import java.applet.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.text.*; public class FastAna extends Applet implements ActionListener { Object MyObject; JTextField InputLetters= new JTextField("Enter Letters", 60); JTextField tfSoFar= new JTextField("", 60); JTextField searchBar= new JTextField("", 60); TextArea Solutions= new TextArea( 10, 60 ); boolean AbortSearch; boolean URLOnError; String newLine; public void ProcessEvent( AWTEvent e ) { tfSoFar.setText( "Action" ); } public void ProcessActionEvent( ActionEvent e ) { tfSoFar.setText( "Action" ); } public void init() { newLine = System.getProperty("line.separator"); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); //ToolTip Tips = new ToolTip( ); setFont(new Font("Helvetica", Font.PLAIN, 14)); setLayout(gridbag); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( InputLetters, c); InputLetters.setToolTipText( "Enter the letter of the anagram here" ); add(InputLetters); InputLetters.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { InputLetters_actionPerformed(e); } }); // Create the copy down button JButton CopyDown = new JButton("CopyDown"); c.gridwidth = GridBagConstraints.LINE_START; c.weightx=1; gridbag.setConstraints( CopyDown, c); CopyDown.setToolTipText( "Copy letters from input field to Known field" ); add( CopyDown ); CopyDown.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { CopyDown_actionPerformed(e); } }); // Create the clear button JButton Clear = new JButton("Clear"); c.gridwidth = GridBagConstraints.CENTER; c.weightx = 1; gridbag.setConstraints( Clear, c); Clear.setToolTipText( "Clear the input field and the Known field" ); add( Clear ); Clear.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { Clear_actionPerformed(e); } }); // Create the copy up button JButton CopyUp = new JButton("CopyUp"); c.gridwidth = GridBagConstraints.REMAINDER; c.weightx=1; gridbag.setConstraints( CopyUp, c); CopyUp.setToolTipText( "Copy letters from the known field to the input field" ); add( CopyUp ); CopyUp.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { CopyUp_actionPerformed(e); } }); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( tfSoFar, c); tfSoFar.setToolTipText( "If you know where some letters are, enter them here e.g. '***k**' " ); add(tfSoFar); JButton Solve = new JButton("Solve"); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( Solve, c); Solve.setToolTipText( "Search for solutions using letters from the input field" ); add( Solve ); Solve.addActionListener(this); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( Solutions, c); //Solutions.setToolTipText( "Double click on a word to select it for searching" ); add(Solutions); // Add a button to search google JButton SearchGoogle = new JButton("Search Google"); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( SearchGoogle, c); SearchGoogle.setToolTipText( "Use Google to find a word" ); add( SearchGoogle ); SearchGoogle.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { SearchGoogle_actionPerformed(e); } }); // Add a button to search dictionary JButton SearchDictionary = new JButton("Search Dictionary"); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( SearchDictionary, c); SearchDictionary.setToolTipText( "Use Dictionary.Com to find a word" ); add( SearchDictionary ); SearchDictionary.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { SearchDictionary_actionPerformed(e); } }); // Add a button to search Wikipedia JButton SearchWiki = new JButton("Search Wikipedia"); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( SearchWiki, c); SearchWiki.setToolTipText( "Use Wikipedia to find a word" ); add( SearchWiki ); SearchWiki.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { SearchWiki_actionPerformed(e); } }); c.gridwidth = GridBagConstraints.REMAINDER; gridbag.setConstraints( searchBar, c); add(searchBar); } void Solutions_actionPerformed(TextEvent e) { AbortSearch = true; Solutions.append( "Search" + newLine ); } void SearchGoogle_actionPerformed(ActionEvent e) { String saveLetters = new String( "" ); URL searchURL = null; URLOnError = false; saveLetters = Solutions.getSelectedText(); searchBar.setText( saveLetters ); if( saveLetters.length() > 0 ) { String searchStr = new String( "http://www.google.com/search?hl=en&q=" + saveLetters ); try { searchURL = new URL( searchStr ); } catch( Exception ex ) { URLOnError = true; } if( !URLOnError ) { getAppletContext().showDocument( searchURL, "_blank" ); } } else { searchBar.setText( "Select search by double clicking on a word above" ); } } void SearchDictionary_actionPerformed(ActionEvent e) { String saveLetters = new String( "" ); URL searchURL = null; URLOnError = false; saveLetters = Solutions.getSelectedText(); searchBar.setText( saveLetters ); if( saveLetters.length() > 0 ) { String searchStr = new String( "http://dictionary.reference.com/browse/" + saveLetters ); try { searchURL = new URL( searchStr ); } catch( Exception ex ) { URLOnError = true; } if( !URLOnError ) { getAppletContext().showDocument( searchURL, "_blank" ); } } else { searchBar.setText( "Select search by double clicking on a word above" ); } } void SearchWiki_actionPerformed(ActionEvent e) { String saveLetters = new String( "" ); URL searchURL = null; URLOnError = false; saveLetters = Solutions.getSelectedText(); searchBar.setText( saveLetters ); if( saveLetters.length() > 0 ) { String searchStr = new String( "http://en.wikipedia.org/wiki/" + saveLetters.toLowerCase() ); try { searchURL = new URL( searchStr ); } catch( Exception ex ) { URLOnError = true; } if( !URLOnError ) { getAppletContext().showDocument( searchURL, "_blank" ); } } else { searchBar.setText( "Select search by double clicking on a word above" ); } } void CopyDown_actionPerformed(ActionEvent e) { String saveLetters = new String( "" ); saveLetters = InputLetters.getText(); tfSoFar.setColumns( saveLetters.length() ); tfSoFar.setText( saveLetters ); } void CopyUp_actionPerformed(ActionEvent e) { String saveLetters = new String( "" ); saveLetters = tfSoFar.getText(); InputLetters.setColumns( saveLetters.length() ); InputLetters.setText( saveLetters ); } void Clear_actionPerformed(ActionEvent e) { String saveLetters = new String( "" ); InputLetters.setColumns( saveLetters.length() ); InputLetters.setText( saveLetters ); tfSoFar.setColumns( saveLetters.length() ); tfSoFar.setText( saveLetters ); } void InputLetters_actionPerformed(ActionEvent e) { int c; String saveLetters = new String( "" ); String numDots = new String( "" ); tfSoFar.setText( "." ); saveLetters = InputLetters.getText(); for( c = 0; c < saveLetters.length(); c++ ) { numDots = numDots + "."; } tfSoFar.setColumns( saveLetters.length() ); tfSoFar.setText( numDots ); } public void setMyObject(Object MyObject) { MyObject = MyObject; } public void actionPerformed(ActionEvent e) { AbortSearch = false; Solutions.setText( "Search Started\n" ); solveAna(); Solutions.append("Search Complete" + newLine); } private void solveAna( ) { int[] VowelCount = { 0, 0, 0, 0, 0, 0 }; int[] ExtraVowels = { 0, 0, 0, 0, 0, 0 }; String saveLetters = new String( "" ); char[] letters = new char [ 80 ]; char first; String fileName; int len; int alphas; int EVowels; int c; int d; int e; int f; int Maxs; int countUnknowns; int usedUnknowns; BufferedReader fp; // Save our input string, and convert to upper case saveLetters = InputLetters.getText(); saveLetters = saveLetters.toUpperCase(); saveLetters.getChars( 0, saveLetters.length(), letters, 0 ); len = saveLetters.length(); // Initialise the vowel counts for( c = 0; c < 6; c++ ) { VowelCount[c] = 0; } // Count the number of each vowel that we've got in // the input string, also count the number of wildcards for( c = 0, alphas = 0, countUnknowns = 0; c < len; c++ ) { if( letters[c] < ' ' ) { letters[c] = ' '; } if( isalpha( letters[c] ) ) { alphas++; if( letters[c] == 'A' ) VowelCount[0]++; if( letters[c] == 'E' ) VowelCount[1]++; if( letters[c] == 'I' ) VowelCount[2]++; if( letters[c] == 'O' ) VowelCount[3]++; if( letters[c] == 'U' ) VowelCount[4]++; if( letters[c] == 'Y' ) VowelCount[5]++; } else { countUnknowns++; } } // Initialise the number of extra vowels, (to replace wildcards) for( EVowels = 0; EVowels < 5; EVowels++ ) { ExtraVowels[ EVowels ] = 0; } do { Maxs = 0; for( EVowels = 1; EVowels < 5; EVowels++ ) { Maxs += ExtraVowels[ EVowels ]; } Maxs = countUnknowns - Maxs; for( EVowels = 0; EVowels <= Maxs && !AbortSearch; EVowels++ ) { ExtraVowels[ 0 ] = EVowels; fileName = "dict/Length_" + Long.toString( alphas + countUnknowns ); fileName = fileName + "/A" + Long.toString( VowelCount[0] + ExtraVowels[0] ) + "_E" + Long.toString( VowelCount[1] + ExtraVowels[1] ) + "_I" + Long.toString( VowelCount[2] + ExtraVowels[2] ) + "_O" + Long.toString( VowelCount[3] + ExtraVowels[3] ) + "_U" + Long.toString( VowelCount[4] + ExtraVowels[4] ); ProcessFile( fileName, countUnknowns ); } ExtraVowels[ 1 ]++; for( EVowels = 1; EVowels < 5; EVowels++ ) { Maxs = 0; for( c = EVowels+1; c < 6; c++ ) { Maxs += ExtraVowels[ c ]; } Maxs = countUnknowns - Maxs; if( ExtraVowels[ EVowels ] > Maxs ) { ExtraVowels[ EVowels ] = 0; ExtraVowels[ EVowels + 1 ]++; } } }while( ExtraVowels[ 4 ] != countUnknowns && !AbortSearch); } public void ProcessFile( String fileName, int Unknowns ) { String Got; String saveSoFar = new String( "" ); char[] soFar = new char [ 80 ]; char[] letters = new char [ 80 ]; int d; int len; int DicSize; String dictionary[ ] = new String[ 10000 ]; int Index; String saveLetters = new String( "" ); int dlen; int alphas; int c; char[] dict = new char [ 80 ]; String fnd; int found; saveLetters = InputLetters.getText(); saveLetters = saveLetters.toUpperCase(); saveLetters.getChars( 0, saveLetters.length(), letters, 0 ); len = saveLetters.length(); Got = tfSoFar.getText(); if( Got.length() > 0 ) { saveSoFar = Got.toUpperCase(); saveSoFar.getChars(0, saveSoFar.length(), soFar, 0 ); } else { for( d = 0; d < len; d++ ) { soFar[d] = '.'; } } String text=null; DicSize = 0; try { URL url = new URL(getDocumentBase(), fileName ); BufferedReader stream = new BufferedReader ( new InputStreamReader(url.openStream())); do { text = stream.readLine(); if( text != null ) { dictionary[DicSize] = text; dictionary[DicSize].toUpperCase(); DicSize++; } }while( text != null ); } catch (IOException e) { e.printStackTrace(); } Index = -1; while( Index++ < DicSize-1 && !AbortSearch ) { saveLetters.getChars(0, saveLetters.length(), letters, 0 ); dlen = dictionary[ Index ].length(); dictionary[ Index ].getChars(0, dlen, dict, 0 ); alphas = 0; for( c = 0; c < 80; c++ ) { if( c < dlen ) { if( isalpha( dict[c] ) ) alphas++; } else { dict[c] = ' '; } } if( CheckMatch( len, soFar, dlen, dict ) ) { found = 0; for( c = 0; c < dlen; c++ ) { if( isalpha( dict[c] ) ) { for( d = 0; d < len; d++ ) { if( dict[c] == letters[d] ) { found++; letters[d] = '\0'; break; } } } } if( found + Unknowns == alphas ) { fnd = new String(); fnd = String.valueOf( dict, 0, alphas ); Solutions.append( fnd + newLine ); } } } } public boolean CheckMatch( int len, char[] inWord, int dlen, char[] dicLetters ) { int alphas; int c; int found = 0; int d; alphas = 0; for( c = 0; c < dlen; c++ ) { if( isalpha( dicLetters[c] ) ) alphas++; } if( alphas == len ) { alphas = 0; for( c = 0; c < len; c++ ) { if( isalpha( inWord[c] ) ) alphas++; } found = 0; for( d = 0, c = 0; c < len; c++, d++ ) { // Skip non-alphabetics in dictionary while( !isalpha(dicLetters[d]) && d < dlen ) { d++; } if( isalpha( inWord[c] ) ) { if( inWord[c] == dicLetters[d] ) found++; } } } return( found == alphas ); } private boolean isalpha( char c ) { if( c < 'A' || c > 'Z' ) { return( false ); } else { return( true ); } } public static void main(String args[]) { Frame f = new Frame("GridBag Layout Example"); FastAna ex1 = new FastAna(); ex1.init(); f.add("Center", ex1); f.pack(); f.setSize(f.getPreferredSize()); f.setVisible( true ); } }