//---------------------------------------------------------------------- // // Slider // //---------------------------------------------------------------------- import java.awt.*; public class Slider extends Canvas { float _value = 0.0f; float _minValue, _maxValue; int _oldXPos = 0; boolean _clicked = false; Graphics _offscreen; // double buffering Image _offscreenImage; int _leftX, _rightX; // positions of the end of the line Image _bullet; Rectangle _bulletRect; //---------------------------------------------------------------------- // Construct a slider - a bullet on a line. Returns value from 0-1. //---------------------------------------------------------------------- public Slider( float inValue, float inMinValue, float inMaxValue) { // _bullet = Toolkit.getDefaultToolkit().getImage("images/b2.gif"); _bulletRect = new Rectangle( 0, 0, 8, 16 ); _minValue = inMinValue; _maxValue = inMaxValue; setValue( inValue, false ); } public Slider( float inValue ) { this( inValue, 0.0f, 1.0f ); } public Dimension preferredSize() { return new Dimension( 200, 16 ); } //---------------------------------------------------------------------- // Accessors //---------------------------------------------------------------------- public float getValue() { return _value; } public void setValue( float inValue, boolean inIsMoreComing ) // flag if more events are coming soon { // i.e. we're in mid-drag _value = inValue; if (_value > _maxValue) _value = _maxValue; if (_value < _minValue) _value = _minValue; positionBullet(); deliverEvent( new Event( this, Event.ACTION_EVENT, new Boolean(inIsMoreComing) )); } void positionBullet() { float normalVal = _value / (_maxValue - _minValue); // adjust to [0.0 - 1.0] _bulletRect.x = _leftX + (int) (normalVal * (_rightX - _leftX)); invalidate(); repaint(); } //---------------------------------------------------------------------- // paint - only draw things that might have changed //---------------------------------------------------------------------- public void paint( Graphics inGfx ) { if (_offscreenImage == null) resetViewport(); setBackground( getParent().getBackground() ); // _offscreen = _offscreenImage.getGraphics(); _offscreen.setColor( Color.black ); _offscreen.clearRect( _leftX + _oldXPos, 0, _bulletRect.width, _bulletRect.height ); // _offscreen.drawLine( _oldXPos, 8, _oldXPos + _bulletRect.width, 8); // patch line _offscreen.drawLine( _leftX, 8, _rightX + _bulletRect.width, 8); // full line // draw bullet _offscreen.setColor( Color.blue ); _offscreen.fillRoundRect( _bulletRect.x, _bulletRect.y, _bulletRect.width, _bulletRect.height, 8, 8); // _offscreen.drawImage( _bullet, _leftX + _bulletRect.x, 0, this); _oldXPos = _bulletRect.x; inGfx.drawImage( _offscreenImage, 0, 0, null ); // _offscreen.dispose(); } //---------------------------------------------------------------------- // mouseDown - grab bullet //---------------------------------------------------------------------- public boolean mouseDown( Event inEvent, int x, int y) { if (_bulletRect.inside( x, y )) _clicked = true; return true; } public boolean mouseDrag( Event inEvent, int x, int y ) { if (_clicked) { setValue( (_maxValue - _minValue) * (float) (x - _leftX) / (_rightX - _leftX), true ); positionBullet(); } return true; } public boolean mouseUp( Event inEvent, int x, int y ) { if (_clicked) { setValue( (_maxValue - _minValue) * (float) (x - _leftX) / (_rightX - _leftX), false ); positionBullet(); } _clicked = false; return true; } public void resize( Dimension inDim ) { super.resize( inDim ); resetViewport( ); } void resetViewport() { _offscreenImage = createImage( size().width, size().height ); _offscreen = _offscreenImage.getGraphics(); _leftX = 0; _rightX = size().width - _bulletRect.width; positionBullet(); } } // end of Slider