
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
/**
 * Class MetricUnitConversion - 
 * 
 * @author N.Kuder 
 * @version 1.6 12/9/2015
 */
public class MetricUnitConversion extends JApplet implements ActionListener
{
   // Declaring all of the variables that are to be used in the applet
   ImageIcon MetersImageIcon, GramsImageIcon, LitersImageIcon;
   Image MetersImg, GramsImg, LitersImg;
   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")};
       
  
   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=+4>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()
    {
       Unitspane.setLayout(new GridLayout(3,2));
       WESTpane.setLayout(new GridLayout(3,2));
       EASTpane.setLayout(new GridLayout(3,2));
       MetersImg = getImage( getCodeBase(), "meters.jpg");
       LitersImg = getImage( getCodeBase(), "liters.jpg"); 
       GramsImg = getImage( getCodeBase(), "grams.jpg");
      MetersImageIcon = new ImageIcon(MetersImg);
      GramsImageIcon = new ImageIcon(GramsImg);
      LitersImageIcon = new ImageIcon(LitersImg);
      
       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
           Unitspane.add( unit[i]);
           unit[i].addActionListener(this);
           //add(UnitsLabel[i]);
           add( Unitspane);//putting the pane in the applet
        }
       unit[0].setIcon(MetersImageIcon);
       unit[1].setIcon(GramsImageIcon);
       unit[2].setIcon(LitersImageIcon);
        
        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);
        }
         
        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, "Start by typing in the value you want into one of the three boxes (depending on which base units you want).\n Then hit enter. Then you can select the type of units you want to convert it to and it should display your answer.\n  You can change the base units at any time by pressing one of the three buttons.", "help", JOptionPane.QUESTION_MESSAGE );
        }
    }
       public void paint (Graphics g)
    {
        super.paint(g);
    }
    
    
}