I am trying to fill in a specific rectangle when a user clicks it. A picture of the grid is below:
So when a user clicks on one of the rectangles it should be filled in with the selected color. I have already done the color part and I am drawing and then filling rectangles. However, my rectangles do not stay within the clicked rectangle because I am not calculating where the grid rectangle is.
Here is how I am getting the starting position to draw a rectangle:
positionX = event.getX();
positionY = event.getY();
width = getSize().width; // This is wrong
height = getSize().height; // This is wrong
I am then adding the rectangle to an Array with parameters positionX, positionY, width, height, color and calling repaint().
My long winded question is how I should go about knowing which rectangle I am in and coloring that one specifically? I believe I need to base the width and height off of where the mouse is pressed, but since the user can press anywhere in a rectangle, I am unclear on how to do that.