Array Dictionary
This application had several requirements:
- Understand arrays
- Understand where to use arrays
- Be able to create an array
- Allocate memory to an array
- Access array elements
- Understand length of an array
- Use for and enhanced for loops to manipulate arrays
- Learn about class Arrays from java.util package
The Code
The main thing that was standing in my way was understanding how to read the data from my arrays and to discern if there was a match or not. It was a real challenge. Try using some English and Spanish words:
- hello
- goodbye
- yes
- no
- toilet
- food
- water
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.MalformedURLException; import java.net.URL; import javax.swing.*; /** * Class a translation app * * @author Tyler Persohn * @version 1.0 */ public class ArrayDictionary extends JApplet implements ActionListener{ private JLabel title, inputInstruction, imagePlaceholderLabel, instructionText, outputLabel, outputText; private JPanel inputPanel, outputPanel, imagePanel; private JTextField textField; private JButton translateWord; private String inputWord, outputWord; //declare and define english dictionary array String[] englishWords = { "HELLO", "GOODBYE", "YES", "NO", "PLEASE", "THANK YOU", "YOU'RE WELCOME", "TOILET", "WATER", "FOOD" }; //declare and define spanish dictionary array String[] spanishWords = { "HOLA", "ADIOS", "SI", "NO", "POR FAVOR", "GRACIAS", "DE NADA", "BAÑO", "AGUA", "COMIDA" }; Icon[] array; // declare image array public void init(){ // This is a workaround for a security conflict with some browsers JRootPane rootPane = this.getRootPane(); rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); //Layout components -----------------------------------------------// Container contentPane = getContentPane(); // Declare size of applet work space. contentPane.setBackground(Color.BLACK); // Set Background color. contentPane.setLayout(new BorderLayout()); // Set Layout style. contentPane.add(createTitlePanel(), BorderLayout.NORTH); // Title placement. contentPane.add(createButtonPanel(), BorderLayout.SOUTH); // Button placement. contentPane.add(createTextFieldPanel(), BorderLayout.CENTER);// TextField Placement. } private Component createTitlePanel() { // Title Attributes------------------------------------------------------// JPanel titlePanel = new JPanel(); // Place a panel to put everything in titlePanel.setLayout(new FlowLayout()); // Set Layout Style titlePanel.add(title = new JLabel("Array Dictionary", JLabel.CENTER));// Application title title.setForeground(Color.WHITE); // Text color titlePanel.setBackground(Color.BLACK); // Background Color return titlePanel; } private Component createTextFieldPanel(){ //Main text Panel---------------------------// JPanel textFieldPanel = new JPanel(); // A panel to put everything in textFieldPanel.setBackground(Color.BLACK);// Background color //Input subpanel--------------------------------// textFieldPanel.add(inputPanel = new JPanel()); // A panel to put all the input stuff in inputPanel.setBackground(Color.BLACK); // Background color //input text field and instructions-----------------------------------------------------------// inputPanel.add(inputInstruction = new JLabel("Enter English or Spanish text to translate:"));// Label for instructions inputInstruction.setForeground(Color.WHITE); // Text Color inputPanel.add(textField = new JTextField()); // Place the textfield for user input textField.setColumns(10); // Size of field //Output subpanel-------------------------------// textFieldPanel.add(outputPanel = new JPanel());// add the output panel to put everything in outputPanel.setBackground(Color.BLACK); // Background Color //label for output-------------------------------------------// outputPanel.add(outputLabel = new JLabel("Translation: ")); // label for output outputLabel.setForeground(Color.WHITE); // text color //translation output------------------------------------------// outputPanel.add(outputText = new JLabel("Translated Text")); // translated text outputText.setForeground(Color.WHITE); // text color //Images go here----------------------------------------------------------// outputPanel.add(imagePanel = new JPanel()); // add the panel to put images in imagePanel.add(imagePlaceholderLabel = new JLabel(" ")); // this is where images will go imagePanel.setBackground(Color.BLACK); // background color imagePlaceholderLabel.setForeground(Color.WHITE); // text color return textFieldPanel; } private Component createButtonPanel() { // Button Controls-----------------------------------------// JPanel controls = new JPanel(); // add the panel to put buttons in controls.setLayout(new FlowLayout()); // Layout style controls.add(translateWord = new JButton("Translate")); // Button title translateWord.addActionListener(this); // listener return controls; } private int findMatchingIndex(String[] words, String input){ //function for grabbing the array index--------// for (int i=0; i<words.length; i++){ // loop through the array if(words[i].equalsIgnoreCase(input)){ // compare input to our array return i; // return the index } // } // return -1; // if no match } @Override public void actionPerformed(ActionEvent event) { //Get variables-----------------------------------------------------// inputWord = textField.getText(); // User input int englishIndex = findMatchingIndex(englishWords, inputWord); // pass variables to findMatchingIndex function int spanishIndex = findMatchingIndex(spanishWords, inputWord); // pass variables to findMatchingIndex function int eImageIndex = findMatchingIndex(englishWords, inputWord); // int sImageIndex = findMatchingIndex(spanishWords, inputWord); // int index = Math.max(englishIndex, spanishIndex); // return largest number from arrays // create image array array = new ImageIcon[10]; try { array[0] = new ImageIcon(new URL("http://i.imgur.com/KkdKxMy.jpg")); array[1] = new ImageIcon(new URL("http://i.imgur.com/8FQbxhH.jpg")); array[2] = new ImageIcon(new URL("http://i.imgur.com/cQQecrl.jpg")); array[3] = new ImageIcon(new URL("http://i.imgur.com/Rs4iJhQ.jpg")); array[4] = new ImageIcon(new URL("http://i.imgur.com/x3aujlN.jpg")); array[5] = new ImageIcon(new URL("http://i.imgur.com/83snYJQ.jpg")); array[6] = new ImageIcon(new URL("http://i.imgur.com/83snYJQ.jpg")); array[7] = new ImageIcon(new URL("http://i.imgur.com/QFfGgHc.jpg")); array[8] = new ImageIcon(new URL("http://i.imgur.com/D7KjrY6.jpg")); array[9] = new ImageIcon(new URL("http://i.imgur.com/BdUSJQV.jpg")); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (index == -1) { outputText.setText("Word was not found."); } else { if (index == englishIndex) { outputText.setText(spanishWords[index]); imagePlaceholderLabel.setIcon(array[index]); } else { outputText.setText(englishWords[index]); imagePlaceholderLabel.setIcon(array[index]); } } } private ImageIcon Image(URL codeBase, String string) { return null; } }