# Fill() in if statement not working

**URL:** <https://discourse.processing.org/t/fill-in-if-statement-not-working/24623>\
**Category:** Beginners\
**Created:** [October 15, 2020, 6:36pm UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623 "2020-10-15T18:36:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Khun](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/khun/32/10861_2.png) [@Khun](https://discourse.processing.org/u/Khun)\
**Post date:** [October 15, 2020, 6:36pm UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623/1 "2020-10-15T18:36:49Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 15, 2020, 7:02pm UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623/2 "2020-10-15T19:02:35Z")

</div>

Hello,

Go to _[www.processing.org](http://www.processing.org)_ and review the tutorials, examples and references.

_[https://processing.org/reference/mousePressed\_.html](https://processing.org/reference/mousePressed_.html)_

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 15, 2020, 9:38pm UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623/3 "2020-10-15T21:38:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Khun](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/khun/32/10861_2.png) [@Khun](https://discourse.processing.org/u/Khun)\
**Post date:** [October 16, 2020, 8:38pm UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623/4 "2020-10-16T20:38:48Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![qewer3322](https://avatars.discourse-cdn.com/v4/letter/q/e19adc/32.png) [@qewer3322](https://discourse.processing.org/u/qewer3322)\
**Post date:** [October 17, 2020, 9:27am UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623/5 "2020-10-17T09:27:51Z")

</div>

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

> **[color() \\ Language (API) \\ Processing 3+](https://www.processing.org/reference/color_.html)**

```auto
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:

```auto
int test = 10;

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 17, 2020, 10:54am UTC](https://discourse.processing.org/t/fill-in-if-statement-not-working/24623/6 "2020-10-17T10:54:31Z")

</div>

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().
> 
> ```auto
> boolean isCol;
> 
> void mousePressed(){
> isCol = !isCol;
> if(isCol){
> fill(random(255),random(255),random(255));
> }
> }
> 
> ```

> [@Khun](#):
>
> Issue is that fill is not working when it reachs a certain number.

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:

_[background() / Reference / Processing.org](https://processing.org/reference/background_.html)_

I edited your code:

```auto
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);
  }

```
