FileDocCategorySizeDatePackage
Slider.javaAPI DocJMF 2.1.1e5890Mon May 12 12:20:52 BST 2003com.sun.media.ui

Slider

public class Slider extends BasicComp implements MouseListener, MouseMotionListener

Fields Summary
Image
imageGrabber
Image
imageGrabberX
Image
imageGrabberDown
Graphics
paintG
boolean
grabbed
boolean
entered
int
grabberPosition
int
leftBorder
int
rightBorder
int
sliderWidth
int
width
int
height
int
displayPercent
float[]
detents
Dimension
dimension
float
lower
float
upper
float
range
float
value
boolean
dragging
boolean
grabberVisible
Constructors Summary
public Slider()


      
	this(null, null);
    
public Slider(float[] detents)

	this(detents, null);
    
public Slider(float[] detents, Color background)

	super("Slider");
	imageGrabber = fetchImage("grabber.gif");
	imageGrabberDown = fetchImage("grabber-pressed.gif");
	imageGrabberX = fetchImage("grabber-disabled.gif");

	this.detents = detents;
	if (background != null) {
	    setBackground(background);
	}
	width = 115;
	height = 18;
	displayPercent = 100;
	dimension = new Dimension(width, height);
	sliderWidth = width - leftBorder - rightBorder;
	setSize(width, height);
	setVisible(true);
	grabbed = false;
	entered = false;
	addMouseListener( this );
	addMouseMotionListener( this );
    
Methods Summary
public java.awt.PointgetPosition()

	return new Point(grabberPosition + leftBorder, 10);
    
public java.awt.DimensiongetPreferredSize()

	return dimension;
    
public booleanisGrabberVisible()

	return grabberVisible;
    
private intlimitGrabber(int mousex)

	int x = mousex - leftBorder;
	if (x < 0)
	    x = 0;
	else if (x > sliderWidth)
	    x = sliderWidth;
	return x;
    
public voidmouseClicked(java.awt.event.MouseEvent me)

 
public voidmouseDragged(java.awt.event.MouseEvent me)

	int modifier = me.getModifiers();
	if ((modifier & InputEvent.BUTTON2_MASK) == 0 &&
	    (modifier & InputEvent.BUTTON3_MASK) == 0 ) {
	    if (isEnabled()) {
		dragging = true;
		grabberPosition = limitGrabber(me.getX());
		seek();
	    }
	}
    
public voidmouseEntered(java.awt.event.MouseEvent me)

	entered = true;
	repaint();
    
public voidmouseExited(java.awt.event.MouseEvent me)

	entered = false;
	repaint();
    
public voidmouseMoved(java.awt.event.MouseEvent me)

 
public voidmousePressed(java.awt.event.MouseEvent me)
MouseListener methods

	int modifier = me.getModifiers();
	if ((modifier & InputEvent.BUTTON2_MASK) == 0 &&
	    (modifier & InputEvent.BUTTON3_MASK) == 0 ) {
	    if (isEnabled()) {
		dragging = false;
		grabbed = true;
		grabberPosition = limitGrabber(me.getX());
		seek();
	    }
	}
    
public voidmouseReleased(java.awt.event.MouseEvent me)

	int modifier = me.getModifiers();
	if ((modifier & InputEvent.BUTTON2_MASK) == 0 &&
	    (modifier & InputEvent.BUTTON3_MASK) == 0 ) {
	    if (isEnabled()) {
		dragging = false;
		grabbed = false;
		repaint();
	    }
	}
    
public voidpaint(java.awt.Graphics g)

	Dimension size = getSize();
	int x;
	int y = (size.height / 2) - 2;

	paintG = g;
	
	int grabberX = grabberPosition + leftBorder - 5;

	// Draw the trough
        g.setColor( getBackground().darker() );
        g.drawRect (leftBorder, y, sliderWidth, 3);
        g.setColor(getBackground());
        g.draw3DRect(leftBorder, y, (int)(sliderWidth * displayPercent / 100), 3, false);

	// Draw the marks for detents
	if (detents != null && detents.length != 0) {
	    paintG.setColor(Color.black);
	    for (int i = 0; i < detents.length; i++) {
		x = leftBorder + (int)(detents[i] * sliderWidth / range);
		paintG.drawLine(x, 12, x, 15);
	    }
	}

	// Draw the grabber
	if (grabberVisible) {
	    Image image;
	    if (isEnabled()) {
		if (grabbed || entered)
		    image = imageGrabberDown;
		else
		    image = imageGrabber;
	    } else {
		image = imageGrabberX;
	    }
	    paintG.drawImage(image, grabberX, 4, this);
	}
    
private voidseek()

	if (control != null && control instanceof NumericControl) {
	    NumericControl nc = (NumericControl) control;
	    float lower = nc.getLowerLimit();
	    float upper = nc.getUpperLimit();
	    float value = ((float)grabberPosition/sliderWidth) * (upper - lower)
		+ lower;
	    if (detents != null && detents.length > 0 && dragging) {
		float tolerance = (upper - lower) * 0.05f;
		for (int i = 0; i < detents.length; i++) {
		    if (Math.abs(detents[i] - value) <= tolerance)
			value = detents[i];
		}
	    }

	    float result = nc.setValue(value);
	    if (value != result)
		setSliderPosition(result - lower, upper - lower);
	}
	repaint();
    
public voidsetDisplayPercent(int percent)

	if (percent != displayPercent) {
	    displayPercent = percent;
	    if (displayPercent > 100) {
		displayPercent = 100;
	    } else if (displayPercent < 0) {
		displayPercent = 0;
	    }
	    repaint();
	}
    
public voidsetEnabled(boolean state)

	super.setEnabled(state);
	repaint();
    
public voidsetGrabberVisible(boolean visible)

	if (grabberVisible != visible) {
	    grabberVisible = visible;
	    repaint();
	}
    
public voidsetSize(int width, int height)
Component methods

	super.setSize(width, height);
	paintG = null;
	repaint();
    
private voidsetSliderPosition(float value, float range)

	grabberPosition = (int) ( value / range * sliderWidth);