/* 	J_DialControl.java	Title:			Virtual Control Surface	Author:			root	Description:	Copyright © 2001 Jason LamportAll Rights ReservedThe dial controls used in J_InputSettings*/package com.strangelight.v4control;import com.strangelight.*;import com.strangelight.salsa.*;import java.awt.*;import java.awt.event.*;import java.net.*;import java.util.*;public class J_DialControl extends Canvas 	implements 		Adjustable, 		MouseListener, 		MouseMotionListener, 		ActionListener{	public final static Color bg_color = new Color( 0.0f, 0.0f, 0.83f);	protected Vector 	listeners;	protected byte 		value, new_value;		protected static Dimension	size;	protected static final double	RADS_PER_UNIT	=	Math.PI/8;	protected static final double	RAD_OFFSET = Math.PI * 0.50;		protected static final J_Image dial_face = new J_Image("/images/dial_face.JPG");	protected static final J_Image[] dial_arrows = {		new J_Image("/images/dial_0.JPG"),		new J_Image("/images/dial_1.JPG"),		new J_Image("/images/dial_2.JPG"),		new J_Image("/images/dial_3.JPG"),		new J_Image("/images/dial_4.JPG"),		new J_Image("/images/dial_5.JPG"),		new J_Image("/images/dial_6.JPG"),		new J_Image("/images/dial_7.JPG"),		new J_Image("/images/dial_8.JPG"),		new J_Image("/images/dial_9.JPG"),		new J_Image("/images/dial_A.JPG"),		new J_Image("/images/dial_B.JPG"),		new J_Image("/images/dial_C.JPG"),		new J_Image("/images/dial_D.JPG"),		new J_Image("/images/dial_E.JPG"),		new J_Image("/images/dial_F.JPG")	};	public static void init() { 		/*	dummy routine: used to force static initialization				(Java generally doesn't run the static initializers until			the first time the class is used.)		 */ 	}	public J_DialControl() {				listeners = new Vector();				this.size = new Dimension( 			dial_face.get_image().getWidth(null) , 			dial_face.get_image().getHeight(null) 		);		this.setSize( size );		this.setVisible( true );		addMouseListener( this );			}		public J_DialControl(int value) {		this();		setValue(value);	}		public Dimension getPreferredSize() {		return size;	}		public Dimension getMaximumSize() {		return size;	}		public Dimension getMinimumSize() {		return size;	}		public void paint(Graphics g) {		Graphics my_g = this.getGraphics();		my_g.drawImage(dial_face.get_image(), 0, 0, null);		paint_arrow();	}		protected void paint_arrow() {		Graphics my_g = this.getGraphics();			Image arrow = dial_arrows[ new_value ].get_image();		int arrowX = ( size.width - arrow.getWidth(null) ) / 2;		int arrowY = ( size.height - arrow.getHeight(null) ) / 2;		my_g.drawImage( arrow, arrowX, arrowY, null);	}		/*	 *	ActionListener Interface routine:	 */ 	/**	Invoked when an action occurs.	*/	public void actionPerformed(ActionEvent e) { }                 	/*	 *	MouseListener Interface routines:	 */ 	/**	Invoked when the mouse has been clicked on a component.	*/	public void mouseClicked(MouseEvent e) { 		//	new_value = value;		//	mouseDragged(e);		//	D.bug(e);	}	/**	Invoked when the mouse enters a component.	*/	public void mouseEntered(MouseEvent e) { 		//	D.bug(e);	}	/**	Invoked when the mouse exits a component.	*/	public void mouseExited(MouseEvent e) { 		//	D.bug(e);	}	/**	Invoked when a mouse button has been pressed on a component.	*/	public void mousePressed(MouseEvent e) {		//	D.bug(e);		new_value = value;		addMouseMotionListener( this );		mouseDragged(e);	}	/**	Invoked when a mouse button has been released on a component.	*/	public void mouseReleased(MouseEvent e) {		//	D.bug(e);		Enumeration enum;		AdjustmentEvent adjustment;				removeMouseMotionListener( this );		value = new_value;				enum = listeners.elements();		adjustment = new AdjustmentEvent(			this,			0,			0,			value		);		while( enum.hasMoreElements() ) {			((AdjustmentListener) (enum.nextElement()) ).adjustmentValueChanged(				adjustment			);		}	}		/*	 *	MouseMotionListener Interface routines:	 */ 	 	/* *	alternate version.	*  /	public void old_mouseDragged(MouseEvent e) {		int y_divisor = 			e.getX() > (size.width/2)	//	past midline?			?	sensitivity 	//	down == clockwise			:	-sensitivity	//	down == counter-clockwise		;		int x_divisor =			e.getY() > (size.height/2)	//	past midline?			?	-sensitivity	//	right == counter-clockwise			:	sensitivity 	//	right == clockwise		;		int temp = 			(	value 				+ ( e.getY() - prev_mouse_event.getY() ) / y_divisor				+ ( e.getX() - prev_mouse_event.getX() ) / x_divisor			) % 0x10		;		while( temp < 0 ) {			temp += 0x10;	//	Java's % operator can produce negative numbers.		}		if ( new_value != (byte) temp ) {			//	prev_mouse_event = e;			new_value = (byte) temp;			paint_arrow();		}	}	**/	/**	Invoked when a mouse button is pressed on a component and then dragged.	*/	public void mouseDragged(MouseEvent e) {		double x = e.getX() - (size.width/2);		double y = e.getY() - (size.height/2);				int temp = 			(int) Math.round( (Math.atan2(y, x) + RAD_OFFSET)/ RADS_PER_UNIT ) 			% 0x10		;		while( temp < 0 ) {			temp += 0x10;				//	Java's % operator can produce negative numbers.		}		if ( new_value != (byte) temp ) {			new_value = (byte) temp;			paint_arrow();		}	}	/**	Invoked when the mouse button has been moved on a component (with no buttons no down).	*/	public void mouseMoved(MouseEvent e) { }	/*	 *	Adjustable Interface routines:	 */ 	public void addAdjustmentListener(AdjustmentListener l) {		/*			Add a listener to recieve adjustment events 			when the value of the adjustable object changes.		 */		listeners.removeElement(l);	//	don't add more than once		listeners.addElement(l);	}	/**	Gets the block value increment for the adjustable object. */	public int getBlockIncrement() {		return 1;	}	/**	Gets the maximum value of the adjustable object. */	public int getMaximum() {		return 15;	}	/**	Gets the minimum value of the adjustable object. */	public int getMinimum() {		return 0;	}	/**	Gets the orientation of the adjustable object. */	public int getOrientation() {		return 0;	}	/**	Gets the unit value increment for the adjustable object. */	public int getUnitIncrement() {		return 1;	}	/**	Gets the current value of the adjustable object. */	public int getValue() {		return (int) value;	}	/**	Gets the length of the propertional indicator. */	public int getVisibleAmount() { 		return -1;	}	/**	Removes an adjustment listener. */	public void removeAdjustmentListener(AdjustmentListener l) {		listeners.removeElement(l);	}	/**	Sets the block value increment for the adjustable object. */	public void setBlockIncrement(int b) { }	/**	Sets the maximum value of the adjustable object. */	public void setMaximum(int max) { }	/**	Sets the minimum value of the adjustable object. */	public void setMinimum(int min) { }	/**	Sets the unit value increment for the adjustable object. */	public void setUnitIncrement(int u) { }	/**	Sets the current value of the adjustable object. */	public void setValue(int v) {		if ( ((byte) v ) == value ) {			return;	//	no change: don't need to do anything		}		if ( 			( v >= getMinimum() )			&& ( v <= getMaximum() )		) {			value = (byte) v;		} else {			throw new IndexOutOfBoundsException( Integer.toString(v) + " not in range {0,15}") ;		}		new_value = value;		paint_arrow();	}	/**	Sets the length of the proportionl indicator of the adjustable object. */	public void setVisibleAmount(int v) { }}/*useful regexp's:*/