Figuring out where a mouse click is under an image

I want to create a JFrame (or JPanel) where the whole area is one big gif (or icon etc) and I want to make some areas of this panel react to mouse clicks and other areas display messages.

There are two ways that I can think of doing this.

The first is that I figure out the x and y coordinates of each area and check to see if each mouse click is within that area.

The second way is to see if I can figure out how to put something like transparent JButtons over the areas that I want to accept mouse clicks (all the areas will be rectangular) and just do what JButtons do normally.

The second way sounds easier but I don't know how to do that.

Does anyone have opinions on which way I should do it?

Thanks

[750 byte] By [alanaa] at [2007-10-3 9:16:29]
# 1

To me the first method sounds eaiser.

A third possible idea? Add a bunch of JPanels to the frame and have split the image into several pieces that are painted by each JPanel. Then use a mouse listener to detect clicks. (Seems easier than trying to make JButton transparent and placing it at the correct point).

zadoka at 2007-7-15 4:29:19 > top of Java-index,Desktop,Core GUI APIs...