/**
* Class: CSC-123
*
* Project 1 part 2: Temp Convert
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*; //Scanner
public class Project1Part2 implements ActionListener
{
JFrame converterFrame;
JPanel converterPanel;
JTextField tempCelsius;
JLabel celsiusLabel, fahrenheitLabel, kelvinLabel, rankineLabel;
JButton convertTemp;
public Project1Part2()
{
//Create and set up the window.
converterFrame = new JFrame("Convert Celsius to Fahrenheit, Kelvin and Rankine");
converterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
converterFrame.setSize(new Dimension(120, 140));
//Create and set up the panel.
converterPanel = new JPanel(new GridLayout(2, 4));
//Add the widgets.
addWidgets();
//Set the default button.
converterFrame.getRootPane().setDefaultButton(convertTemp);
//Add the panel to the window.
converterFrame.getContentPane().add(converterPanel, BorderLayout.CENTER);
//Display the window.
converterFrame.pack();
converterFrame.setVisible(true);
}
/**
* Create and add the widgets.
*/
private void addWidgets()
{
//Create widgets.
tempCelsius = new JTextField(2);
celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT);
convertTemp = new JButton("Convert");
fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);
kelvinLabel = new JLabel("Kelvin", SwingConstants.LEFT);
rankineLabel = new JLabel("Rankine", SwingConstants.LEFT);
//Listen to events from the Convert button.
convertTemp.addActionListener(this);
//Add the widgets to the container.
converterPanel.add(tempCelsius);
converterPanel.add(celsiusLabel);
converterPanel.add(convertTemp);
converterPanel.add(fahrenheitLabel);
converterPanel.add(kelvinLabel);
converterPanel.add(rankineLabel);
celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
rankineLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
kelvinLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
}
public void actionPerformed(ActionEvent event)
{
//Parse degrees Celsius as a double and convert to Fahrenheit Rankine and Kelvin.
int tempFahr = (int)((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32);
fahrenheitLabel.setText(tempFahr + " Fahrenheit");
int tempRankine = (int)(((((Double.parseDouble(tempCelsius.getText())) * 1.8)+32)+459.67));
rankineLabel.setText(tempRankine + " Rankine");
int tempKelvin = (int)((Double.parseDouble(tempCelsius.getText())) + 273.15);
kelvinLabel.setText(tempKelvin + " Kelvin");
}
/**
* Create the GUI and show it.
*/
private static void createAndShowGUI()
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
tempConvert converter = new tempConvert();
}
public static void doCli()
{
Scanner keyboard = new Scanner(System.in); //Creates Scanner object for input.
double output;
int celsuis;
System.out.print("Enter Temp in Degrees Celsuis: ");
celsuis = keyboard.nextInt();
output = (celsuis * 1.8 + 32);
System.out.println("Farenheit: " + output); // Output for Farenheit Temp Convert.
output = (((celsuis * 1.8)+32)+459.67);
System.out.println("Rankine: " + output); // Output for Rankine Temp Convert.
output = (celsuis + 273.15);
System.out.println("Kelvin: " + output); // Output for Kelvin Temp Convert.
}
public static void doGuiYesNo()
{
//Create the frame
String title = "Frame Title";
JFrame frame = new JFrame(title);
// Create a component to add to the frame
JComponent comp = new JTextArea();
// Add the component to the frame's content pane;
// by default, the content pane has a border layout
frame.getContentPane().add(comp, BorderLayout.CENTER);
// Modal dialog with OK button
String message = "Do you want to convert Temps with GUI?";
JOptionPane.showMessageDialog(frame, "You are now going to choose either gui or CLI");
// Modal dialog with yes/no button
int answer = JOptionPane.showConfirmDialog(frame, message);
if (answer == JOptionPane.YES_OPTION)
{
createAndShowGUI(); // User clicked YES.
}
else if (answer == JOptionPane.NO_OPTION)
doCli(); // User clicked NO.
}
public static void main(String[] args)
{
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
doGuiYesNo();
}
});
}
}