Attaching MouseListeners to multiple graphics Objects ie rectangles

Can any one please help me by telling me how I can go about attaching mouselisteners to different rectangles in an application using one panel. I m not looking for code but any thing that my show me the way to this. Its very important that i do this.Please help meSq2000
[291 byte] By [Sq2000a] at [2007-11-26 18:59:37]
# 1
If your rectangles are primtive graphics then I don't think you can attach a mouselistener directly to them. You can however listen on the panel and get the coords where the user clicked and find out which rectangle was clicked.
Martin@Stricenta at 2007-7-9 20:41:03 > top of Java-index,Java Essentials,Java Programming...
# 2
In the future, Swing related questions should be posted in the Swing forum.Maybe you just use a JPanel as a rectangle by setting the border of the panel. Then you can add a MouseListener to the panel.
camickra at 2007-7-9 20:41:03 > top of Java-index,Java Essentials,Java Programming...
# 3

> In the future, Swing related questions should be

> posted in the Swing forum.

>

> Maybe you just use a JPanel as a rectangle by setting

> the border of the panel. Then you can add a

> MouseListener to the panel.

The code is as follows

import javax.swing.*;

import java.awt.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;

class MapImager extends JPanel

{

BufferedImage imageMap;

private static JFrame frame = new JFrame();

//private String botswana = "Images/botswana.jpg";

private Rectangle2D.Double square =

new Rectangle2D.Double(0, 0, 350, 350);

private GradientPaint gradient =

new GradientPaint(0, 0, Color.red, 175, 175, Color.yellow,true); // true means to repeat pattern

private final static int SQUARE_EDGE_LENGTH = 10;

private static JPanel panel;

private static int xPos;

private static int yPos;

private static int xP;

private static int yP;

private MapPanel mapPanel;

public MapImager (){

//imageMap = getBufferedImage(botswana, this);

xPos = 186;yPos = 100;

}

static MouseInputAdapter mia = new MouseInputAdapter(){

public void mousePressed(MouseEvent e){

xPos = e.getX();

System.out.println(" The X2 When mousePressed Cordinates are "+xPos);

yPos = e.getY();

System.out.println(" The Y2 When mousePressed Cordinates are "+yPos);

frame.repaint();

}

public void mouseDragged(MouseEvent e){

xPos = e.getX();

System.out.println(" The X2 When mouseDragged Cordinates are "+xPos);

yPos = e.getY();

System.out.println(" The Y2 When mouseDragged Cordinates are "+yPos);

frame.repaint();

}

};

public void paintComponent (Graphics g){

Graphics2D g2d = (Graphics2D)g;

this.setBackground(Color.WHITE);

g2d.setPaint(gradient);

g2d.fill(square);

g2d.drawImage (imageMap, 0, 0, this);

g.setColor(Color.RED);//I want to draw as many as i want and have each move freely on the frame.

g.fillRect(xPos-(SQUARE_EDGE_LENGTH/2), yPos-(SQUARE_EDGE_LENGTH/2), SQUARE_EDGE_LENGTH, SQUARE_EDGE_LENGTH);

}

public static BufferedImage getBufferedImage(String imageFile,Component c){

Image image = c.getToolkit().getImage(imageFile);

waitForImage(image, c);

BufferedImage bufferedImage =

new BufferedImage(image.getWidth(c), image.getHeight(c),

BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = bufferedImage.createGraphics();

g2d.drawImage(image, 0, 0, c);

return(bufferedImage);

}

public static boolean waitForImage(Image image, Component c){

MediaTracker tracker = new MediaTracker(c);

tracker.addImage(image, 0);

try {

tracker.waitForAll();

}

catch(InterruptedException ie) {}

return(!tracker.isErrorAny());

}

public static void main(String[] args){

MapImage map = new MapImage();

frame.add(map);

frame.addMouseListener(mia);

frame.addMouseMotionListener(mia);

frame.setSize(360, 380);

frame.setTitle("Map of Botswana");

frame.setVisible(true);

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

}

}

Sq2000

Sq2000a at 2007-7-9 20:41:03 > top of Java-index,Java Essentials,Java Programming...