// Michael Tartaglia
// Temp.JAVA	Program allows user to convert temperature between
//					Kelvin, celsius, and farenheit.


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Temp extends Applet
implements ActionListener {

 private double degrees = 0.0;				// current temp default
 private boolean wasTempEntered = false;  // glitch protection
 private String mesg = "";						// message to print
 private Color color;							// color of message to print

 private TextField entered;		// temperature entry field
 private Choice type;				// type of conversion to be employed
 private Button click;				// enter button

 public void init() {
  setBackground(Color.white);
  setLayout(new FlowLayout(FlowLayout.CENTER,2,2));

  // DISPLAY INSTRUCTIONS LABEL
  Label instruct = new Label("Type in a temperature and hit enter.");
  instruct.setFont(new Font("Arial",Font.BOLD,12));
  instruct.setBackground(Color.white);

  // DISPLAY FORM ELEMENTS
  entered = new TextField(5);
  entered.setText("");
  entered.setEditable(true);
  entered.addActionListener(this);

  type = new Choice();
  type.addItem("°F to °C");
  type.addItem("°F to °K");
  type.addItem("°C to °F");
  type.addItem("°C to °K");
  type.addItem("°K to °F");
  type.addItem("°K to °C");

  click = new Button("Enter");
  click.setEnabled(true);
  click.addActionListener(this);

  add(instruct); add(entered); add(type); add(click);
 }


 // CONVERTS INPUT NUMBER TO CELSIUS (ASSUMING TEMP IS IN F)
 public double getF (double temp) {
  return (temp*9)/5 + 32.0;
 }


 // CONVERTS INPUT NUMBER TO FARENHEIT (ASSUMING TEMP IS IN C)
 public double getC (double temp) {
  return ((temp-32.0)*5)/9;
 }


 public void setColorAndMessage (double temp)
 // FUNCTION: inputted temperature determines via if statements
 //			  the color and message to output

 {
  int red, grn, blu;

  if (temp < -273) {
   red = 0; grn = blu = 230;
   mesg = "Colder than absolute zero?!?";
  } else if (temp < -20) {
   red = 0; grn = blu = 220;
   mesg = "Wow! So verrry cold!";
  } else if (temp < 3) {
   red = 0; grn = 120; blu = 220;
   mesg = "It must be winter.";
  } else if (temp < 14) {
   red = 0; grn = 160; blu = 220;
   mesg = "It\'s fairly chilly, no?";
  } else if (temp < 21) {
   red = 0; grn = 220; blu = 120;
   mesg = "It seems nice, but wear a jacket!";
  } else if (temp < 25) {
   red = blu = 60; grn = 220;
   mesg = "Is it spring or autumn? Who cares!";
  } else if (temp < 30) {
   red = 180; grn = 180; blu = 30;
   mesg = "It\'s nice and warm. Enjoy the park!";
  } else if (temp < 37) {
   red = 200; grn = 120; blu = 0;
   mesg = "It\'s hot out! Go to the pool or beach.";
  } else if (temp < 43) {
   red = 220; grn = 60; blu = 0;
   mesg = "It\'s deathly outside! Stay hydrated!";
  } else if (temp < 53) {
   red = 220; grn = blu = 0;
   mesg = "Hot! Hot!! Hot!!!";
  } else if (temp < 101) {
   red = 180; grn = blu = 0;
   mesg = "Are you trying to boil water?!";
  } else if (temp < 150) {
   red = 160; grn = blu = 0;
   mesg = "You mind as well be walking on the sun.";
  } else {
   red = grn = blu = 140;
   mesg = "Now you are just being rediculous.";
  }

  color = new Color(red,grn,blu);		// SETTING THE COLOR
 }


 public void actionPerformed(ActionEvent E)
 throws NumberFormatException {
  double temp = 0.0;
  wasTempEntered = true;
  if (E.getSource() == entered || E.getSource() == click) {
   try {temp = Double.parseDouble(entered.getText());}	// READ NUMBER ENTERED
   catch (NumberFormatException nfe) {}
   switch (type.getSelectedIndex()) {				// READ IN CONVERSION CHOICE
      case 0: degrees = getC(temp);					// F TO C
			     setColorAndMessage(degrees); break;
      case 1: degrees = getC(temp) + 273;			// F TO K
			     setColorAndMessage(degrees-273); break;
      case 2: degrees = getF(temp);					// C TO F
			     setColorAndMessage(temp); break;
      case 3: degrees = temp + 273;					// C TO K
			     setColorAndMessage(temp); break;
      case 4: degrees = getF(temp-273);			// K TO F
			     setColorAndMessage(temp-273); break;
      case 5: degrees = temp - 273;					// K TO C
			     setColorAndMessage(degrees); break;
   }
  }
  repaint();
 }


 public void paint(Graphics g) {
  if (wasTempEntered) {
   g.setFont(new Font("Courier",Font.BOLD,12));
   g.drawString("Temperature: "+degrees,40,50);

   g.setColor(new Color(220,220,220));
   g.drawString(mesg,41,66);
   g.setColor(color);
   g.drawString(mesg,40,65);
  }
 }

}
