Using Values from a Class as Arguments for Another Class

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();
  }
}
1 Like

You’re currently only passing those arguments in setup(), but never updating those values beyond that.

I would think about accessing the brush color via the slider class and updating the color from there.

I think the reason your brushColor is returning strange values is because it needs to be initialized first.

color brushColor = color(0,0,0);

I messed around a little and reworked your ColorSlider class … it’s not perfect, but now updates the brush color from the class. Should be a decent start.

colorSlider[0] = new ColorSlider(width*0.945, (height*0.1), width * 0.075, 5, "r");
  colorSlider[1] = new ColorSlider(width*0.945, (height*0.15), width * 0.075, 5, "g");
  colorSlider[2] = new ColorSlider(width*0.945, (height*0.2), width * 0.075, 5, "b");
class ColorSlider {
  PVector barPos, headPos, barRes, headRes, offset;
  boolean isDraggable;
  String col;
  int value;
  color barCol = color(0, 0, 0);


  ColorSlider(float x, float y, float w, float h, String _col) {
    barPos = new PVector(x, y);
    headPos = new PVector(x, y);
    barRes = new PVector(w, h);
    headRes = new PVector(30, 20);
    col = _col;
    offset = new PVector();
    isDraggable = false;
    rectMode(CENTER);
  }

  boolean rollover() {
    return (mouseX >= headPos.x - (headRes.x / 2) &&
      mouseX <= headPos.x + (headRes.x / 2) &&
      mouseY >= headPos.y - (headRes.y / 2) &&
      mouseY <= headPos.y + (headRes.y / 2));
  }

  void checkIfDraggable() {
    if (mousePressed) {
      if (mouseX >= headPos.x - (headRes.x / 2) &&
        mouseX <= headPos.x + (headRes.x / 2) &&
        mouseY >= headPos.y - (headRes.y / 2) &&
        mouseY <= headPos.y + (headRes.y / 2)) {
        isDraggable = true;
        offset.x = headPos.x - mouseX;
        offset.y = headPos.y - mouseY;
      }
    } else {
      this.isDraggable = false;
    }
  }

  void drag() {
    if (isDraggable) {
      headPos.x = constrain(mouseX + offset.x, barPos.x - barRes.x/2, barPos.x + barRes.x/2);
    }
  }

  void head() {
    pushStyle();
    noStroke();
    fill(10);
    ellipse(headPos.x, headPos.y, headRes.x / 2, headRes.x/2);
    popStyle();

    pushStyle();
    if (this.isDraggable) {
      fill(50, 100, 100, 0.7);
    } else {
      fill(50, 100, 100, 1);
    }

    ellipse(headPos.x, headPos.y, headRes.x / 3, headRes.x/3);
    popStyle();
  }

  void bar() {
    pushStyle();
    fill(barCol);
    rect(barPos.x, barPos.y, barRes.x, barRes.y, 30);
    popStyle();
  }

  void updateBarCol() {
    value = int(map(headPos.x, barPos.x - barRes.x/2, barPos.x + barRes.x/2, 0, 255));
  }

  void colText() {
    pushStyle();
    textSize(12);
    textAlign(CENTER);
    fill(3);
    noStroke();
    text(col, barPos.x - ((barRes.x/2) + 15), barPos.y);
    popStyle();
  }

  void display() {
    bar();
    head();
    colText();
    updateBarCol();
  }

  void render() {
    checkIfDraggable();
    drag();
    display();
    rollover();    
    updateBrushColor();
  }

  void updateBrushColor() {
    switch (col) {
    case "r":
      brush.r = value;
      barCol = color(value, 0, 0);
      break;

    case "g":
      brush.g = value;
      barCol = color(0, value, 0);
      break;

    case "b":
      brush.b = value;
      barCol = color(0, 0, value);
      break;
    }
  }
}


1 Like

Thank you, that helps a lot!

1 Like