Results 1 to 2 of 2
- 02-25-2009, 10:23 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
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
Java 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..."); } }
- 02-26-2009, 10:39 PM #2
I'm not an expert in the graphics arena, but I seriously doubt that painted areas are in any way treated as objects and therefore don't emit events. Your best bet is to keep track of where you painted objects and correlate the mouse position within the JComponent.
Here are two things I would experiment with.
1. Try using a javax.swing.Timer firing at around 30 milli intervals instead of handling a continuous stream of mouse movements. Each time the timer fires, capture the mouse position and test it against what you drew.
2. Create a custom class that retains the location, size, and shape of each item you paint. Add a method to test if the mouse is within its region. Keep a list of these objects, and use them to test if the mouse entered or exited a region you painted.
Good luck...
Similar Threads
-
Detecting software installed in PC
By Lukalo in forum Advanced JavaReplies: 3Last Post: 02-13-2009, 03:04 AM -
JComponent gradient background
By snipered in forum AWT / SwingReplies: 0Last Post: 12-30-2008, 12:38 AM -
polygon-shaped JComponent
By zenMarko in forum New To JavaReplies: 2Last Post: 11-04-2008, 06:06 PM -
Detecting browser with JSP scriptlet
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:05 AM -
Detecting Browser Settings
By arupranjans in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 07-31-2007, 02:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks