//Monica Linnell


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.LayoutPath;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class SeparatorDriver extends JFrame 
{
   private JButton celsius, fahrenheit;
   private JPanel guiPanel, fieldPanel, centerPanel, southPanel, celPanel, fahrenPanel;
   private JLabel clicked, progMsg, prompt, resultLabel;
   private JTextField CFfield;
   double celTemperature, fahrenTemperature;

   public SeparatorDriver()
   {
      super("Converting C-->F or F-->C Temperature");
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
   					
      buildCenterPanel();
      buildSouthPanel();
   			
      //add two panels and a separator created to north, center, and south regions of BorderLayout
      guiPanel = new JPanel();
      guiPanel.setLayout (new BorderLayout());   
      guiPanel.setBackground (Color.lightGray);
   	
      JSeparator x = new JSeparator(JSeparator.HORIZONTAL);
      x.setPreferredSize(new Dimension(100, 9));
      guiPanel.add(x, BorderLayout.CENTER);
     
      guiPanel.add(centerPanel, BorderLayout.NORTH);
      guiPanel.add(southPanel, BorderLayout.SOUTH);
   
      add (guiPanel);
      pack();    
      setVisible (true);    
   
   }//end constructor()
	
	
	
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	 *  This method creates a result panel called southPanel
	 *  to be added to south of big panel, guiPanel
	 */

   private void buildSouthPanel()
   {
      clicked = new JLabel ("   ");
      resultLabel = new JLabel ("   ");       	
      southPanel = new JPanel();
      southPanel.setBackground (Color.WHITE);
      southPanel.setLayout (new GridLayout(2,1));
      southPanel.add(clicked);
      southPanel.add(resultLabel);	
   }

   
	
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	 *  This method creates a panel called centerPanel which adds various components 
	 *  such as prompt, input field, and buttons to be displayed in the center. 
	 *  It is to be added to center of big panel, guiPanel
	 */

   private void buildCenterPanel()
   {  
      prompt = new JLabel ("Enter Temperature and Click Conversion Button");      
   	
      CFfield = new JTextField("0", 10); 
      fieldPanel = new JPanel();
      fieldPanel.add (CFfield);
   			
      celsius = new JButton ("Celsius to Fahrenheit");
      fahrenheit = new JButton ("Fahrenheit to Celsius");
   			
      CelToFahrenListener listener = new CelToFahrenListener( );
      celsius.addActionListener ((ActionListener) listener);
   	
      fahrenheit.addActionListener (new FahrenToCelListner ()); 
   			  
      celPanel = new JPanel();    
      fahrenPanel = new JPanel();
      celPanel.add (celsius);
      fahrenPanel.add (fahrenheit);
      centerPanel = new JPanel();
      centerPanel.setLayout (new GridLayout(2,2));
      centerPanel.add(prompt);
      centerPanel.add(fieldPanel);
      centerPanel.add (celPanel);
      centerPanel.add (fahrenPanel);
   }
   



	 //~~~~~~~~~~~~~~~~~~~~~~CLASSES FOR MAKING BUTTONS HOT~~~~~~~~~~~~~~~~
	 
	 /*  This class listens to when celsius to fahrenheit button is clicked and 
	  *  converts celsius temperature to fahrenheit temperature and displays it.
	  */

	 
   private class CelToFahrenListener implements ActionListener
   {
      public void actionPerformed (ActionEvent e)
      {
         DecimalFormat oneDecimal = new DecimalFormat ("0.0"); 			
      
         clicked.setText ("           You Clicked Celsius to Fahrenheit Button");
         clicked.setForeground(Color.red);
         String str = CFfield.getText();
         celTemperature = Double.parseDouble(str);
         fahrenTemperature = 9.0/5.0 * celTemperature + 32;
         resultLabel.setText("         " + celTemperature + " Celsius = "  
                                            + oneDecimal.format(fahrenTemperature)
                                            + " Fahrenheit ");
      }
   }//end CelToFahrenListener class
	
	
	
	/*  This inner class listens to when fahrenheit to celsius  button is clicked and 
	 *  converts celsius temperature to fahrenheit temperature and displays it.
	 */
   private class FahrenToCelListner implements ActionListener
   {
      public void actionPerformed (ActionEvent e)
      {
         DecimalFormat oneDecimal = new DecimalFormat ("0.0");
         clicked.setText ("           You Clicked Fahrenheit to Celsius Button");
         clicked.setForeground(Color.blue);
         String str = CFfield.getText();	
         fahrenTemperature = Double.parseDouble(str);
         celTemperature = 5.0/9.0 * (fahrenTemperature - 32);
         resultLabel.setText("         " + fahrenTemperature + " Fahrenheit = " 
                                            + oneDecimal.format(celTemperature)
                                            + " Celsius");
      }
   }// end FahrenListner
	

	

   /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	 *  main method
	 */
   public static void main(String[] arg)
   {
      SeparatorDriver checkRadioButtons = new SeparatorDriver();
   	
   } //end main()

}
