Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have to write an applet that displays a thermometer. The user should be able to control the temperature with a slider component. When the slider is moved, the thermometer should show a corresponding temperature.

Problem: I can get the slider component working, but I do not know how to tie in a moving bar into the equation. How do I make a rectangle ( which acts as the thermometer) to move with the slider? I know that I am supposed to use something like  myRect.height = slider.getValue();, but that doesn't help any. I hope this is a quick solution.

Here is the code that a I have so far.

import java.awt.*;
import javax.swing.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class TempConverter extends JFrame
{
private JLabel label1, label2; //Display labels
private JTextField fahrenheitTemp, centigradeTemp;
private JPanel fpanel, cpanel, sliderPanel;
private JSlider slider; //Temperature adjuster
private Container contentPane; //Content pane
/**
*Constructor
*/
public TempConverter()
{
//Set the title
setTitle("Temperature");

//Specify what happens when the
//close button is clicked.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create the labels.
label1 = new JLabel("Fahrenheit: ");
label2 = new JLabel("Centigrade: ");

//Create the read-only text fields.
fahrenheitTemp = new JTextField("0.0", 10);
fahrenheitTemp.setEditable(false);
centigradeTemp = new JTextField("0.0", 10);
centigradeTemp.setEditable(false);

//Create the slider
slider = new JSlider(JSlider.HORIZONTAL,0,100,0);
slider.setMajorTickSpacing(20);
slider.setMinorTickSpacing(5);
slider.setPaintTicks(true);
slider.setPaintLabels(true);
slider.addChangeListener(new SliderListener());

//Create panels and place the components in them.
fpanel = new JPanel();
fpanel.add(label1);
fpanel.add(fahrenheitTemp);
cpanel = new JPanel();
cpanel.add(label2);
cpanel.add(centigradeTemp);
sliderPanel = new JPanel();
sliderPanel.add(slider);

//Get the content pane.
contentPane = getContentPane();
contentPane.setLayout(new GridLayout(3,1));
contentPane.add(fpanel);
//contentPane.add(cpanel);
contentPane.add(sliderPanel);

//Pack and display the frame.
pack();
setVisible(true);
}
/**
*Private inner clas to handle the change events
*that are generated when the slider is moved.
*/
private class SliderListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
double fahrenheit, centigrade;
DecimalFormat fmt = new DecimalFormat("0.0");

//Get the slider value
centigrade = slider.getValue();

//Convert the value to Fahrenheit.
//fahrenheit = (9.0 / 5.0) * centigrade + 32.0;
fahrenheit = slider.getValue();

//Store the centigrade temp in its display field.
centigradeTemp.setText(Double.toString(centigrade));

//Store the Fahrenheit temp in its display field.
fahrenheitTemp.setText(fmt.format(fahrenheit));
}
}

/**
*This program creaates an instance of the TempConverter
*class, which displays a window with a slider.
*/

public static void main(String[] args)
{
// TODO, add your application code
TempConverter tc = new TempConverter();
System.out.println("Temperature Program.");
}
}

Posted

1 solution

Adjust the height of the rectangle in relation to the maximum range of the slider value, for the available space the thermometer has to work in. remember to flip it round though, as your screen co-ords will be back to front for the thermometer min -max

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900