/** * @file JSlider.java * @author R Daniel Bergeron * @version 2.0 * @see Draw3d.java *

* description Build a class that supports a Scrollbar with a label (and * maybe someday a label that shows the current value) and an * arbitrary range of floating point values and some other conveniences. * This class also provides a simple version of an Adjustment Listener * for float values. * The user of the slider can create an instance of a FloatListener and * ask that object to be called (through it's valueChanged method) * whenever the slider value is changed. This is done by passing the * Float Listener to the addFloatListener method of Slider. * *

* modified by Robert S Laramee * modification date Thursday 10 Dec 98 * Sliders were replaced with JSliders (Swing Components) * Slider --> JSlider * Panel --> JPanel * Scrollbar --> JScrollBar * Label --> JLabel * List --> JList */ import java.awt.*; import java.awt.event.*; //import list.List; import javax.swing.*; public class JSlider extends JPanel { protected final int MAXVAL = 500; // max scrollbar value protected JPanel spanel; protected JScrollBar sbar; protected JLabel titleLabel; protected JLabel valueLabel; protected List listenerList; protected float minValue; protected float maxValue; protected float curValue; protected boolean curValueSet = false; //hack for mouse problem //---------- Slider( title, orient ) ------------------ public JSlider( String title, int orient ) { this( title, orient, 0, 1, (float) 0.5 ); } //---------- Slider( title, orient, min, max, init ) ------------------ public JSlider( String title, int orient, float min, float max, float init ) { GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbcLabel = new GridBagConstraints(); GridBagConstraints gbcSbar = new GridBagConstraints(); JLabel label = new JLabel( title ); setLayout ( gbl ); gbcLabel.fill = GridBagConstraints.NONE; gbcLabel.gridx = 0; gbcLabel.gridy = 0; gbcLabel.gridwidth = 1; gbcLabel.gridheight = 1; curValue = init; minValue = min; maxValue = max; int initval = toIntValue( init ); listenerList = new List(); if ( orient == JScrollBar.HORIZONTAL ) { gbcLabel.anchor = GridBagConstraints.EAST; gbcSbar.fill = GridBagConstraints.HORIZONTAL; gbcSbar.gridx = 1 ; gbcSbar.gridy = 0 ; gbcSbar.weightx = 1000 ; gbcSbar.gridwidth = 100 ; gbcSbar.gridheight = 1 ; } else { gbcLabel.anchor = GridBagConstraints.WEST; gbcSbar.fill = GridBagConstraints.VERTICAL; gbcSbar.fill = GridBagConstraints.BOTH; gbcSbar.gridx = 0 ; gbcSbar.gridy = 1 ; gbcSbar.weighty = 1000 ; gbcSbar.gridwidth = 1 ; gbcSbar.gridheight = 100 ; } gbl.setConstraints( label, gbcLabel ); add( label ); sbar = new JScrollBar( orient ); sbar.setValues( initval, 5 /* visible */, 0, MAXVAL+5 ); sbar.setUnitIncrement( 10 ); sbar.setBlockIncrement( 50 ); sbar.addMouseListener( new MouseAdapter( ) // some peers don't seem to generated button up events?? { public void mouseReleased( MouseEvent e) { resetSlider( e ); } public void mouseExited( MouseEvent e) { resetSlider( e ); } } ); gbl.setConstraints( sbar, gbcSbar ); add ( sbar ); sbar.addAdjustmentListener( new FloatAL( 0, MAXVAL, min, max ) ); } //---------- toIntValue( float ) ------------------ public int toIntValue( float val ) { return (int)( (val-minValue)/(maxValue-minValue) * (float)MAXVAL); } //---------- setValue( float ) ------------------ public void setValue( float newValue ) { if ( minValue < maxValue ) { newValue = Math.max( newValue, minValue ); newValue = Math.min( newValue, maxValue ); } else { newValue = Math.min( newValue, minValue ); newValue = Math.max( newValue, maxValue ); } curValue = newValue; sbar.setValue( toIntValue( curValue ) ); } //---------- getValue( ) ------------------ public float getValue( float newVal ) { return curValue; } //---------- resetSlider( event ) ------------------ public void resetSlider( MouseEvent e ) { if ( ! curValueSet )// hack because mouseReleased not called return; curValueSet = false; listenerList.reset(); for (int i=0; i < listenerList.length(); i++ ) ((FloatListener) listenerList.next()).reset( ); } //---------- valueChanged( float ) ------------------ public void valueChanged( float newVal ) { curValue = newVal; curValueSet = true; // hack because mouseReleased not called listenerList.reset(); for (int i=0; i < listenerList.length(); i++ ) ((FloatListener) listenerList.next()).valueChanged( curValue ); } //---------- addFloatListener( ) ------------------ public void addFloatListener( FloatListener listener) { listenerList.add( listener ); } //=================== internal class FloatAL ======================== class FloatAL implements AdjustmentListener { private float sbarRange; // sbar max- min value private float floatMin; // desired min value private float floatMax; // desired max value private float floatRange; // floatMax - floatMin private float floatVal; // desired value private float scaleFactor; // desired value public FloatAL( int min, int max, float fmin, float fmax) { sbarRange = (float)( max - min ); floatMin = fmin; floatMax = fmax; scaleFactor = (fmax - fmin) / sbarRange; } public void adjustmentValueChanged( AdjustmentEvent event ) { float val = (float) event.getValue(); floatVal = val * scaleFactor + floatMin; valueChanged( floatVal ); } } } // end Slider