Grid of rectangles

From: Luke (lukeita_at_gawab.com)
Date: 11/30/04


Date: Tue, 30 Nov 2004 18:32:55 GMT

Hello!
In my applet I need to add an object like this:
www.dentella.it/java/grid.jpg
a grid of 5x5 rectangles.
If the user clicks on one of them it have to change its color (white
-> gray -> black -> white...). I need also a method to set rectangle's
color, like:

setColor(int rgb, int rectrow, int rectcolumn)

My first idea was to use 25 buttons, but I noticed that with some Look
and Feel (for example the new Windows XP one) I can't set button
background, while in Metal it works.
So my idea now (please let me know if it's a good idea :-)) is to
create a new object, from JPanel.
In the paintComponent() method I draw the whole grid with values
stored in a matrix:

rectColors[5][5]

and in the same object (can I do that?) I manage the MouseEvents... if
I get the (x,y) position of mouse when the user clicks, I can detect
on which rectangle was the click and change its value in the matrix...
then I call the repaint() method.

My questions:
- can I manage the mouse events for my object or have I to manage them
in the applet's code?
- should I use double buffering to redraw my object?
- is there a better way to detect on which rect the user clicked?
Thanks!