Source
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.accessibility.*;
/**
*
* An Applet to convert Celsius to Fahrenheit to Kelvin
* Demo of JSlider class
* @author http://www.whatrain.com/
* @version beta 0.1
* @author Timothy M. Radonich
* @author WhatRain Internet Services
* @see
*/
public class TemperatureSlider
extends Applet
implements ChangeListener,
Runnable
{
// ===========================================
// APPLET METHODS
// ===========================================
/* --Initialization----------------- */
public void init()
{
}
public TemperatureSlider()
{
setBackground( bgColor );
// Horizontal Slider
sliderPanel = new JPanel();
sliderPanel.setBackground( bgColor );
sliderPanel.setLayout(new BoxLayout
(sliderPanel, BoxLayout.Y_AXIS));
sliderPanel.setBorder(new TitledBorder(
new EtchedBorder(),
"Increase Temperature >>>",
TitledBorder.CENTER,
TitledBorder.ABOVE_TOP,
boldFont));
slider = new JSlider( 0, 1000, 10);
slider.getAccessibleContext().setAccessibleName
("Temperature");
slider.getAccessibleContext().setAccessibleDescription
("A Temperature slider");
slider.setBackground( bgColor );
slider.setPaintTicks(true);
slider.setMajorTickSpacing(100);
slider.setMinorTickSpacing(10);
slider.addChangeListener(this);
sliderPanel.add(Box.createRigidArea(new Dimension(475,5)));
sliderPanel.add(slider);
sliderPanel.add(Box.createRigidArea(new Dimension(475,5)));
displayPanel = new JPanel();
displayPanel.setBackground( bgColor );
displayPanel.setLayout(new BoxLayout
(displayPanel, BoxLayout.Y_AXIS));
displayPanel.setBorder(new EtchedBorder());
labelPanel = new JPanel();
labelPanel.setBackground( bgColor );
labelPanel.setLayout(new GridLayout(1,3));
labelPanel.add( fahrenheitDisplay );
labelPanel.add( kelvinDisplay );
labelPanel.add( celsiusDisplay );
displayPanel.add(Box.createRigidArea(new Dimension(475,5)));
displayPanel.add(labelPanel);
displayPanel.add(Box.createRigidArea(new Dimension(475,5)));
setLayout( new BorderLayout());
add("North", sliderPanel);
add("South", displayPanel);
celsiusDisplay.setFont(boldFont) ;
fahrenheitDisplay.setFont(boldFont) ;
kelvinDisplay.setFont(boldFont) ;
setSize(500, 105);
setVisible(true);
}
/* --Starting and Stopping---------- */
public void start()
{
if ( theThread == null )
{
theThread = new Thread();
theThread.start();
}
}
public void stop()
{
if ( theThread != null )
{
//theThread.destroy();
theThread = null;
}
}
// ===========================================
// IMPLEMENT RUNNABLE
// ===========================================
public void run()
{
while ( true )
{
try { Thread.sleep( 1000 ); }
catch ( InterruptedException e ) {}
}
}
// ===========================================
// implements ChangeListener
// ===========================================
public void stateChanged(ChangeEvent e)
{
celsiusDisplay.setText
("Celsius: " + DoubleFormat.toString
((slider.getValue() - 273.15), -4));
fahrenheitDisplay.setText
(" Fahrenheit: " + DoubleFormat.toString
(((slider.getValue
() - 273.15) * 1.8 + 32), -4));
kelvinDisplay.setText
("Kelvin: " + (slider.getValue()));
}
// ===========================================
// CLASS ATTRIBUTES
// ===========================================
Font boldFont = new Font("Hevetica", Font.BOLD, 16 ) ;
private Thread theThread = null;
private JSlider slider;
private JPanel sliderPanel;
private JPanel displayPanel;
private JPanel labelPanel;
private JLabel celsiusDisplay =
new JLabel("Celsius: -273.15",JLabel.CENTER);
private JLabel fahrenheitDisplay =
new JLabel(" Fahrenheit: -459.67",JLabel.CENTER);
private JLabel kelvinDisplay =
new JLabel("Kelvin: 0",JLabel.CENTER);
GridBagLayout gbLayout = new GridBagLayout () ;
GridBagConstraints gbConstraints = new GridBagConstraints();
Color bgColor = new Color(152,215,225 ) ;//blue
}
/**
*
*
*
* copyright(c)1997 Jamsa Press
*
*
*
*
* @version 1
* @author 1001 Java Programmer's Tips
*
* @since jdk1.0
*/
class DoubleFormat {
public static String toString(double inValue, int precision) {
return toString(inValue, precision, false);
}
public static String toString (double inValue, int precision,
boolean use_comma)
{
boolean trailing_zero;
double absval= Math.abs(inValue); // get positive portion
if (precision < 0)
{
precision = -precision;
trailing_zero = false;
}
else
trailing_zero = true;
String signStr = "";
if (inValue < 0)
signStr= "-";
// get integer part
long intDigit = (long) Math.floor(absval);
String intDigitStr = String.valueOf(intDigit);
if (use_comma)
{
int intDigitStrLen= intDigitStr.length();
int dig_index= (intDigitStrLen - 1) % 3;
dig_index++;
String intCommaDigitStr = intDigitStr.substring(0,
dig_index);
while (dig_index < intDigitStrLen)
{
intCommaDigitStr += "," +
intDigitStr.substring(dig_index, dig_index+3);
dig_index+= 3;
}
intDigitStr= intCommaDigitStr;
}
String precDigitStr= "";
long precDigit= Math.round((absval - intDigit) *
Math.pow(10.0, precision));
precDigitStr= String.valueOf(precDigit);
// pad zeros between decimal and precision digits
String zeroFilling= "";
for (int i= 0; i < precision-precDigitStr.length(); i++)
zeroFilling += "0";
precDigitStr= zeroFilling + precDigitStr;
if (!trailing_zero)
{
int lastZero;
for (lastZero = precDigitStr.length() - 1;
lastZero >= 0; lastZero--)
if (precDigitStr.charAt(lastZero)!= '0')
break;
precDigitStr= precDigitStr.substring(0, lastZero + 1);
}
if (precDigitStr.equals(""))
return signStr + intDigitStr;
else
return signStr + intDigitStr + "." + precDigitStr;
}
}
|