FileDocCategorySizeDatePackage
FloatControlComponent.javaAPI DocExample2338Mon Jan 09 11:02:00 GMT 2006None

FloatControlComponent

public class FloatControlComponent extends JPanel implements ChangeListener

Fields Summary
FloatControl
control
JSlider
slider
float
min
float
max
float
range
static final int
SLIDER_MIN
static final int
SLIDER_MAX
static final float
SLIDER_RANGE
Constructors Summary
public FloatControlComponent(FloatControl c)


        
        control = c;
        /*
        System.out.println ("Control " + control +
                            ": min = " +  control.getMinimum() +
                            ", max = " +  control.getMaximum());
        */
        min = c.getMinimum();
        max = c.getMaximum();
        range = max - min;
        add (new JLabel (control.getMinLabel()));
        slider = new JSlider (SLIDER_MIN, SLIDER_MAX);
        slider.addChangeListener (this);
        setSliderFromControl();
        add (slider);
        add (new JLabel (control.getMaxLabel()));
    
Methods Summary
private voidsetControlFromSlider()

        // figure out slider percentage
        float sliderPercentage =
            (float) slider.getValue() / SLIDER_RANGE;
        // System.out.println ("new slider value = " + slider.getValue() +
        //                    ", percentage= " + sliderPercentage);
        // figure out value for that percentage of range
        float rangeOffset = sliderPercentage * range;
        float newValue = rangeOffset + min;
        // System.out.println ("rangeOffset = " + rangeOffset +
        //                     ", newValue = " + newValue);
        control.setValue (newValue);
    
private voidsetSliderFromControl()

        // figure out value as percent of range
        float offsetValue = control.getValue() - min;
        // System.out.println ("offsetValue = " + offsetValue);
        float percent = 0.0f;
        if (range != 0.0)
            percent = offsetValue / range;
        // System.out.println ("percent = " + offsetValue/range);
        // apply that to SLIDER_RANGE
        int sliderValue = (int) (percent * SLIDER_RANGE);
        // System.out.println ("sliderValue = " + sliderValue);
        slider.setValue (sliderValue);
    
public voidstateChanged(javax.swing.event.ChangeEvent e)

        setControlFromSlider();