import java.awt.*; //============================================================================== // HueDial Class // // AGG - Alexander Gee // // 041497 - code converted to Java //============================================================================== public class HueDial extends Canvas { private Dimension offsize; // off screen buffer size private Graphics offgraphics; // off screen buffer private Image offimage; // off screen buffer image private boolean redrawall; // redraw state private int width; // Component width private int height; // Component height private Color hue_list[]; // list of colors private int hue_count; // number of colors private double oldpos; // previous marker position (radians) private int diam_outer; // dial outer diameter private int diam_inner; // dial inner diameter private int diam_marker; // dial marker diameter private int center_x; // dial center x-position private int center_y; // dial center y-position public double newpos; // current marker position (radians) // HueDial Class Constructor 1: //-------------------------------------------------------------------------- public HueDial() { width = 200; height = 200; resize( width, height ); diam_outer = 100; diam_inner = 40; diam_marker = 120; center_x = width/2; center_y = height/2; newpos = 0.0; oldpos = 0.0; redrawall = true; hue_count = 256; hue_list = new Color[hue_count]; for( int i = 0; i < hue_count; i++ ) { hue_list[i] = new Color( Color.HSBtoRGB( (float)i/(float)hue_count, (float)1, (float)1 ) ); } } // HueDial Class Constructor 2: //-------------------------------------------------------------------------- public HueDial( int width, int height ) { this.width = width; this.height = height; resize( this.width, this.height ); if( width < height ) { diam_outer = width - 60; } else { diam_outer = height - 60; } diam_inner = 40; diam_marker = diam_outer + 20; center_x = this.width/2; center_y = this.height/2; newpos = 0.0; oldpos = 0.0; redrawall = true; hue_count = 256; hue_list = new Color[hue_count]; for( int i = 0; i < hue_count; i++ ) { hue_list[hue_count-1-i] = new Color( Color.HSBtoRGB( (float)i/(float)hue_count, (float)1, (float)1 ) ); } } public void update( Graphics g ) { paint( g ); } public void paint( Graphics g ) { if( offgraphics == null ) { offsize = size(); offimage = createImage( offsize.width, offsize.height ); offgraphics = offimage.getGraphics(); } if( redrawall ) { offgraphics.setColor( getBackground() ); offgraphics.fillRect( 0, 0, width, height ); offgraphics.setColor( Color.white ); offgraphics.drawRect( 5, 5, width-10, height-10 ); for( int i = 0; i < 360; i++ ) { offgraphics.setColor( hue_list[(int)(i*255.0/360.0)] ); offgraphics.fillArc( center_x - (int)(diam_outer/2.0), center_y - (int)(diam_outer/2.0), diam_outer, diam_outer, 360-i, 3 ); } offgraphics.setColor( getBackground() ); offgraphics.fillArc( center_x - (int)(diam_inner/2.0), center_y - (int)(diam_inner/2.0), diam_inner, diam_inner, 0, 360 ); } g.drawImage( offimage, 0, 0, this ); g.setColor( Color.black ); //g.drawString( "( " + (int)(newpos*180.0/Math.PI) + " )", 10, 20 ); int mx = center_x + (int)(Math.cos( newpos )*diam_marker/2.0); int my = center_y - (int)(Math.sin( newpos )*diam_marker/2.0); g.fillArc( mx-2, my-2, 6, 6, 0, 360 ); } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown( Event evt, int x, int y ) { calculatePositionAngle( x, y ); redrawall = false; repaint(); return false; } // MOUSE SUPPORT: // The mouseUp() method is called if the mouse button is released // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseUp( Event evt, int x, int y ) { calculatePositionAngle( x, y ); redrawall = false; repaint(); return false; } // MOUSE SUPPORT: // The mouseDrag() method is called if the mouse cursor moves over the // applet's portion of the screen while the mouse button is being held. //-------------------------------------------------------------------------- public boolean mouseDrag( Event evt, int x, int y ) { calculatePositionAngle( x, y ); redrawall = false; repaint(); return false; } //-------------------------------------------------------------------------- private void calculatePositionAngle( int x, int y ) { double xx = x - center_x; double yy = center_y - y; oldpos = newpos; if( -0.001 < xx && xx < 0.001 ) { if( yy < 0 ) { newpos = 0.5*Math.PI; } else { newpos = 1.5*Math.PI; } } else { newpos = Math.atan( yy/xx ); if( yy > 0 ) { if( xx > 0 ) { // no action } else { newpos = Math.PI + newpos; } } else { if( xx < 0 ) { newpos = Math.PI + newpos; } else { newpos = 2.0*Math.PI + newpos; } } } } // public access functions //------------------------------------------------------------------------- public float getValue() { return (float)(newpos*180.0/Math.PI); // convert RAD to DEG } public void setValue( float pos ) { newpos = (double)pos*Math.PI/180.0; // convert DEG to RAD repaint(); } }