On Sun, 03 May 2009 19:33:37 -0700, sso <strongsilent...@gmail.com> wrote:
[...]
So I guess only one class needs to implement MouseListener and it must
be a class that extends a JComponent (panel, label, etc).
You need not implement MouseListener in any named class if you don't want
to. I often use anonymous classes for various Swing "listener"
implementations, often making the anonymous class extend MouseAdapter so
that I only have to override the methods of interest to me.
There is no requirement that your MouseListener implementation extend a
JComponent. What you do need is to make sure that once you've got a
MouseListener implementation, you actually add it to a component via an
addMouseListener() method.
I think it would be ideal if I could draw the board with Graphics (a
Vector of tile) and then determine which tile it was that the
MouseEvent occurred on. I'm not sure how to determine which tile the
MouseEvent occurred on.
Two obvious approaches come to mind:
-- Make each tile a component (e.g. JComponent), and add a
MouseListener to each one. Then you simply need to look at the sender of
the event to know which tile was clicked.
-- Make each tile a simple part of the "board" implementation, adding
a MouseListener to whatever part of the GUI presentation is actually a
proper Swing component. In the handler, mathematically determine based on
the mouse coordinates which tile was actually clicked on.
Note that in the first approach, you'll probably want to make the "board"
implementation a component as well. Note also that you need not have
multiple instances of your MouseListener implementation. A single
instance can do all of the appropriate things simply by checking to see
what the actual sender of the event was.
Pete
I had some examples I was looking at that finally make sense.