0

I am trying to fill in a specific rectangle when a user clicks it. A picture of the grid is below:

Grid Layout

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.

  • 1
    You mean something like [this for example](http://stackoverflow.com/questions/14742069/java-how-to-paint-rectangles-on-mouseclick/14742199#14742199)? – MadProgrammer Mar 01 '17 at 03:49
  • 1
    *"My long winded question is how I should go about knowing which rectangle I am in and coloring that one specifically?"* - would invoke a long winded answer based on assumption and guess work to cover all the possible solutions you could use to achieve the same result as you provided no additional context to your problem, like which framework your using to display the grid, how the grid locations are defined/calculated. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Mar 01 '17 at 03:51

0 Answers0