I’m trying to make the code I have for a basic painting sketch more efficient, so I made classes for the color sliders and the brush. I’m trying to set the arguments for Brush() using the values from each colorSlider[]. The ‘colorSlider[x].value’ values are changing, but the ‘r, g, b’ values in Brush() that are linked to them aren’t getting changed, and the brush.brushColor is being set at ‘-16777216’.
Am I not allowed to use values from one class as arguments for another class, or is there something else going on? Thanks.
float drawAreaX;
float drawAreaY;
float drawAreaWidth;
float drawAreaHeight;
color drawAreaBackground;
float toolBarLeftX;
float toolBarRightX;
float toolBarY;
float toolBarWidth;
float toolBarHeight;
// BRUSH
Brush brush;
// COLOR SLIDERS
ColorSlider[] colorSlider = new ColorSlider[3];
// BRUSH SIZE SLIDER
BrushSlider brushSlider;
// BUTTONS
Button[] button = new Button[3];
void setup() {
size(1000, 800);
background(255);
// Draw Area Dimensions
drawAreaX = width/2;
drawAreaY = height/2;
drawAreaWidth = width*3/4;
drawAreaHeight = height;
// Tool Bar Dimensions
toolBarLeftX = drawAreaWidth*1/12;
toolBarRightX = drawAreaWidth*15/12;
toolBarY = height/2;
toolBarWidth = drawAreaWidth*1/6;
toolBarHeight = height;
// COLOR SLIDERS
colorSlider[0] = new ColorSlider((toolBarRightX*888/937.5), (height*7/160), (drawAreaWidth*1/150), (height*3/160), "r");
colorSlider[1] = new ColorSlider((toolBarRightX*888/937.5), (height*11/160), (drawAreaWidth*1/150), (height*3/160), "g");
colorSlider[2] = new ColorSlider((toolBarRightX*888/937.5), (height*15/160), (drawAreaWidth*1/150), (height*3/160), "b");
// BRUSH SIZE SLIDER
brushSlider = new BrushSlider((toolBarLeftX*13/62.5), (height/2), (drawAreaWidth*1/150), (height*3/160));
// BUTTONS
button[0] = new Button(toolBarLeftX, height*1/16, toolBarWidth, height*3/160, "CLEAR ALL");
button[1] = new Button(toolBarLeftX, (button[0].y + button[0].h), toolBarWidth, height*3/160, "BUCKET");
button[2] = new Button(toolBarLeftX, (button[1].y + button[1].h), toolBarWidth, height*3/160, "ERASER");
// BRUSH
brush = new Brush( (colorSlider[0].value), (colorSlider[1].value), (colorSlider[2].value) ); // brushColor is not changed by these values
}
void draw() {
// Drawing Area Centre
noFill();
noStroke();
rectMode(CENTER);
rect(drawAreaX, drawAreaY, drawAreaWidth, drawAreaHeight);
// Tool Bars
fill(215);
noStroke();
rectMode(CENTER);
rect(toolBarLeftX, toolBarY, toolBarWidth, toolBarHeight);
rect(toolBarRightX, toolBarY, toolBarWidth, toolBarHeight);
// Brush Size Indicator
fill(255);
rect(toolBarLeftX, toolBarY*4/5, 104, 104);
fill(brush.brushColor);
ellipse(toolBarLeftX, brushSlider.y*4/5, brushSlider.value, brushSlider.value);
// BUTTONS
button[0].display();
button[1].display();
button[2].display();
// DISPLAY SLIDERS
colorSlider[0].run();
colorSlider[1].run();
colorSlider[2].run();
println("Red Value: ", colorSlider[0].value);
println("Green Value: ", colorSlider[1].value);
println("Blue Value: ", colorSlider[2].value);
brushSlider.run();
println("Brush Red: ", brush.r);
println("Brush Green: ", brush.g);
println("Brush Blue: ", brush.b);
println("Brush Color: ", brush.brushColor);
}
// COLOR SLIDER
class ColorSlider {
// variable
float x, y; // x/y position of slider line + initial slider position
float w, h; // w/h of draggable slider
float initialX; // initial position of draggable slider
float endX;
float value;
color c;
String letter;
boolean lock = false;
// constructors
// default
ColorSlider () {
}
ColorSlider (float _x, float _y, float _w, float _h, String _letter) {
x = _x;
initialX = x;
y = _y;
w = _w;
h = _h;
letter = _letter;
}
void run() {
endX = width*981/1000;
value = map(x, initialX, endX, 0, 255);
c = color(value);
fill(c);
rectMode(CORNER);
rect(initialX, y, (width*49/500), 4);
fill(150);
rectMode(CORNER);
rect(x, y, w, h);
fill(0);
textSize(11);
text(nf(int(value), 3), x+5, y+13);
textAlign(CENTER, CENTER);
text(letter, initialX - 7, y);
float mx = constrain(mouseX, initialX, endX);
if (lock) {
x = mx;
}
}
boolean isOver()
{
return (x+w >= mouseX) && (mouseX >= x) && (y+h >= mouseY) && (mouseY >= y);
}
}
// BRUSH SIZE SLIDER
class BrushSlider {
float x, y; // x/y position of slider line + initial slider position
float w, h; // w/h of draggable slider
float initialX; // initial position of draggable slider
float endX;
float value;
color c;
boolean lock = false;
// constructors
// default
BrushSlider () {
}
BrushSlider (float _x, float _y, float _w, float _h) {
x = _x;
initialX = x;
y = _y;
w = _w;
h = _h;
}
void run() {
endX = width*49/500;
value = map(x, initialX, endX, 0, 100);
c = color(value);
fill(c);
rectMode(CORNER);
rect(initialX, y, endX, 4);
fill(150);
rectMode(CORNER);
rect(x, y, w, h);
fill(0);
textSize(11);
text(nf(int(value), 3), x+5, y+13);
float mx = constrain(mouseX, initialX, endX);
if (lock) {
x = mx;
}
}
boolean isOver()
{
return (x+w >= mouseX) && (mouseX >= x) && (y+h >= mouseY) && (mouseY >= y);
}
}
// Brush Object
class Brush {
float r;
float g;
float b;
color brushColor;
Brush() {
}
Brush (float _r, float _g, float _b) {
r = _r;
g = _g;
b = _b;
}
void draw() {
brushColor = color(r, g, b);
stroke(brushColor);
strokeWeight(brushSlider.value);
line(pmouseX, pmouseY, mouseX, mouseY);
}
boolean isOver() {
return (mouseX > (drawAreaWidth*1/6)) && (mouseX < (drawAreaWidth*7/6));
}
}
// BUTTON OBJECT
class Button {
float x, y;
float w, h;
String text;
Button (float _x, float _y, float _w, float _h, String _text) {
x = _x;
y = _y;
w = _w;
h = _h;
text = _text;
}
void display() {
fill(255);
rectMode(CENTER);
rect(x, y, w, h);
fill(0);
textAlign(CENTER, CENTER);
text(text, x, (y - y*1/32));
}
boolean isOver() {
return (x+(w/2) >= mouseX) && (mouseX >= x-(w/2)) && (y+(h/2)) >= mouseY && (mouseY >= y-(h/2));
}
}
void mousePressed() {
for (ColorSlider s : colorSlider)
{
if (s.isOver())
s.lock = true;
}
if (brushSlider.isOver()) {
brushSlider.lock = true;
}
if (button[0].isOver()) {
background(255);
}
if (button[1].isOver()) {
background(brush.brushColor);
}
}
void mouseReleased() {
for (ColorSlider s : colorSlider)
{
s.lock = false;
}
brushSlider.lock = false;
}
void mouseDragged() {
if (brush.isOver()) {
brush.draw();
}
}