Here is an example with several notes :
1- you don't need to extend JFrame
, use a custom JPanel
and set it as the content of the frame.
2- override paintComponent
and not paint
, paintComponent
has the single responsibility of painting the current component (your panel).
3- use a Shape object (here an Ellipse2D.Double
), because it has a lovely contains(int x,int y)
method .
4- add a MouseMotionListener
to the panel and check when the mouse is moved, if its location is inside your shape.
5- Display the frame in the Event Dispatch Thread
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
// see note 1
public class CirclePanel extends JPanel {
// see note 3
Ellipse2D circle = new Ellipse2D.Double(0, 0, 100, 100);
public CirclePanel() {
// see note 4
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(final MouseEvent e) {
if (circle.contains(e.getX(), e.getY())) {
System.out.println("Mouse entered");
}
}
});
}
// see note 2
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.draw(circle);
}
public static void main(final String args[]) {
// see note 5
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
CirclePanel t = new CirclePanel();
frame.getContentPane().add(t);
frame.setTitle("Tutorial");
frame.setSize(150, 150);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}