
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;


/**
 * @author Rosemary Ross
 * @course CSCI 1130-51
 * @date October 7, 2015
 * @description Dictionary translates word from English to Japanese and vice-versa.
 */


public class Dictionary extends JApplet implements ActionListener 
{
    
    JLabel title, imageLabel, translation;
    JButton englishButton, japaneseButton;
    JTextField wordField;
    JPanel inputPanel, outputPanel;
    String word;
    String[] wordPhotoFileName = {"dog.jpg", "bicycle.jpg", "snow.jpg", "sadFace.jpeg"};
    Image wordPhoto;
    ImageIcon wordIcon;
    String[] japaneseWords = {"inu", "jitensha", "yuki"};
    String[] englishWords = {"dog", "bicycle", "snow"};
    int i;
  
    
    public void init()
    {
        setLayout (new BorderLayout() );
        getContentPane().setBackground(Color.white);
        
        title = new JLabel ("<HTML><FONT SIZE=+4><I>English-Japanese Translator</I></FONT>", JLabel.CENTER);
        add( title, BorderLayout.NORTH );
                
        translation = new JLabel("Here's where your translation will appear!", JLabel.CENTER);
        add( translation, BorderLayout.EAST);
        
        setupInputPanel();

    }

         
    public void setupInputPanel()
    {
        englishButton = new JButton ("Translate to English");
        englishButton.addActionListener(this);
        japaneseButton = new JButton("Translate to Japanese");
        japaneseButton.addActionListener(this);
        
        wordField = new JTextField(10);
        
        inputPanel = new JPanel(new FlowLayout() );
        inputPanel.add(englishButton);
        inputPanel.add(wordField);
        inputPanel.add(japaneseButton);
                
        add( inputPanel, BorderLayout.WEST);
    }
              
    public void actionPerformed( ActionEvent event )
    {
        String word = wordField.getText();
        
        Object source = event.getSource();
        
        boolean foundJapanese = false;
        boolean foundEnglish = false;   
        
        int i = 0;
                       
        if( source == englishButton)
            for(i = 0; i < japaneseWords.length; i++)
            {
                if (foundJapanese = word.equals( japaneseWords[i]))
                {
                    translation.setText(englishWords[i]);
                    wordPhoto = getImage( getCodeBase( ), wordPhotoFileName[i]);
                    wordIcon = new ImageIcon( wordPhoto );
                    translation.setIcon( wordIcon );
                    break;
                }
                else 
                {
                    translation.setText("Word not found.");
                    wordPhoto = getImage( getCodeBase( ), wordPhotoFileName[3]);
                    wordIcon = new ImageIcon( wordPhoto );
                    translation.setIcon( wordIcon );
                }
            }                
                
        else if(source == japaneseButton)
            for(i = 0; i < englishWords.length; i++)
            {
                if (foundEnglish = word.equals( englishWords[i]))
                {
                    translation.setText(japaneseWords[i]);
                    wordPhoto = getImage( getCodeBase( ), wordPhotoFileName[i]);
                    wordIcon = new ImageIcon( wordPhoto);
                    translation.setIcon( wordIcon );
                    break;
                }
                else
                {
                    translation.setText("Word not found.");
                    wordPhoto = getImage( getCodeBase( ), wordPhotoFileName[3]);
                    wordIcon = new ImageIcon( wordPhoto );
                    translation.setIcon( wordIcon );
                }
            }
            repaint( );
            
            
        }
    }
