Hover color change

Hello! I’m a Processing beginner.

Trying to make (a few) circles and rectangles in the 100 pixel radius of the mouse coordinates change color (and maybe even move, once I have the color change down)

It says “b cannot be resolved to a variable”. I can’t tell why. How could I go about this better?

My code so far:

size(600, 600);
noStroke();
background(255);
fill(250);

int a = 0;
int c = 0;
while (a < width) 
while (c < width){
int b = 0;
int d = 0;
while(b < height)
while(d < height){
  
  rect(a, b-15, 70, 30);
  rect(c-15, d, 30, 70);
  b= b + 70;
  d= d + 70;
  }
  
  a= a + 70;
  c= c + 70;
  
}

int x = 0;
while (x < width) {
  int y = 0;
  while(y < height) {
    fill(240);
  ellipse(x, y, 30, 30);
  y= y + 70;
  }
  x= x + 70;
}

if( dist(a, b, mouseX, mouseY) < 20 ){
      fill(0);
} else {
      fill(250);
}
  • Local variable b is declared inside the while (c < width) { loop block: int b = 0;
  • And then you attempt to access it at if (dist(a, b, mouseX, mouseY) < 20) {.
  • Which is already outside the while (c < width) { loop block.
  • In Java, variables cease to exist when it’s outta scope.

I tried to use what I understood from this and wrote this instead. No error now, but the colour still doesn’t change on hovering. Not sure I understand why.

int a = 0;
int b = 0;
int c = 0;
int d = 0;

void setup(){
size(450, 850);
noStroke();
frameRate(15);
background(255);
}

//fill(250);
//vertical and horizontal rectangles

void draw(){
while (a < width) 
while (c < width){
b = 0;
d = 0;
while(b < height)
while(d < height){
  fill(random(150,255), 220, 220);
  rect(a, b-15, 70, 30);
  rect(c - 15, d, 30, 70);
  b= b + 70;
  d= d + 70;
  }
  
  a= a + 70;
  c= c + 70;
  
}

//circles

int x = 0;
int y = 0;
while (x < width) {
y = 0;
  while(y < height) {
    fill(255);
  ellipse(x, y, 30, 30);
  y= y + 70;
  }
  x= x + 70;
}
  if ( dist(x, y, mouseX, mouseY) < 20 ){
      fill(0);
  } else {
      fill(250);
  }

  if ( dist(x, y, mouseX, mouseY) < 20 ){
      fill(0);
  } else {
      fill(250);
  }

}

Click ctrl-t to improve your indents automatically

Then check how far the while loops reach

Hint: the if must be inside the while loops }

and also before ellipse