Fill() in if statement not working

Hello,
I am a recent beginner towards processing api and I have an issue with fill().
(editted)

int numRows;
int rowLength;
int startX;
int startY;
int rectWidth;
int rectHeight;
int takeaway;
boolean isColour;
boolean isAllowed;
boolean isAllowed2;
void setup() {
  size(900, 400);
  numRows = 0;
  rowLength = 10;
  startX = 50;
  startY = 350;
  rectWidth = 75;
  rectHeight = 30;
  takeaway = 1;
  rectMode(CENTER);
}

void draw() {
  int rowX;
  int rowY;
  rowX = startX;
  rowY = startY;
  if (isAllowed == true) { 
    rowLength -= takeaway;
    isAllowed = false;
  }
  for (int rows = 0; rows < numRows; ++rows) {
    rowX = startX;
    if (rows == 0) {
      isAllowed2 = true;
    }
    rowX = (rows+2) * (rectWidth/2);
    for (int block = 0; block < rowLength; block++) {
      rect(rowX, rowY, rectWidth, rectHeight);
      rowX += rectWidth;
    }
    rowY -= rectHeight;
  }
}
void mousePressed() {
  numRows += 1;
 // fill(25,25,25);
  if(isColour){
    println("Hello");
    fill(0);
    fill(random(200),random(200),random(200)); 
  }else{
    fill(255);
  }
  if (isAllowed2 == true) {
    isAllowed = true;
  }
  if (numRows > 10) {
    numRows = 0;
    isColour = !isColour;
    
  }
  println(numRows);
}

Issue is that fill is not working when it reachs a certain number.

Hello,

Go to www.processing.org and review the tutorials, examples and references.

https://processing.org/reference/mousePressed_.html

:)

Hello, and welcome to the forum!!

Great to have you here!!

Is this your entire code? mousePressed won‘t work without setup and draw.

Also you need a rect or something to show your fill color.

Also, you need to reset fill (0); to see a difference. Otherwise the random color stays active.

It’s random (256) by the way

Warm regards

Chrisir

int numRows;
int rowLength;
int startX;
int startY;
int rectWidth;
int rectHeight;
int takeaway;
boolean isColour;
boolean isAllowed;
boolean isAllowed2;
void setup() {
  size(900, 400);
  numRows = 0;
  rowLength = 10;
  startX = 50;
  startY = 350;
  rectWidth = 75;
  rectHeight = 30;
  takeaway = 1;
  rectMode(CENTER);
}

void draw() {
  int rowX;
  int rowY;
  rowX = startX;
  rowY = startY;
  if (isAllowed == true) { 
    rowLength -= takeaway;
    isAllowed = false;
  }
  for (int rows = 0; rows < numRows; ++rows) {
    rowX = startX;
    if (rows == 0) {
      isAllowed2 = true;
    }
    rowX = (rows+2) * (rectWidth/2);
    for (int block = 0; block < rowLength; block++) {
      rect(rowX, rowY, rectWidth, rectHeight);
      rowX += rectWidth;
    }
    rowY -= rectHeight;
  }
}
void mousePressed() {
  numRows += 1;
 // fill(25,25,25);
  if(isColour){

    fill(0);
    fill(random(200),random(200),random(200)); 
  }else{
    fill(255);
  }
  if (isAllowed2 == true) {
    isAllowed = true;
  }
  if (numRows > 10) {
    numRows = 0;
    isColour = !isColour;
    
  }
  println(numRows);
}

here is my code and unfortunately it didn’t work.

You can use a color variable to achieve what you want:

int numRows;
int rowLength;
int startX;
int startY;
int rectWidth;
int rectHeight;
int takeaway;
boolean isColour = true;
boolean isAllowed;
boolean isAllowed2;

color c = color(255);
void setup() {
  size(900, 400);
  numRows = 0;
  rowLength = 10;
  startX = 50;
  startY = 350;
  rectWidth = 75;
  rectHeight = 30;
  takeaway = 1;
  rectMode(CENTER);
}

void draw() {
  int rowX;
  int rowY;
  rowX = startX;
  rowY = startY;
  fill(c);
  if (isAllowed == true) { 
    rowLength -= takeaway;
    isAllowed = false;
  }
  for (int rows = 0; rows < numRows; ++rows) {
    rowX = startX;
    if (rows == 0) {
      isAllowed2 = true;
    }
    rowX = (rows+2) * (rectWidth/2);
    for (int block = 0; block < rowLength; block++) {
      rect(rowX, rowY, rectWidth, rectHeight);
      rowX += rectWidth;
    }
    rowY -= rectHeight;
  }
}
void mousePressed() {
  numRows += 1;
 // fill(25,25,25);
  if(isColour){
    c = color(random(200),random(200),random(200)); 
  }else{
    c = color(255);
  }
  if (isAllowed2 == true) {
    isAllowed = true;
  }
  if (numRows > 10) {
    numRows = 0;
    isColour = !isColour;
    
  }
  println(numRows);
}

It creates a pretty cool effect!

also you can assign variables like integers while initializing which will make your job easier:

int test = 10;

Hello,

You should not have edited your original post.
There were already responses to the original and you changed it.

Original Post < Click here to expand

I am a recent beginner towards processing api and I have an issue with fill().

boolean isCol;

void mousePressed(){
isCol = !isCol;
if(isCol){
fill(random(255),random(255),random(255));
}
}

fill() is working in your code when it reaches a certain number.

I added:
background(0)
println()
to let you see what each click of the mouse is doing.

fill() does change the color of the circle.
fill() does not change the rectangle because you are not drawing any to window when you change the color from red to green.

I would revisit your loops.

References:

https://processing.org/reference/background_.html

I edited your code:

int numRows;
int rowLength;
int startX;
int startY;
int rectWidth;
int rectHeight;
int takeaway;
boolean isColour;
boolean isAllowed;
boolean isAllowed2;

void setup() 
  {
  size(900, 400);
  numRows = 0;
  rowLength = 10;
  startX = 50;
  startY = 350;
  rectWidth = 75;
  rectHeight = 30;
  takeaway = 1;
  rectMode(CENTER);
  }

void draw() 
  {
  background(0);
  int rowX;
  int rowY;
  rowX = startX;
  rowY = startY;
  
  circle(width/3, height/2, 50);
  
  if (isAllowed == true) 
    { 
    rowLength -= takeaway;
    isAllowed = false;
    }
  
  for (int rows = 0; rows < numRows; ++rows) 
    {
    rowX = startX;
    if (rows == 0) 
      {
      isAllowed2 = true;
      }
    rowX = (rows+2) * (rectWidth/2);
    for (int block = 0; block < rowLength; block++) 
      {
      rect(rowX, rowY, rectWidth, rectHeight);
      rowX += rectWidth;
      }
    rowY -= rectHeight;
    }
  
  circle(2*width/3, height/2, 50);
  }
  
void mousePressed() 
  {
  numRows += 1;
 // fill(25,25,25);
  if(isColour)
    {
    //fill(0);
    //fill(random(200),random(200),random(200)); 
    fill(0, 255, 0);
    println("green");
    }  
  else
    {
    fill(255, 0, 0);
    println("red");
    }
  if (isAllowed2 == true) 
    {
    isAllowed = true;
    }
  if (numRows > 10) 
    {
    numRows = 0;
    isColour = !isColour;
    }
  println(numRows, isColour);
  }
1 Like