FileDocCategorySizeDatePackage
Dial.javaAPI DocExample2621Fri Jun 20 01:11:16 BST 1997None

Dial

public class Dial extends Component

Fields Summary
int
minValue
int
value
int
maxValue
int
radius
Vector
dialListeners
Constructors Summary
public Dial()


	  
		enableEvents( AWTEvent.MOUSE_MOTION_EVENT_MASK );
	
public Dial(int maxValue)

		this();
		this.maxValue = maxValue; 
	
Methods Summary
public voidaddDialListener(DialListener listener)

		if ( dialListeners == null )
			dialListeners = new Vector();
		dialListeners.addElement( listener );
	
private voiddraw3DCircle(java.awt.Graphics g, int x, int y, int radius, boolean raised)

		g.setColor( raised ? Color.white : Color.black );
		g.drawArc( x, y, radius*2, radius*2, 45, 180);
		g.setColor( raised ? Color.black : Color.white);
		g.drawArc( x, y, radius*2, radius*2, 225, 180);
	
private voidfireEvent()

		if ( dialListeners == null )
			return;

		DialEvent event = new DialEvent(this, value);
		for (Enumeration e = dialListeners.elements(); e.hasMoreElements(); ) 
			((DialListener)e.nextElement()).dialAdjusted( event );
	
public intgetMaximum()

 return maxValue; 
public intgetMinimum()

 return minValue; 
public java.awt.DimensiongetPreferredSize()

 
		return new Dimension( 100, 100 );
	
public intgetValue()

 return value; 
public voidpaint(java.awt.Graphics g)

		int tick = 10;
		radius = getSize().width/2 - tick;
		g.drawLine(radius*2+tick/2, radius, radius*2+tick, radius); // the tick
		draw3DCircle( g, 0, 0, radius, true );
		draw3DCircle( g, 1, 1, radius-1, true );
		int knobRadius = radius/7;
		double th = value*(2*Math.PI)/(maxValue-minValue);
		int x = (int)(Math.cos(th)*(radius-knobRadius*3)), 
			y = (int)(Math.sin(th)*(radius-knobRadius*3));
		draw3DCircle( g, x+radius-knobRadius, y+radius-knobRadius, knobRadius, false );
	
public voidprocessEvent(java.awt.AWTEvent e)

		if ( e.getID() == MouseEvent.MOUSE_DRAGGED ) {
			int y=((MouseEvent)e).getY();
			int x=((MouseEvent)e).getX();
			double th = Math.atan( (1.0*y-radius)/(x-radius) );
			int value = ( (int)(th/(2*Math.PI) * (maxValue-minValue)) );
			if ( x < radius ) 
				setValue( value + maxValue/2 );
			else if ( y < radius )
				setValue( value + maxValue );
			else 
				setValue( value );
			fireEvent();
		}
		super.processEvent( e );
	
public voidremoveDialListener(DialListener listener)

		if ( dialListeners != null )
			dialListeners.removeElement( listener );
	
public voidsetMaximum(int maxValue)

 this.maxValue = maxValue; 
public voidsetMinimum(int minValue)

 this.minValue = this.minValue; 
public voidsetValue(int value)

 
		this.value = value; 
		repaint();