-1

How to create a circle in Java Swing with radios 100 and add with mouse event:

When mouse pointer enters in the circle , it should display message "mouse entered"

This is what I could do so far:

package Circle;

import java.awt.Graphics;
import javax.swing.JFrame;
import java.awt.Color;

public class Circle extends JFrame 
{
public Circle()
{
    setTitle("Tutorial");
    setSize(1960,1960);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void paint(Graphics g)
{
    g.setColor(Color.BLACK);
       g.drawOval(500,500,500,500);
         g.fillOval(500, 500, 500, 500);
    }

    public static void main(String args[])
    {
    Circle t = new Circle();
    t.paint(null);
    }
  }
Wagner Leonardi
  • 4,226
  • 2
  • 35
  • 41

2 Answers2

2

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);
            }
        });

    }
}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • 1
    Can we make `paintComponent` `protected`, there is no reason anyway should ever call it - it's a sore point for me ;) – MadProgrammer Nov 06 '17 at 20:56
0

You can add a MouseMotionListener to the JFrame. It contains a method void mouseMoved(MouseEvent) which you can use to calculate wether your mouse pointer is inside the circle. Use MouseEvent.getX() and MouseEvent.getY() and the bounds of your circle to do so.

An easier way is to use Ellipse2D. Initialize the nested static class Ellipse2D.Double or Ellipse2D.Float with the bounds of your circle and call the method contains(x, y) to verify if the given point is inside the circle. Hint: the width and height of the ellipse is twice as big as the radius of your circle.

You should not directly call paint(null) because it will throw a NullPointerException within the method. It is easier to use repaint() instead.

EDIT: see Berger's answer for a detailed example.

The_Programmer
  • 186
  • 3
  • 12