import java.awt.*;
import javax.swing.*;
import java.text.NumberFormat;

        // A simple display of a pair of double values.
        // Used as a visualization for the Temperature and Pressure
        // examples.
class ValueDisplay extends JPanel {
    public ValueDisplay(){
        setBackground(Color.yellow);
        final int width = 120, height = 40;
        // setPreferredSize(new Dimension(width,height));
        add(getLabel());
        getFrame().addComponent(this);
    }

    public void showTemperature(double value){
        setTemperatureString(getFormatter().format(value));
        showStrings();
    }

    public void showPressure(double value){
        setPressureString(getFormatter().format(value));
        showStrings();
    }

    private void showStrings(){
        getLabel().setText(getTemperatureString()+"   "+getPressureString());
        delay();
    }

    private void delay(){
        // Notify the parent frame of a change of state.
        getParent().validate();
        final int sleepTime = 300;
        try{
            Thread.sleep(sleepTime);
        }
        catch(Exception e){
        }
    }

    private String getTemperatureString(){
        return temperatureString;
    }

    private void setTemperatureString(String s){
        temperatureString = s;
    }

    private String getPressureString(){
        return pressureString;
    }

    private void setPressureString(String s){
        pressureString = s;
    }

    private NumberFormat getFormatter(){
        return formatter;
    }

    private JLabel getLabel(){
        return label;
    }

    private ComponentFrame getFrame(){
        return frame;
    }

    private static final int textSize = 20;
    private static final Font font = new Font("SansSerif",Font.BOLD,textSize);
    private String temperatureString = "", pressureString = "";
    // Provide a reasonable amount of space for two values.
    private JLabel label = new JLabel("       ",JLabel.CENTER);
    {
        label.setFont(font);
    }
    // The frame shared by all devices displays.
    private static final ComponentFrame frame =
                        new ComponentFrame("Temperature and Pressure");
    // Initialised by the constructor of the first instance.
    private static NumberFormat formatter;
    static{
        formatter = NumberFormat.getNumberInstance();
        // Print to two decimal places.
        formatter.setMaximumFractionDigits(2);
        // Don't group by thousands.
        formatter.setGroupingUsed(false);
    }
}
