How to detect click over specfic area in the Canvas
i making a board game.
i draw the graphics on my JPanel,
i don't actually used the JButton but instead i draw the picture of the graphics button on JPanel (because i want fixed size-location button, and not simple button look Button) okay i can detect the user clicking on my JPanel. but what i want is that i want to be able to call the function on the Button that is draw on that JPanel. is there any approach on doing that. i don't want to do something like
public void mouseClicked(MouseEvent ev){ // on JPanel click
int x = ev.getX, y = ev.getY();
if ( (x > 20) && (x < 70) && (y > 10) && (y < 40)){
doButtonClick1();
}else if ( (x > 90) && (x < 15) && (y > 20) && (y < 50)){
doButtonClick2();
}
[846 byte] By [
Joe13x] at [2007-9-27 13:58:01]

How about this: public class ImageMap implements MouseListener
{
private List shapeMaps;
private Class ShapeMap
{
Shape shape;
MouseListener handler;
}
public ImageMap()
{
shapeMaps = new Vector();
}
public add(Shape shape, MouseListener handler)
{
if (shape == null || handler == null)
{
throw new IllegalArgumentException("Null arguments not permitted");
}
ShapeMap shapeMap = new shapeMap();
shapeMap.shape = shape;
shapeMap.handler = handler;
shapeMaps.add(shapeMap);
}
public void mouseClicked(MouseEvent evt)
{
MouseListener handler = getHandler(evt);
if (handler != null)
{
handler.mouseClicked(evt);
}
}
// and similarly for mouseEntered, etc.
private MouseListener getHandler(MouseEvent evt)
{
Iterator it = shapeMaps.iterator();
while (it.hasNext())
{
ShapeMap shapeMap = (ShapeMap)it.next();
if (shapeMap.shape.contains(evt.getX(), evt.getY())
{
return shapeMap.handler;
}
}
return null;
}
}