
import java.awt.*;
import javax.swing.*;
import java.text.*;
import javax.swing.JOptionPane;
import javax.swing.JDialog;

/**
 * Class ArrayAssignment - write a description of the class here
 * 
 * @author Chase Conner
 * @version JDK 8
 * @course 1130-01
 * @date 10-30-15
 * @geocities URL http://www.geocities.ws/chasec366/javaapplets/ArrayAssignment/ArrayAssignment.html
 */
public class ArrayAssignment extends JApplet 
{
    String[ ] EnglishWords = //declares initializes the text for the English words
        {"House", "Lamp", "Table", "Sofa", "Window", "Bathroom", "Bed",
        "Kitchen", "Door", "Chair"};
 
    
    String[ ] ItalianWords = //declares and initializes the text for the Italian words
        {"Casa", "Lampada", "Tabella", "Divano", "Muro", "Bagno", 
            "Letto", "Cucina", "Porta", "Sedia"};
    
    String[ ] imageFileNames = //declares and initializes the images' file na
        {"house.jpg", "lamp.jpg", "table.jpg", "sofa.jpg", 
        "window.jpg", "bathroom.jpg", "bed.jpg", "kitchen.jpg", "door.jpg", 
        "chair.jpg"};
    
    /* 
     * Declares adn initializes the gallery size, which is the number of images in the file to be
     * added to the applet
     * 
     * The gallerySize will be used as the termination number in the
     * getImage and icon loop.
     */
    int gallerySize = 10; 
    
    Image img; //declaration of an alias for the Image variable
    ImageIcon[ ] icons; //declaration for the actual future images 
    
    Image errorImage; //declares the image that will be used if there's an error 
    
    Image flagImage; //declaration for flag icon
    ImageIcon icon; //declaration for a second icon 
    
    String EnglishInput; //declares the string to be used to define the JOptionPane input dialog box
  
    String translatedWord; //variable to define the translated English word 
    
    int response; 
    boolean runAgain = true; 

public void init(){
        setLayout(new BorderLayout()); //changes the layout to border 
        
        icons = new ImageIcon[gallerySize]; // load the images into memory
   
        //loop that retrieves all of the household images and converts them to icons 
        for (int i=0; i<gallerySize; i++)
        {
            img = getImage( getDocumentBase( ), imageFileNames[i]);
            icons[i] = new ImageIcon( img );
        }          
        

        flagImage = getImage(getDocumentBase( ),"flags.png"); //retrieves the flag image 
        icon = new ImageIcon(flagImage); //converts the flag image to an icon 
       
        JOptionPane.showMessageDialog(null, "This applet translates any of the ten English words on the following word list into Italian. Memorize a word you want translated, and type it into the dialogue window shown after the word list. When you're ready, please click OK to view the list.", "Applet Instructions", JOptionPane.INFORMATION_MESSAGE);
        
        //shows a list of the available words to translate
        JOptionPane.showMessageDialog(null, EnglishWords, "Available Words to Translate Into Italian", JOptionPane.INFORMATION_MESSAGE);
       
        
        //creates a dialog box for where you will type in the English word you wish transalted to Italian 
        EnglishInput = (String)JOptionPane.showInputDialog(this, "English to Italian Translator", 
                                                     "English to Italian", JOptionPane.PLAIN_MESSAGE, 
                                                      icon, null, "enter your selected word from list here");
                                                      
        //converts the input to lowercase, thereby making it case insensitive                                               
        String lowerCase = EnglishInput.toLowerCase();
        
        String finalInput = lowerCase; //declares that the finalInput variable is the lowercase-converted input string
         
        /*
         * determines the entered English word by analyzing the string
         * and connecting it to the correct
         * array index
         */
        switch (finalInput){
            case "house":
                finalInput = EnglishWords[0]; break;
            case "lamp": 
                finalInput = EnglishWords[1]; break;
            case "table":
                finalInput = EnglishWords[2]; break;
            case "sofa":
                finalInput = EnglishWords[3]; break;
            case "window":
                finalInput = EnglishWords[4]; break;
            case "bathroom":
                finalInput = EnglishWords[5]; break;
            case "bed":
                finalInput = EnglishWords[6]; break;
            case "kitchen": 
                finalInput = EnglishWords[7]; break;
            case "door": 
                finalInput = EnglishWords[8]; break;
            case "chair":
                finalInput = EnglishWords[9]; break;
            }
        
     /*
     * Displays an output message box that 
     * has the correct translation and icon.
     * 
     * If the word doesn't exist in the translator,
     * it will display an error message. 
     */
       
        for (int i=0; i<EnglishWords.length; i++)
        {
           if (finalInput.equals(EnglishWords[i])){
                 JOptionPane.showMessageDialog(null, ItalianWords[i], "Italian Word", JOptionPane.INFORMATION_MESSAGE, icons[i]);
                 translatedWord = ItalianWords[i];
                 img = getImage( getDocumentBase( ), imageFileNames[i]);
                 break;
                }
           else if (i == EnglishWords.length-1){
                 JOptionPane.showMessageDialog(null, "Sorry, word is not found", "ERROR", JOptionPane.ERROR_MESSAGE);
                 translatedWord = "No translation available";
                 errorImage = getImage( getDocumentBase( ), "error.jpg");
                 break;
                }
            }
      


        }
    
public void start(){
     //asks the user if they want another word translated
     response = JOptionPane.showConfirmDialog(null, "Would you like to translate another word?", "Run Program Again?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
     
     //repeats the program if the user chooses to  
    if (response == JOptionPane.YES_OPTION){
        while (runAgain){
            init();
            start();
            break;
            }
        } 
      else {
            runAgain = false; 
            }
      }
}
           
            
        






