
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
/**
 * Class MetricUnitConversion - 
 * 
 * @author N.Kuder 
 * @version 1.5 12/8/2015
 */
public class MetricUnitConversion extends JApplet implements ActionListener
{
   // Declaring all of the variables that are to be used in the applet
   double grams;
   double meters;
   double liters;
   String units;
   String modifyer;
   double input;
   double output;
   /**Creating an array of JButtons, these are grouped together because they are the conversions to smaller units
    * typically thought of as to the left when showing diagrams of the conversions, as such these buttons were desired to be to the 
    * left of the other conversions.  It is also worth noting that they are called WESTbuttons because the original applet used borderlayout,
    * since then it has been changed to a gridlayout but the name remains the same.
    */
   JButton WESTbuttons[] = { new JButton("pico"),
       new JButton("nano"), new JButton("micro"), new JButton("milli"), new JButton("centi"),
       new JButton("deci")};
   
   
    /**Creating an array of JButtons for the larger conversions, grouped together so that they can be to the right of the smaller conversions.
     * Once again these are called EASTbuttons because of the original applets layout.
     */   
   JButton EASTbuttons[] = {new JButton ("deca"), new JButton ("hecto"), new JButton("kilo"),
       new JButton("mega"), new JButton("giga"), new JButton("tetra")};
       
  /**This array of JButtons was originally created for buttons that would switch the units, this has since been changed to something that the 
   * textfields do so these buttons are no longer in the applet.  As the project description was to have three textfields for the inputs that is
   * what I did, however my original idea was to have one textfield for the input of the numerical value and then have buttons change the units,
   * so I have left the initilazation in so that the program can be easily modified to do that if I decide to in the future for my own use.
   * 
   */   
   JButton unit[] = {new JButton ("meters"), new JButton ("grams"), new JButton ("liters")};
   
    JPanel WESTpane = new JPanel();//Initializing a JPanel for the WESTbuttons, the buttons will be added in a loop
    
    JPanel EASTpane = new JPanel();//Initializing a JPanel for the EASTbuttons, the buttons will be added in a loop
    
    JPanel CENTERpane = new JPanel();//This was originally for the units buttons, it no longer has a purpose
    /**
     * Creating an array of text fields
     * These fields will be used to recieve the input of numeric values from the user as well as change the untis from a variation of meters,
     * liters, or grams
     */
    JTextField UnitsFields[] = {new JTextField("meters"), new JTextField("grams"), new JTextField("liters")};
    
    
    JLabel title = new JLabel("<HTML><FONT SIZE=+1>Metric Unit Conversions");//Creating the title in the form of a JLabel
    
    JPanel Unitspane = new JPanel();
    
    JButton help = new JButton("Help");//creating a help button
    
    JTextField AnswerField = new JTextField("output " + "unselected");//Creating a textfield that will display the converted number as well as the units
    DecimalFormat myFormat = new DecimalFormat(".0");//setting the decimal format to one decimal place
    JLabel UnitsLabel[] = {new JLabel("meters"), new JLabel("grams"), new JLabel("liters")};
    public void setupButtons()
    {
       
    }
    public void init()
    {
       getContentPane().setBackground(Color.white);//setting the background color
       setLayout( new GridLayout(2,3));//setting the layout to a grid layout
       add( title, BorderLayout.NORTH);//Adding the title to the applet in the first grid, upper left corner
       for(int i = 0; i<WESTbuttons.length; i++)
       {
           WESTbuttons[i].addActionListener( this );//adding an actionlistener to these buttons
           WESTpane.add( WESTbuttons[i]);//addung the buttons to a pane
           add( WESTpane);//adding the pane to the applet
        }
        
        for(int i = 0; i<EASTbuttons.length; i++)
       {
           EASTbuttons[i].addActionListener(this);//adding actionlistener to the buttons
           EASTpane.add( EASTbuttons[i]);//putting the buttons on a pane
           
           add( EASTpane);//adding the pane to the applet
        }
        help.addActionListener(this);
        add( help );
        for(int i = 0; i<UnitsFields.length; i++)
       {
           
           Unitspane.add( UnitsFields[i]);//adding the field to a pane
           UnitsFields[i].addActionListener(this);//adding an action listener to the fields
           
           //add(UnitsLabel[i]);
           add( Unitspane);//putting the pane in the applet
        }
       
        
        add( AnswerField);//adding the answer textfield
    }
    public void actionPerformed( ActionEvent ae )
    {
        Object obj = ae.getSource();
        //This method tells the applet what to do for the case of if each button is pressed. It does so via an if, if else statement
        if( obj == unit[0]){
         units = "meters";
         AnswerField.setText(output + modifyer + units);
        }//These first three parts are left over from previous versions, once again I left them there so that I could modify this with ease for my own use
         
        else if (obj == unit[1]){
         units = "grams";
         AnswerField.setText(output + modifyer + units);}
        else if (obj == unit[2]) {
         units = "liters";
         AnswerField.setText(output + modifyer + units);}
        else if ( obj == WESTbuttons[0]){
            modifyer = "pico";
            output = input;
            AnswerField.setText(output + "E12 "+modifyer + units);
        }
        else if (obj == WESTbuttons[1]){
            modifyer = "nano";
            output = input*1000000000;
            AnswerField.setText(output +" "+ modifyer + units);
        }
        else if (obj == WESTbuttons[2]){
            modifyer = "micro";
            output = input*1000000;
            AnswerField.setText(output + " "+ modifyer + units);
        }
        else if (obj == WESTbuttons[3]){
            modifyer = "milli";
            output = input*1000;
            AnswerField.setText(output +" "+ modifyer + units);
        }
        else if (obj == WESTbuttons[4]){
            modifyer = "centi";
            output = input*100;
            AnswerField.setText(output +" "+ modifyer + units);
        }
        else if (obj == WESTbuttons[5]){
            modifyer = "deci";
            output = input*10;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if ( obj == EASTbuttons[0]){
            modifyer = "deca";
            output = input*.1;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if (obj == EASTbuttons[1]){
            modifyer = "hecto";
            output = input*.01;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if (obj == EASTbuttons[2]){
            modifyer = "kilo";
            output = input*.001;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if (obj == EASTbuttons[3]){
            modifyer = "mega";
            output = input*.000001;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if (obj == EASTbuttons[4]){
            modifyer = "giga";
            output = input*.000000001;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if (obj == EASTbuttons[5]){
            modifyer = "tetra";
            output = input*.000000000001;
            AnswerField.setText(output +" " +modifyer + units);
        }
        else if (obj == UnitsFields[0]){
            input = Double.parseDouble(UnitsFields[0].getText());
             units = "meters";
        }
        else if (obj == UnitsFields[1]){
            input = Double.parseDouble(UnitsFields[1].getText());
            units = "grams";
        }
        else if (obj == UnitsFields[2]){
            input = Double.parseDouble(UnitsFields[2].getText());
            units = "liters";
        }
        else if (obj == help){
            JOptionPane.showMessageDialog( null, "blah blah blah", "help", JOptionPane.QUESTION_MESSAGE );
        }
    }
       public void paint (Graphics g)
    {
        super.paint(g);
    }
    
    
}