you are adding a MouseListener
and waiting for mouseEntered
events. These are fired when the mouse enters a Component
, not a region of it. Try entering the component's boundary where the ellipse is shown and observe.
What you need is a MouseMotionListener
, so that you can observe the mouse pixel by pixel; use the mouseMoved
or mouseDragged
events.
You might still need to listen for mouseEntered
or mouseExited
events, as MouseMotionEvent
s are only fired while inside the component's boundary, so you might miss the mouse exiting the component while still inside the ellipse.
A good and simple way for debugging this is adding prints inside the event handler. You would then see that the handler was called, but only once or a few times, and not when you move the mouse within the component.
class Help_Option extends JComponent implements MouseListener, MouseMotionListener {
Ellipse2D ellipse = ...;
public Help_Option() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e) {
if (ellipse.contains(e.getX(), e.getY()) ) {
//mouse is inside the ellipse
} else {
//mouse is outside the ellipse
}
}
public void mouseExited(MouseEvent e) {
//mouse is outside the ellipse
}
//more method stubs
}