Search This Blog

Monday, February 6

!ANSWER! Assignment 4

import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Scanner;
import javax.swing.*;


public class BajrovicAssignment4 {
	public static void main(String[] args) {

		// Swing is not thread safe.
		// UI code should run in a event dispatching thread.
		EventQueue.invokeLater(
		/**
		 * An object of an anonymous inner class that implements Runnable
		 * interface is sent to event dispatching thread which executes the run
		 * method.
		 */
		new Runnable() {

			@Override
			public void run() {

				// construct a JFrame object
				FrameWithLabel frame = new FrameWithLabel();
				// set the new JFrame to be visible
				frame.setVisible(true);
				// exit program when the JFrame is closed
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}
		});
	}
}

	class FrameWithLabel extends JFrame {
		private Box tempConv;
		private JLabel label;
		private JPanel panelButtons;
		private JRadioButton radioTo;
		private JRadioButton radioFrom;
		private JLabel textLabel;
		private JLabel textLabel2;
		private static JLabel converted;
		static JTextField temperature;
		private JPanel gridPanel;
		private static String identifier;
		private static String convertedDegree;
		private JButton convertButton;
		private JPanel chensterPanel;
		private JPanel lowLevelPanel;
		
		public FrameWithLabel() {
			//sets name & size of window
			setTitle("BajrovicAssignment4");
			setSize(500, 200);
			
			//label box
			tempConv = Box.createHorizontalBox();
			label = new JLabel("Temperature Convertor");
			tempConv.add(label, BorderLayout.NORTH);
			
			//radio buttons 
			panelButtons = new JPanel();
			radioTo = new JRadioButton("To Celsius");
			radioFrom = new JRadioButton("From Celsius");
			panelButtons.setBorder(BorderFactory.createTitledBorder("Options"));
			panelButtons.add(radioTo);
			panelButtons.add(radioFrom);
			radioTo.setSelected(true);
			
			//limit of 1 radio button selected
			ButtonGroup buttonGroup = new ButtonGroup();
			buttonGroup.add(radioFrom);
			buttonGroup.add(radioTo);
			
			//associate events with each button
			radioTo.addItemListener(new Buttons ());
			radioFrom.addItemListener(new Buttons ());
			
			//text field
			textLabel = new JLabel("Input a Fahrenheit Value", SwingConstants.RIGHT);
			textLabel2 = new JLabel("Celsius Value", SwingConstants.RIGHT);
			textLabel2.setHorizontalAlignment(JLabel.RIGHT);
			converted = new JLabel ();
			converted.setHorizontalAlignment(SwingConstants.LEFT);
			temperature = new JTextField();
			
			temperature.addActionListener(new Text());
			
			//Convert Button
			lowLevelPanel = new JPanel();
			convertButton = new JButton("Convert");
			lowLevelPanel.add(convertButton);
			
			convertButton.addActionListener(new Text());
			
			//adding all components to all panels
			chensterPanel = new JPanel();
			gridPanel = new JPanel();
			chensterPanel.setLayout(new GridLayout(2, 1));
		   	gridPanel.setLayout(new GridLayout(2, 2,20,5));
		   	gridPanel.add(textLabel);
		   	gridPanel.add(temperature);
		   	gridPanel.add(textLabel2);
		   	gridPanel.add(converted);
		   	chensterPanel.add(gridPanel);
		   	chensterPanel.add(lowLevelPanel);
			
			// add the panel to the content pane of the JFrame
			Container contentPane = getContentPane();
			contentPane.add(tempConv, BorderLayout.NORTH);
			contentPane.add(panelButtons, BorderLayout.CENTER);
			contentPane.add(chensterPanel, BorderLayout.SOUTH);
			
			//focuses on the text field ONLY WHEN THE WINDOW IS SELECTED
			addWindowFocusListener(new WindowAdapter() {
				@Override
			    public void windowGainedFocus(WindowEvent e) {
			   	 FrameWithLabel.temperature.requestFocus();
			   	 //immediately selects the 'To Celsius' radio button
			   	 identifier = "to";
			    };
			});	
		}
		
		class Buttons implements ItemListener{
			@Override
			public void itemStateChanged(ItemEvent e){
				if(e.getItem() == radioTo){
					//tells the converter function which equation to use
					identifier = "to";
					textLabel.setText("Input a Fahrenheit Value");
					textLabel2.setText("Celsius Value");
					//clears the text field when the user changes the radio button
					temperature.setText(null);					
				}
				if(e.getItem() == radioFrom){
					identifier = "from";
					textLabel.setText("Input a Celsius Value");
					textLabel2.setText("Fahrenheit Value");
					temperature.setText(null);				
				}
			}
		}
		class Text implements ActionListener{
			@Override
			public void actionPerformed(ActionEvent a){
				//calls the converter function if a there is a value in the temperature text field
				converter();
			}
		}
		public static void converter(){
			if (identifier.equals("to")){
				//changes the string in the text field to a double
				double Fahrenheit = Double.parseDouble(temperature.getText());
				//the conversion equation
				double Celsius = (Fahrenheit-32)*5/9;
				//formats the double to 2 rounded decimal places
				DecimalFormat df = new DecimalFormat("0.00");
				//converts the double into a string so it can be stored in a label
				convertedDegree = String.valueOf(df.format(Celsius));
			//stores the calculated value in the label
			converted.setText(convertedDegree);
			}
				if (identifier.equals("from")){
				double Celsius = Double.parseDouble(temperature.getText());
				double Fahrenheit = (Celsius*9/5)+32;
				DecimalFormat df = new DecimalFormat("0.00");
				convertedDegree = String.valueOf(df.format(Fahrenheit));
			converted.setText(convertedDegree);
			}
		}
	}

No comments:

Post a Comment