Variables Not Changing?

Hey, newbie coder here. I'm currently trying to create a small program that is kind of like Microsoft Paint, it can change colours, brush size, etc. But I want to make it so that you can customize your own colour by clicking a button to add value to "int r =20" and g and b and so forth, but it doesn't seem to work and I can't figure out why, if someone could help me that'd be great. :>

int x=20;
int r=20;
int g=20;
int b=20;

void setup() {
  size(800, 600);
  background(255); // white
  noStroke(); //don't use an outline
  drawControls(); //run function
  extraColours(); //run function 2
  fill(0); //black
}

void draw() {
  println(mouseX, mouseY, r, g, b);
  if (mousePressed == true && mouseY < 500) {
    noStroke();
    ellipse(mouseX, mouseY, x, x);
  }
  if (mousePressed == true && mouseY > 500) {
    checkButtons(); //run checkButtons
  }
  if (mousePressed == true && mouseY > 500 && mouseX > 400 && mouseX < 500); 
  {
    CustomColour(); //run CustomColours
  }
  text("Made by Adrian Chow", 650, 550);
  text("Brush Size=" +x, 550, 550);
}

void drawControls() {
  fill(127); //grey
  rect(0, 500, 800, 100); //bottom part of screen
  fill(255, 0, 0); //red`
  rect(0, 550, 50, 50); //bottom left of screen
  fill(0); //black
  rect(50, 550, 50, 50); //next to red square
  fill(0, 255, 0); //green
  rect(0, 500, 50, 50); //above red square
  fill(0, 0, 255); //blue
  rect(50, 500, 50, 50); //next to green square
}
void checkButtons() {
  if (mouseX < 50 && mouseY >550) { //red
    fill(255, 0, 0);
  }
  if (mouseX < 100 && mouseX > 50 && mouseY >550) { //black
    fill(0, 0, 0);
  }
  if (mouseX < 150 && mouseX > 100 && mouseY >550) { //purple
    fill(143, 0, 179);
  }
  if (mouseX < 200 && mouseX > 150 && mouseY >550) { //pink
    fill(240, 89, 225);
  }
  if (mouseX < 250 && mouseX > 200 && mouseY >550) { //light blue
    fill(41, 255, 242);
  }
  if (mouseX < 50 && mouseX > 0 && mouseY > 500 && mouseY < 550) { //green
    fill(0, 255, 0);
  }
  if (mouseX < 100 && mouseX > 50 && mouseY > 500 && mouseY < 550) { //blue
    fill(0, 0, 255);
  }
  if (mouseX < 150 && mouseX > 100 && mouseY > 500 && mouseY < 550) { //orange
    fill(255, 102, 0);
  }
  if (mouseX < 200 && mouseX > 150 && mouseY > 500 && mouseY < 550) { //yellow
    fill(255, 255, 0);
  }
  if (mouseX < 250 && mouseX > 200 && mouseY > 500 && mouseY < 550) { //brown
    fill(106, 77, 4);
  }
  if (mouseX < 300 && mouseX > 250 && mouseY >550) { //rubber
    fill(255);
  }
  if (mouseX < 350 && mouseX > 300 && mouseY > 550) { //add size
    x=x+2;
  }
  if (mouseX < 350 && mouseX > 300 && mouseY > 500 && mouseY < 550) { //minus size
    x=x-2;
  }
  if (mouseX < 250 && mouseX > 200 && mouseY > 500 && mouseY < 550); 
  {
    fill(r, g, b);
  }
}

void CustomColour() {
  if (mouseX < 400 && mouseX > 350 && mouseY > 500 && mouseY >550); 
  { 
    r=r+5;
  }
  if (mouseX < 400 && mouseX > 350 && mouseY > 550 && mouseY > 600); 
  {
    r=r-5;
  }
  if (mouseX < 450 && mouseX > 400 && mouseY > 500 && mouseY > 550); 
  {
    g=g+5;
  }
  if (mouseX < 450 && mouseX > 400 && mouseY > 550 && mouseY > 600); 
  {
    g=g-5;
  }
  if (mouseX < 500 && mouseX > 450 && mouseY > 500 && mouseY > 550); 
  {
    b=b+5;
  }
  if (mouseX < 500 && mouseX > 450 && mouseY > 550 && mouseY > 600); 
  {
    b=b-5;
  }
}
void extraColours() {
  fill(255, 102, 0); //orange
  rect(100, 500, 50, 50); //second row, bottom left of screen
  fill(240, 89, 225); //pink
  rect(150, 550, 50, 50); //second row, next to orange square
  fill(143, 0, 179); //purple
  rect(100, 550, 50, 50); //second row, above orange square
  fill(255, 255, 0); //yellow
  rect(150, 500, 50, 50); //second row, next to purple square
  fill(106, 77, 4); //brown
  rect(200, 500, 50, 50); //third group, next to yellow
  fill(41, 255, 242); //light blue
  rect(200, 550, 50, 50); //third group, below brown
  fill(255); //rubber square colour
  rect(250, 550, 50, 50); //rubber square
  fill(0); //addsize colour
  rect(300, 550, 50, 50); //addsize
  fill(255); //minusesize colour
  rect(300, 500, 50, 50); //minussize
  //add sign
  fill(255); //white
  rect(305, 572, 40, 5); //horizontal
  rect(323, 555, 5, 40); //vertical
  //minus sign
  fill(0); //black
  rect(305, 525, 40, 5); //horizontal
  //Custom Colour First Row (Plus)
  stroke(5);
  fill(255); //RGB White
  rect(350, 500, 50, 50); //R
  rect(400, 500, 50, 50); //G
  rect(450, 500, 50, 50); //B
  fill(0);
  text("R+", 370, 530); //R Text
  text("G+", 420, 530); //G Text
  text("B+", 470, 530); //B Text
  //Custom Colour Second Row (Minus)
  fill(255);
  rect(350, 550, 50, 50); //R
  rect(400, 550, 50, 50); //G
  rect(450, 550, 50, 50); //B
  fill(0);
  text("R-", 370, 580); //R Text
  text("G-", 420, 580); //G Text
  text("B-", 470, 580); //B Text
}
Tagged:

Answers

  • edited May 2015

    Let's take a look at just one of your if statements:

    if (mouseX < 400 && mouseX > 350 && mouseY > 500 && mouseY >550); 
      { 
        r=r+5;
      }
    

    See that semicolon at the end if your if statement? That shouldn't be there.

    With that semicolon, you're basically saying "if this if statement is true, then do nothing" and then the code in the block always executes.

    You should debug problems like this by adding print statements and trying to narrow down when the program's execution differs from your expectations.

    Note that you've also got some strange logic here. You're checking wether mouseY is greater than 500, and then you're also checking whether mouseY is greater than 550. Maybe you meant for one of those to be a less than?

  • Yeah, that's probably it. Thanks so much KevinWorkman, I'll try it when I get home. :)

Sign In or Register to comment.