classSlider{float x,y; //positionfloat w, h; //sizeString label;float min, max; //end points of rangefloat sliderX; //mouseX when slider value changesfloat sliderVal; //calculate using the map function float hue, sat, bright; //IMPORTANT REMEMBER THESE ALL HAVE VALUE of 0.0//CONSTRUCTORSlider(float x,float y,float w,float h,float min,float max,String label ){this.x= x;this.y= y;this.w= w;this.h= h;this.min= min;this.max= max;this.label= label; sliderX = x + w/2.0 ; sliderVal =map( sliderX, x, x+w, min, max); }//Behaviors - Methods voidbackgroundLayer(){fill( 200);noStroke();rect( x-5, y-28,w+10, h+34); //larger than inner sliderfill(0); //text colortextAlign( RIGHT);textSize(10);text( label,x+w, y-16); }voiddisplay( ){backgroundLayer( );fill( 150);rect( x,y,w,h);//indicator rectangle fill(0);//blackrect( sliderX-2, y-3,4, h+6);textSize(10);text( (int)sliderVal, sliderX+2, y-4); }booleancheckPressed( int mx,int my){boolean changed =false;if( mx > x && mx < x+w && my> y && my< y+h){ sliderX = mx; sliderVal =map( sliderX, x, x+w, min, max); changed =true; } //end ifreturn changed; }//end checkPressed} //end class slider
Main Tab Code:
In the code below, we use a slider instance to control the scale of an ellipse
Slider slider1;float hue; //modified by the slidervoidsetup(){size( 700,700);colorMode( HSB,360,100,100); slider1 =newSlider( 200,200,200,30,0,360,"Hue"); hue =slider1.sliderVal; //initialize hue using slider to set the initial value}voiddraw(){background( 0);if( mousePressed){boolean isChanged =slider1.checkPressed( mouseX, mouseY);if(isChanged){ hue =slider1.sliderVal; //update hue using updated sliderVal } }slider1.display();fill( hue,100,100); //hue modified by slider1rect( 20,20,50,50); //rectangle hue modified by slider1 }
HueSlider
For the HueSlider child class, we override the display( ) method. We use a for-loop to draw vertical lines across the rectangle, where each line's color is determined by the map function. The map function calculates what the hueValue should be for each value of i in the for-loop. The for-loop is executed once for every pixel of width in the display's rectangle. So, one line is drawn for every value of i, so map calculates what the hueValue should be, based on i having a range of values from 0-w, where the hueValue should have a range of values from min to max.
classHueSliderextendsSlider{//CONSTRUCTORS// Slider(float x, float y, float w, float h, float min, float max, String label ){HueSlider(float x,float y,float w,float h,float min,float max){ super( x, y, w, h, min, max,"HUE"); }//METHODS - displays specialized background with ROYGBIVvoiddisplay(){ super.backgroundLayer(); //call base class methodfor( int i=0; i< w; i++){float hueValue =map(i,0,w,min, max); stroke( hueValue,100,100);line(x+i, y, x+i, y+h); }//end for-loop//Indicator rectangle//reset strokestroke(0);fill(sliderVal,100,100);//blackrect( sliderX-2, y-3,4, h+6);fill(0); //for texttextSize(10);text( (int)sliderVal, sliderX+2, y-4); }//end display}//end class
SatSlider
The SatSlider also overrides the display( ) method from the base-class. It has logic to determine how the saturation value should change for each vertical line drawn from i=0 to i=w (the width of the rectangle).
classSatSliderextendsSlider{//no child-class instance variables //base class slider constructor// Slider(int x, int y, int w, int h, float min, float max, String label ){//Define constructorSatSlider( int x,int y,int w,int h,float min,float max ){ super(x, y, w, h, min, max,"Sat" ); //call base-class constructor }//Draw vertical lines where the saturation changes from min to maxvoiddisplay(){ super.backgroundLayer();float satValue =0; ///this will be used to color the line///creates saturation varied background of the hitbox rectanglefor( int i =0; i< w; i++){ satValue =map( i,0, w, min, max); ///convert pixels to colorsstroke( hue, satValue,100);line( x + i, y, x+i, y+h); }stroke( 50);fill(hue ,sliderVal,100);rect( sliderX-2, y-3,4, h+6); } //end display} // end satSlider