mouseClicked question
in
Programming Questions
•
7 months ago
Hello,
I am trying to create a program that displays 6 rectangles, that change color when clicked. I have succeeded in drawing the rectangles, and the program responds when you click, but it changes the color of all of the rectangles. I am not sure how to get the program working properly. In the code I have included, I have the program set to change color when the upper left rectangle is pressed. Any advice would be much appreciated!
- int width = 640;
- int height = 480;
- float red_channel = 255;
- float green_channel = 255;
- float blue_channel = 255;
- void setup()
- {
- size(width, height);
- }
- void draw() {
- rect(0, 0, width/3, height/2);
- rect((width/3), 0, width/3, height/2);
- rect(width-width/3, 0, width/3, height/2);
- rect(0, height/2, width/3, height/2);
- rect(width/3, height/2, width/3, height/2);
- rect(width-width/3, height/2, width/3, height/2);
- }
- void mouseClicked()
- {
- if(mouseX < width/3 && mouseY < height/2)
- {
- red_channel = random(0, 255);
- green_channel = random(0, 255);
- blue_channel = random(0, 255);
- fill(red_channel, green_channel, blue_channel);
- rect(0, 0, width/3, height/2);
- }
- }
Thanks,
Trevor
1