/**
 * Fahrenheit.java
 *
 * Converts Fahrenheit temperatures to Celius.
 * @author Lewis and Loftus
 * Modified by THC to use Swing components and to modify the colors.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Fahrenheit extends JApplet implements ActionListener {
  private static final long serialVersionUID = 1L;
  
  private final int APPLET_WIDTH = 200;
  private final int APPLET_HEIGHT = 100;

  private JLabel resultLabel;
  private JTextField fahrenheit;

  /**
   * Sets up the applet with its various components.
   */
  public void init() {
    // Set up the labels.
    JLabel inputLabel = new JLabel("Enter Fahrenheit:");
    JLabel outputLabel = new JLabel("Temperature in Celsius:");
    resultLabel = new JLabel("N/A");
    inputLabel.setForeground(Color.blue);
    outputLabel.setForeground(Color.darkGray);
    resultLabel.setForeground(new Color(128, 0, 255)); // purple

    // Set up the text field and make the applet its listener.
    fahrenheit = new JTextField(5);
    fahrenheit.setBackground(Color.yellow);
    fahrenheit.setForeground(Color.red);
    fahrenheit.addActionListener(this);

    // For a JApplet, we add GUI components to its content pane.
    Container cp = getContentPane();
    // Place GUI components left to right, top to bottom, within the
    // content pane.
    cp.setLayout(new FlowLayout());

    // Add the GUI components to the content pane.
    cp.add(inputLabel);
    cp.add(fahrenheit);
    cp.add(outputLabel);
    cp.add(resultLabel);

    // Set the content pane's background color.
    cp.setBackground(Color.pink);

    setSize(APPLET_WIDTH, APPLET_HEIGHT);
    setVisible(true);
  }

  /**
   * Performs the conversion when the enter key is pressed in
   * the text field.
   */
  public void actionPerformed(ActionEvent event) {
    int fahrenheitTemp, celsiusTemp;

    // Get the text entered in the text field.
    String text = fahrenheit.getText();

    fahrenheitTemp = Integer.parseInt(text); // convert the text to an int
    celsiusTemp = (fahrenheitTemp - 32) * 5 / 9; // do the actual conversion

    // Convert the int celsius temperature to a String, and display it in
    // the result label.
    resultLabel.setText(Integer.toString(celsiusTemp));
  }
}
