import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

/**
 * Class DictionaryArrays - translates 10 different words from English to Italian and from Italian to English. Every word has an image corresponding to it.
 * @author Atanas Ivanov 
 * CSCI 1130-01
 */

public class DictionaryArrays extends JApplet implements ActionListener
{
    String []englishArray = new String [10];   //adding all the main components of the program
    String []italianArray = new String[10];
    Image []imageArray = new Image[10];
    JButton english, italian;
    JLabel imagestring;
    JTextField input;
    ImageIcon icon;
    String userinput = " ";
    String infoUser;
    
    
    
    public void init()
    {
        setLayout (new BorderLayout () ); //setting the layout
        
        italian = new JButton ("Translate into Italian!"); //adding the two buttons
        english = new JButton ("Translate into English!");
       
        input = new JTextField ("Enter the word you just chose here and press ENTER on your keyboard, then click on one of the buttons to translate it:"); //instructions for the user
        
        infoUser = JOptionPane.showInputDialog("Choose a word to be translated: dog,cat,city,heart,team,gold,meat,question,phone,autumn"); //the first thing that the user sees
        
        
        imagestring = new JLabel  (" ");
        imagestring.setIcon(icon);
        
        add(italian, BorderLayout. SOUTH);
        add(english, BorderLayout. NORTH);
        add(imagestring, BorderLayout. CENTER);
        add(input, BorderLayout. WEST);

        english.addActionListener (this);
        italian.addActionListener (this);
        input.addActionListener (this);
        
        
        italianArray[0] = "cane"; //all the Italian words that are stored in the array
        italianArray[1] = "gatto";
        italianArray[2] = "citta";
        italianArray[3] = "cuore";
        italianArray[4] = "squadra";
        italianArray[5] = "oro";
        italianArray[6] = "carne";
        italianArray[7] = "domanda";
        italianArray[8] = "telefono";
        italianArray[9] = "autunno";

        

        englishArray[0] = "dog"; //The English words of the array
        englishArray[1] = "cat";
        englishArray[2] = "city";
        englishArray[3] = "heart";
        englishArray[4] = "team";
        englishArray[5] = "gold";
        englishArray[6] = "meat";
        englishArray[7] = "question";
        englishArray[8] = "phone";
        englishArray[9] = "autumn";
        

        imageArray[0] = getImage(getDocumentBase(), "dog.jpg"); //adding images for every word I have in the program
        imageArray[1] = getImage(getDocumentBase(), "cat.jpg");
        imageArray[2] = getImage(getDocumentBase(), "city.jpg");
        imageArray[3] = getImage(getDocumentBase(), "heart.jpg");
        imageArray[4] = getImage(getDocumentBase(), "team.jpg");
        imageArray[5] = getImage(getDocumentBase(), "gold.jpg");
        imageArray[6] = getImage(getDocumentBase(), "meat.jpg");
        imageArray[7] = getImage(getDocumentBase(), "question.jpg");
        imageArray[8] = getImage(getDocumentBase(), "phone.jpg");
        imageArray[9] = getImage(getDocumentBase(), "autumn.jpg"); 
          
    }

    
    public void actionPerformed (ActionEvent ae){ //creating the actionPerformed method
        Object obj = ae.getSource ();

         if (obj == italian) {
             for ( int i = 0; i < 10; i++) //the actual translation part
            {
                if(englishArray[i].equals(userinput)){icon=new ImageIcon(imageArray[i]);
                    imagestring.setIcon(icon);
                    imagestring.setText(italianArray[i]);
                    break;
                }
                
                if (i == 9) imagestring.setText ("The word is not in this dictionary!");
            }

        }
        
       else if (obj == english){
           for (int i = 0; i < 10; i++) //the other translation part
            {
                if(italianArray[i].equals(userinput)){icon=new ImageIcon(imageArray[i]);
                    imagestring.setIcon(icon);
                    imagestring.setText (englishArray[i]);
                break;
            }
                if (i == 9)imagestring.setText ("The word is not in this dictionary!");}

        }   
        else if (obj == input) {userinput = input.getText().toLowerCase();
            imagestring.setText (userinput);

        }

        }   
    }



