Slider
Slider
Slider Base Class
class Slider{
float x,y; //position
float w, h; //size
String label;
float min, max; //end points of range
float sliderX; //mouseX when slider value changes
float sliderVal; //calculate using the map function
float hue, sat, bright; //IMPORTANT REMEMBER THESE ALL HAVE VALUE of 0.0
//CONSTRUCTOR
Slider(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
void backgroundLayer(){
fill( 200);
noStroke();
rect( x-5, y-28,w+10, h+34); //larger than inner slider
fill(0); //text color
textAlign( RIGHT);
textSize(10);
text( label,x+w, y-16);
}
void display( ){
backgroundLayer( );
fill( 150);
rect( x,y,w,h);
//indicator rectangle
fill(0);//black
rect( sliderX-2, y-3, 4, h+6);
textSize(10);
text( (int)sliderVal, sliderX+2, y-4);
}
boolean checkPressed( 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 if
return changed;
}//end checkPressed
} //end class sliderMain Tab Code:
In the code below, we use a slider instance to control the scale of an ellipse
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.
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).
Last updated
Was this helpful?