
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Class dictionary - translates some words to spanish and shows a picture of what it is
 * 
 * Gabe Monson 
 * 
 */
public class dictionary extends JApplet implements ActionListener
{
    //initialized arrays
    String [] englishArray;
    String [] spanishArray;
    String [] imagesArray;
    //initialized the text field
    JTextField englishField;
    //initialized the button
    JButton okBut;
    //initialized labels to say what to do
    JLabel spanishLabel, pictureLabel;
    
    //all the arrays to store data
    public void setupArrays()
    {
        //makes the english array
        englishArray = new String[10];
        englishArray [0] = "potato";
        englishArray [1] = "dog"; 
        englishArray [2] = "desk";
        englishArray [3] = "pants";
        englishArray [4] = "socks";
        englishArray [5] = "shoes";
        englishArray [6] = "shirt";
        englishArray [7] = "school";
        englishArray [8] = "cat";
        englishArray [9] = "book";
        
        //makes the spanish array
        spanishArray = new String[10];
        spanishArray [0] = "patata";
        spanishArray [1] = "perro";
        spanishArray [2] = "escritorio";
        spanishArray [3] = "pantalones";
        spanishArray [4] = "calcetines";
        spanishArray [5] = "Zapatos";
        spanishArray [6] = "camisa";
        spanishArray [7] = "escuela";
        spanishArray [8] = "gato";
        spanishArray [9] = "libro";
        
        //makes the array for images
        imagesArray  = new String[10];
        imagesArray [0] = "potato.jpeg";
        imagesArray [1] = "dog.jpeg";
        imagesArray [2] = "desk.jpg";
        imagesArray [3] = "pants.jpg";
        imagesArray [4] = "socks.jpg";
        imagesArray [5] = "shoes.jpg";
        imagesArray [6] = "shirt.jpg";
        imagesArray [7] = "school.jpg";
        imagesArray [8] = "cat.jpg";
        imagesArray [9] = "book.jpg";
        
        
        
    }
    
    //handle the button click and lookup the english word to give spanish translation and an image
     public void actionPerformed ( ActionEvent event)
    {
        //returns the source for the button getting pressed
        Object button = event.getSource();
        //checks that the source is the button
        if ( button == okBut)
            {
                //puts what is put into the text field into a string
                String englishWord = englishField.getText();
                int i;
                //makes the default to not be found
                boolean found = false;
                //search the englishArray for a matching word
                for (i=0; i<englishArray.length; i++)
                { 
                    //tests to see if the word input matches one in the storage
                    if ( englishWord.toLowerCase().equals(englishArray[i]))
                    {   
                        found = true;
                        break;
                    }
                    
                }
                //what to run if it does find a word match
                if (found == true)
                {
                    //matches the index number of the found english word with the one for the
                    //spanish array to use
                    spanishLabel.setText(spanishArray[i]);
                    //matches the index number of the found english word with the one for the
                    //image index
                    Image picture = getImage( getCodeBase(), imagesArray[i] );
                    ImageIcon pictureIcon = new ImageIcon(picture);
                    pictureLabel.setIcon (pictureIcon);
                }//what to run if it doesn't find a word match
                else
                {
                    //says that the input word was not found if it doesn't match with the english array
                    spanishLabel.setText(englishWord += " was not found");
                    //sets the image to a circle with a line to say the word wasn't found
                    Image picture = getImage( getCodeBase(), "no.jpeg" );
                    ImageIcon pictureIcon = new ImageIcon(picture);
                    pictureLabel.setIcon (pictureIcon);
                }
                
            }
        
    }
    
    //sets the layout and displays everything
    public void boxes()
    {
        //set the layout
        setLayout(new BorderLayout());
        //initialises the top panel and sets it to flow layout
        JPanel top = new JPanel();
        top.setLayout(new FlowLayout());
        //dislpays a text field to input a word
        englishField = new JTextField(10);
        top.add (new JLabel ("English word: "));
        top.add (englishField);
        //displays the button to press to translate
        okBut = new JButton("ok!");
        okBut.addActionListener(this);
        top.add(okBut);
        //displays the output of the spanish translation
        spanishLabel = new JLabel();
        top.add (new JLabel ("Spanish word: "));
        top.add (spanishLabel);
        //displays the panel at the top of the page
        add (top, BorderLayout.NORTH);
        
        //displays the picture of what the word is in the middle
        JPanel picturePanel = new JPanel();
        pictureLabel = new JLabel();
        picturePanel.add (pictureLabel);
        add (picturePanel, BorderLayout.CENTER);
        
        
    }
    
    public void init()
    {
        //i wanted to leave this here to see if it would help with browser compatibility or something
        JRootPane rootPane = this.getRootPane();    
        rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
        //calls the method for arrays
        setupArrays();
        //calls the method to display the user interface
        boxes();
        
    }


    //draws super paint
    public void paint(Graphics g)
    { //draws super paint
        super.paint(g);
        
        
        
    }

    
}
