//
// temps.java
//
//
// Created by Herbert J. Bernstein on 10/2/11.
// Copyright 2011 Herbert J. Bernstein. All rights reserved.
//
// A demonstration applet for temperature conversions
//
// This will be set up to run both as an applet and
// as an application
//
// The basic structure is based on the tutorial by
// William Wilson at
//
// http://www.dreamincode.net/forums/topic/28410-application-to-japplet-and-reverse/
// with some useful tidbits from
// http://leepoint.net/notes-java/deployment/applications_and_applets/70applets.html
//
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class temps extends JApplet
{
appPanel apppanel;
JTextField tempF;
JTextField tempC;
double FahrenheitTemp;
double CentigradeTemp;
public temps() //default constructor
{
System.out.println("creating temperature conversion object");
}
public void init() // to call on load (once)
{
// System.out.println("initializing temperature conversion applet");
Container outerpane=getContentPane(); // The ContentPane is the
// place where we add all the
// components of the applet
apppanel = new appPanel(); // starting with a panel
// that has within it eveything
// we will draw
outerpane.add(apppanel);
}
public void start() // to call after load
{
// System.out.println("temperature conversion applet starting");
}
public void stop() // to call at the end
{
// System.out.println("temperature conversion applet stopping");
}
public void destroy() // to call on exit
{
}
public class appPanel extends JPanel // this is where we really do
// the drawing for the applet
{
public appPanel() //applet panel object
{
// System.out.println("temperature conversion panel created");
this.setBackground(Color.yellow);
this.setLayout(new FlowLayout());
this.add(new JLabel("Enter Fahrenheit Temp here:"));
tempF = new JTextField("?",20);
this.add(tempF);
this.add(new JLabel("Enter Centigrade Temp here:"));
tempC = new JTextField("?",20);
this.add(tempC);
// When changes are made to tempF compute tempC
tempF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String FahrenheitText = tempF.getText();
try {
FahrenheitTemp = Double.parseDouble(FahrenheitText);
CentigradeTemp = (FahrenheitTemp-32.)*5./9.;
tempC.setText(Double.toString(CentigradeTemp));
}catch (NumberFormatException nfe) {
tempF.setText("Please enter a valid number: " + nfe.getMessage());
}
}
});
// When changes are made to tempC compute tempF
tempC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
String CentigradeText = tempC.getText();
try {
CentigradeTemp = Double.parseDouble(CentigradeText);
FahrenheitTemp = 32.+ CentigradeTemp*9./5.;
tempF.setText(Double.toString(FahrenheitTemp));
}catch (NumberFormatException nfe) {
tempC.setText("Please enter a valid number: " + nfe.getMessage());
}
}
});
}
}
// This is the main program to convert the applet to
// an application
public static void main(String args[]) {
JFrame DrawingFrame = new JFrame("Temperature Conversions"); //Make the frame
DrawingFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Be sure is closes on exit
temps applet = new temps(); //Make an applet instance
DrawingFrame.setContentPane(applet); //Give this frame to the applet
applet.init(); //Initialize the applet
DrawingFrame.setSize(350,200); //Set the frame size
DrawingFrame.setVisible(true); //Make the frame visible
applet.start(); //If the applet has start logic
//run it
}
}