How does one go about coloring an individual rectangle when it's hovered over? The specific method used below really doesn't give me any ideas on how to solve this problem. It generates a grid in the window using individual rectangles. How would it be possible to listen for mouseX
and mouseY
and color one rectangle without disrupting this code? Thanks.
int cols,rows;
int scl = 20;
int gridsize = 0;
void setup(){
size(400,400);
int w = 400;
int h = 400;
cols = w / scl;
rows = h / scl;
}
void draw() {
//mouseX, mouseY
background(r,g,b);
for (int x = 0; x < cols; x++){
for (int y = 0; y < rows; y++){
stroke(55);
//noFill();
fill(50,50,50);
rect(x*scl,y*scl,scl,scl);
}
}
}
For reference, I am using Processing 3 for Java.
You can always check if the mouse is within the bounds of a rectangle:
Here's the above applied to your code (if condition formatting for easy visibility, feel free to re-format):
For more info also checkout the Processing Button example