# Problem in code 2

**URL:** <https://discourse.processing.org/t/problem-in-code-2/33755>\
**Category:** Beginners\
**Created:** [November 25, 2021, 12:47pm UTC](https://discourse.processing.org/t/problem-in-code-2/33755 "2021-11-25T12:47:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dohvk](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@dohvk](https://discourse.processing.org/u/dohvk)\
**Post date:** [November 25, 2021, 12:47pm UTC](https://discourse.processing.org/t/problem-in-code-2/33755/1 "2021-11-25T12:47:50Z")

</div>

Hello everyone, I have a problem with my code. I would want to click on one single rectangle to change the color of it but every rectangle change color at the same time. Can someone help? And is there a function to give each shape a color?

this is my code.  
void setup(){  
size(700, 750);  
background(225, 242, 228);  
}

void draw(){  
// rectangles  
int y = 40;  
while(y \< 650) {  
int x = 100;  
while(x \< 600){  
stroke(0);  
strokeWeight(3);  
fill(value);  
rect(x, y, 100, 140, 10);  
if(mouseX \> x && mouseX \< x+100 && mouseY \> y && mouseY \< y+140){  
strokeWeight(3);  
stroke(255, 0, 0);  
//fill(0);  
rect(x, y, 100, 140, 10);  
}  
x = x + 130;  
}  
y = y + 170;  
}  
}

int value = 0;  
int x = 100;  
int y = 40;

void mousePressed() {  
if (value == 0) {  
value = 255;  
} else {  
value = 0;  
}  
}

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [November 25, 2021, 1:36pm UTC](https://discourse.processing.org/t/problem-in-code-2/33755/2 "2021-11-25T13:36:27Z")

</div>

You are redrawing all your rectangles every frame. You are redrawing them all in the same color.

The fill() function is what sets the drawing color.

---

<div class="post-metadata">

**Author:** ![dohvk](https://avatars.discourse-cdn.com/v4/letter/d/e495f1/32.png) [@dohvk](https://discourse.processing.org/u/dohvk)\
**Post date:** [November 25, 2021, 2:06pm UTC](https://discourse.processing.org/t/problem-in-code-2/33755/3 "2021-11-25T14:06:51Z")

</div>

But then what should I change?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [November 25, 2021, 3:22pm UTC](https://discourse.processing.org/t/problem-in-code-2/33755/4 "2021-11-25T15:22:36Z")

</div>

You could create a 2D color array.

> **[Reference](https://processing.org/reference/color_.html)**
>
> Creates colors for storing in variables of the color datatype. The parameters are interpreted as RGB or HSB values depending on the current colorMode(). The default mode is RGB values fr…

> **[Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

When you redraw the rectangles, you should first access the color from the array and use it to fill the shape.
