I hope someone can enlighten me as I have no idea how to do what im trying to do.
I have an applet which has a grid of 225 squares (15x15). The grid starts at 200, 0. The squares are 40x40. I have added a
mouselistener and have coded it to execute a method on mousedown. What I am trying to do is make a method that finds the square that the mouse was clicked in, without coding 225 if(mouse is here) do something statements.
What I am asking is if there is a way to get the x, y values of the corner of the square in which the user clicked. Example: the user clicks at 212, 5. The method will return 200, 0 (the x,y position of the corner of the topleft square in the grid).
public void paintGrid(Graphics g)
{
g.setColor(Color.gray);
for(int x = 200; x < 800; x += 40)
{
for(int y = 0; y < 600; y += 40)
{
g.drawRect(x, y, 40, 40);
}
}
}
Thanks