//package list
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

/**Course: CSci 1130-51
 * Project Description: This is an applet that translates from english to spanish or vice versa for 10 words.
 * @developer Nandu Chintapalli
 * @version 1.01 2015/10/6
 * website: http://www.geocities.ws/nanducweb/Dictionary/DictionaryArrays.html
 */

public class DictionaryArrays extends JApplet implements ActionListener{
//spanish words array
String[] spanishWords =   {"biblioteca","reloj",
"pirata", "volcan", "automóvil", 
"autobus", "playa", "lago", "cósmico", "bomba atómica"};
//english words array
String[] englishWords =  {"library", "clock", "pirate", 
"volcano",  "car", "bus", "beach",
"lake","space","atom bomb"};
//component declaration
String textFieldWord;
Image[] photos;
ImageIcon icon;
ImageIcon icontwo;
JButton getTranslation;
JTextField entry; 
JLabel imageviewer;
TextArea translate;
//variable declaration     
static int defaultX = 10;
static int defaultY = 10;
static int defaultW = 780;
static int defaultH = 50;

//method declaration
public void init() {
  //photos array
  photos = new Image[10];
  photos[0] = getImage(getCodeBase(), "library.jpg");
  photos[1] = getImage(getCodeBase(), "clock.jpg");
  photos[2] = getImage(getCodeBase(), "pirate.jpg");
  photos[3] = getImage(getCodeBase(), "volcano.jpg");
  photos[4] = getImage(getCodeBase(), "car.jpg");
  photos[5] = getImage(getCodeBase(), "bus.jpg");
  photos[6] = getImage(getCodeBase(), "beach.jpg");
  photos[7] = getImage(getCodeBase(), "lake.jpg");
  photos[8] = getImage(getCodeBase(), "space.jpg");
  photos[9] = getImage(getCodeBase(), "bomb.jpg");
  
  final JPanel outer = new JPanel(new BorderLayout());
  JPanel inner = new JPanel(new BorderLayout());
  JPanel viewer = new JPanel(new BorderLayout());
  JPanel visualviewer = new JPanel(new BorderLayout());
  // when the outer panel is added to the null layout
  final JLabel list = new JLabel("<HTML><FONT COLOR=RED>English</FONT> - library, clock, pirate, volcano, car, bus, beach, lake, space, atom bomb"
    +"<BR><FONT COLOR=RED>Spanish</FONT> - biblioteca, reloj, pirata, volcan, automóvil, autobus, playa, lago, cósmico, bomba atómica<BR>");
  translate = new TextArea("Your translation will show here");
  imageviewer = new JLabel(icon);
  viewer.add("West",translate);
  visualviewer.add("East",imageviewer);
  inner.add("Center",list);     
  //inner.add("West",toSpanish);
  //inner.add("East", toEnglish);
  outer.add("Center", inner);
  
  JPanel c = (JPanel)getContentPane();
  final JPanel nullLayoutPanel = new JPanel();
  nullLayoutPanel.setLayout(null);
  c.add("Center", nullLayoutPanel);
  // set the bounds of the panels manually
  nullLayoutPanel.add(outer);
  nullLayoutPanel.add(viewer);
  nullLayoutPanel.add(visualviewer);
  outer.setBounds(defaultX, defaultY, defaultW, defaultH);
  viewer.setBounds(20, 75, 400, 400);
  visualviewer.setBounds(485, 75, 400, 400);
  JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 30));
  //enters initial text into Jtextfield
  entry = new JTextField("Enter English or Spanish word to translate here");
  //clears the JTextfield when it is clicked on
  entry.addActionListener(this);
  entry.addMouseListener(new MouseAdapter(){
        @Override
        public void mouseClicked(MouseEvent e){
            entry.setText("");
            }});
  //adds button to translate
  getTranslation = new JButton("Translate");
  getTranslation.addActionListener(this);
  controlPanel.add(entry);
  controlPanel.add(getTranslation);
  c.add("South", controlPanel);
  //changes background color of JPanels
  viewer.setBackground(Color.blue);
  controlPanel.setBackground(Color.red);
  inner.setBackground(Color.yellow);
  visualviewer.setBackground(Color.white);
  outer.setBackground(Color.white);
  }

public void paint(Graphics g) {
    super.paint(g);

}
    public void actionPerformed (ActionEvent ae){
        if (ae.getSource() == getTranslation) {
        textFieldWord = (entry.getText().toLowerCase());

        translate.setText(null);

        String translatedWord = null;
        for (int english = 0; english < spanishWords.length; english++) {
            if (textFieldWord.equals(englishWords[english])) {
                translatedWord = spanishWords[english];
                translate.append(translatedWord + "\n");
                icon = new ImageIcon(photos[english]);
                imageviewer.setIcon(icon);
                break;
            }
        }

        if (translatedWord == null) {

            for (int spanish = 0; spanish < englishWords.length; spanish++) {
                if (textFieldWord.equals(spanishWords[spanish])) {
                    translatedWord = englishWords[spanish];
                    translate.append(translatedWord + "\n");
                icontwo = new ImageIcon(photos[spanish]);
                    imageviewer.setIcon(icontwo);
                    break;
                }
            }

        }

        if (translatedWord == null) {
            translate.append("A Spanish-English match is not in the Dictionary\n");
        }
    }
}
 }
