/**
 * Dictionary Assignment using Arrays
 * url:http://www.geocities.ws/farishussein/hw5/Dictionary.html
 * @author (Faris Hussein) 
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Dictionary extends JApplet implements ActionListener
{

     JButton Switch,EnglishLanguage,FrenchLanguage;//3 buttons
     JButton Translate;
     int index =-1;
     boolean English = true ;
        JTextField tf = new JTextField( "English to French",20 );
        JTextField TextToTranslate = new JTextField("Enter Text Here To Translate",20);
        
         JTextField Output = new JTextField( " ",20);
       
         JPanel pane;
         int array_length=10;
        String []EnglishWords= new String[array_length];
        String []FrenchWords= new String[array_length];
          String []Photos= new String[array_length];
        
                          
                           
      
    
    public void init()
    {
         EnglishWords[0] = "Human";
            EnglishWords[1]="Car";
              EnglishWords[2]="Monkey";
                EnglishWords[3]= "Computer";
                  EnglishWords[4]="Table";
                    EnglishWords[5]="Camera";
                      EnglishWords[6]="Plane";
                        EnglishWords[7]="Glass";
                          EnglishWords[8]="Tomato";
                           EnglishWords[9]="Apple";
                           
                           
                           FrenchWords[0] = "Humain";
                           FrenchWords[1] = "Voiture";
                           FrenchWords[2] = "Singe";
                           FrenchWords[3] = "Ordinateur";
                           FrenchWords[4] = "Table";
                           FrenchWords[5] = "Caméra";
                           FrenchWords[6] = "Avion";
                           FrenchWords[7] = "Verre";
                           FrenchWords[8] = "Tomate";
                           FrenchWords[9] = "Pomme";
                           
                           Photos[0]="1.gif";
                           Photos[1]="2,jpg";
                           Photos[2]="3.jpg";
                           Photos[3]="4.jpg";
                           Photos[4]="5.gif";
                           Photos[5]="6.jpg";
                           Photos[6]="7.png";
                           Photos[7]="8.gif";
                           Photos[8]="9.png";
                           Photos[9]="10.jpg";
                           
                           
        setupButtons();
         tf.setEnabled(false);
    }
    public void setupButtons()
    {
       

        pane = new JPanel( );
       
        pane.setLayout( new FlowLayout( ) );
       
        

    Switch = new JButton("Switch language"); 
    
     Switch.addActionListener(this); 
    
    
    pane.add(Switch);
     EnglishLanguage = new JButton("English"); // English button to change input to English language
 
     EnglishLanguage.addActionListener(this);
      pane.add(EnglishLanguage);
      
     FrenchLanguage= new JButton("French"); //French button to change input to French language
     FrenchLanguage.addActionListener(this);
    pane.add(FrenchLanguage);
   pane.add(tf);
   pane.add(TextToTranslate);
    Translate = new JButton("Translate");
    Translate.addActionListener(this);
   pane.add(Translate);
   pane.add(Output);
    add(pane);
     
  
   

    }
    public void actionPerformed(ActionEvent event){
        Object obj = event.getSource();
        if(obj==Switch) // if the user presses Switch languages
       {
           
          
           if (English == true) //English is a global variable to know which lanuage are we using now
           
           {
            English= false;
           
            tf.setText("French To English");//change the language
           
            }
            else{
                
            English = true;
          tf.setText("English to French");//change the language
           
            }
        }
       else if(obj==EnglishLanguage) // if the user presses English
       {
           tf.setText("English To French");//change the language
           English= true;
        }
        else if(obj==FrenchLanguage) // if the user presses French
        {
        tf.setText("French to English");
        English = false;
        }
        else if(obj== Translate)//when the user fill text as input and press Translate button
        {
            String Temp = TextToTranslate.getText();//store the input in temp string
            
            if(English==true)//if the word in English then search the English array
            {
         index =SearchArray(EnglishWords,Temp);
        }
        else{ // if it's french then search the French array
         index =SearchArray(FrenchWords,Temp);
        }
        if(index==-1)//if the word is not found in the array then show a messagebox telling this
        {
       JOptionPane.showMessageDialog(null, "The word is not found in the dictionary,Make sure the first character is capital");
    }
    else // the word is found!
    {
        if(English==true) // if language is English, then found its translation in French Array
        {
        Output.setText(FrenchWords[index]);
    }
    
    else {// if language is French, then found its translation in English Array
    Output.setText(EnglishWords[index]);
    }
     JOptionPane.showMessageDialog(null, "The word is found in the dictionary");
    }
        }
       
   repaint(); // repaint everytime the user presses a new button
    }

    
    public int SearchArray(String []Array,String word){//function to search for a specific word as word string is an input
    for (int i=0; i< 10;i++)//compare the user input with each element in the array
    {
    if (Array[i].equals(word))//compare the user input with Element[i] in the array
    {
  return i;//return the index of element if found
    }

    }
        return -1;//return -1 if the element is not found
    }
    

   
    public void paint(Graphics g)
    {
       super.paint(g);
       Image  FlagImg;
       if(index!=-1)// if the word exists in the dictionary then print its picture on the applet
       {
       
        Image  img = getImage( getDocumentBase(), Photos[index] );
        g.drawImage( img, 200,200, this ); // get the car image 
    }
      
      if (English== true)// if the language is Englsih then print US flag on the applet
      {
         
          FlagImg = getImage( getDocumentBase(), "US.png" );
           
      g.drawImage( FlagImg, 0,80, this ); 
        }
        
        else// if the language is French then print France flag on the applet
        {
          FlagImg = getImage( getDocumentBase(), "France.png" );
      g.drawImage( FlagImg, 0,80, this ); 
        }
        
      
    }

   


    


   
}
