Detecting mouseEntered event in JComponent
I'm having one of those days.
My application has a JComponent that serves as a paintable canvas, onto which other components (PaintedCircle) are drawn. I want the PaintedCircle objects to detect mouseEntered events and report them but I can't seem to make that happen. I get all of the events from the canvas JComponent but none from the PaintedCircle objects.
Thanks in advance...
djhallx
Code:
package test;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PaintApp extends JComponent
implements ActionListener, MouseListener, MouseMotionListener {
private ArrayList<Component> componentList = new ArrayList<Component>();
/**
*
* @param msgDisplay
*/
public PaintApp() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
private Object pcLock = new Object();
/**
*
* @param c
*/
public void addComponent(Component c) {
synchronized(pcLock) {
componentList.add(c);
this.add(c,0);
this.validateTree();
pcLock.notifyAll();
}
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
//System.out.println("Paint...");
Graphics2D g2d = (Graphics2D) g;
render(g2d);
}
public void paintComponent(Graphics g) {
synchronized (pcLock) {
paint(g);
pcLock.notifyAll();
}
}
public void render(Graphics2D g) {
synchronized (pcLock) {
Graphics2D gx = (Graphics2D)g;
Iterator it = componentList.iterator();
while(it.hasNext()) {
Component comp = (Component)it.next();
comp.paint(gx);
}
}
}
public void actionPerformed(ActionEvent evt) {
this.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("PaintApp.mouseClicked...");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("PaintApp.mouseEntered...");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("PaintApp.mouseExited...");
}
@Override
public void mousePressed(MouseEvent me) {
System.out.println("PaintApp.mousePressed...");
}
@Override
public void mouseReleased(MouseEvent me) {
System.out.println("PaintApp.mouseReleased()...");
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
System.out.println("PaintApp.mouseMoved()...(" + x + "," + y + ")");
}
private static Color[] markColors = {
Color.BLACK, Color.BLUE, Color.RED, Color.CYAN, Color.GREEN
};
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
f.getContentPane().add(p);
PaintApp surface = new PaintApp();
p.add(surface, BorderLayout.CENTER);
PaintedCircle pc;
for (int i = 0; i < 10; i++) {
int x = i*30; //(int)(Math.random() * 400.0);
int y = i*30; //(int)(Math.random() * 300.0);
pc = new PaintedCircle(x, y, markColors[(i%5)], "Mark " + i);
surface.addComponent(pc);
}
f.setSize(800, 400);
f.setVisible(true);
}
}
/**
*
*
*/
class PaintedCircle extends JComponent implements MouseListener {
private int px;
private int py;
private Color color;
private String rolloverMsg;
private int dotCount;
public PaintedCircle(int px, int py, Color color) {
this.px = px;
this.py = py;
this.color = color;
this.dotCount++;
this.addMouseListener(this);
System.out.println("dotCount = " + dotCount);
}
/**
*
* @param px
* @param py
* @param color
* @param rolloverMsg
*/
public PaintedCircle(int px, int py, Color color, String rolloverMsg) {
this.px = px;
this.py = py;
this.color = color;
this.rolloverMsg = rolloverMsg;
this.addMouseListener(this);
}
private static final int DIAMETER = 10;
public int getWidth() {
return DIAMETER;
}
public int getHeight() {
return DIAMETER;
}
public Rectangle getBounds() {
return new Rectangle(6,6);
}
public void paint(Graphics g) {
Graphics2D gx = (Graphics2D) g;
gx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.00f));
Color cSave = gx.getColor();
gx.setColor(color);
gx.fillOval(px-3, py-3,DIAMETER, DIAMETER);
gx.setColor(cSave);
System.out.println("PaintedCircle.paint()");
}
public void processMouseEvent(MouseEvent e) {
System.out.println("PaintedCircle.processMouseEvent ->>" + rolloverMsg);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("PaintedCircle.mouseEntered ->>" + rolloverMsg);
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("PaintedCircle.mouseExited ->>" + rolloverMsg);
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("PaintedCircle.mousePressed...");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("PaintedCircle.mouseReleased...");
}
}