using area of image as button (active area)

Hello

I have image (jpg) that is displayed in applet and I'd like to change portion of it into let's say button. I would see image as I did before but when I move mouse over this button area something would happen (listener).

Can I do it?. If yes, can this button have any shape I want?.

Cheers

[321 byte] By [macmacmaca] at [2007-10-2 23:09:46]
# 1

yes and yes.

the easy way is this:

put the image in one component, and put the button over top of it. there are a couple ways to do this:

subclass JPanel or JComponent (different people have different preferences here) and override the paint component method to paint the image.

THEN, Override either abstract button or JButton (again, different camps think differently here), and override the paint method (not the paint component method) to do absolutely NOTHING.

add your new button to the panel/component and voila. You have an invisible button overtop of your image (note, the visible property of the button will need to be true for it to respond, but you can choose not to paint the button, even if visible is true).

if you want to use any shape, you must also override the contains method of the button. I find the easiest way to do this is to create a java.awt.Shape or java.awt.Area object, and use the default implemnetation of contains of those objects.

Another solution, rather than painting the image in a JPanel or JComopnent, use the image as the icon of a JLabel, and put both the label and button in the same container, using either a null layout, and set the bounds yourself, or using a layout manager like stack layout which places all components in the same position, with a Z-ordering.

- Adam

guitar_man_Fa at 2007-7-14 6:23:44 > top of Java-index,Security,Cryptography...
# 2

Thank you it helped a bit. But as I remember well there were two examples in Java tutorial just about this. One with planets(picture of solar system), when you moved cursor over one of the planets it showed its name. The other with picture of a man, when you pressed on mouth the sound was being played. Where can I find these examples?. These examples are no more in Java Tutorial.

macmacmaca at 2007-7-14 6:23:44 > top of Java-index,Security,Cryptography...