user1296809
user1296809

Reputation: 13

Using listeners and mouse pointer location to check if pointer is inside of a circle

I'm trying to make it so a line of text is drawn on the point of the cursor that states if the cursor is inside or outside of the drawn circle. I'm not entirely sure how to do that and I don't think that the global X and Y coordinates I've made are working properly.

Does anyone know of a way to some how trigger the string to be drawn when the cursor is in or out of the circle? I'm still sort of new to Java.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MouseCircleLocation extends JPanel {

boolean insideCircleStatus;

PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();

public MouseCircleLocation() {

    //the listener that checks if the mouse if moving.
    addMouseMotionListener(new MouseAdapter() {
        @Override
        public void mouseMoved(MouseEvent e) {

            //gets the location of the mouse.
            int x = e.getX();
            int y = e.getY();

            //simple check to see if the mouse location is inside the circle area.
            if (  (Math.pow((x - 100), 2)) + (Math.pow((y - 60), 2)) < (Math.pow((50), 2))){

                insideCircleStatus = true;
            }
            else{

                insideCircleStatus = false;
            }
        }
    });
}

//basic frame setup.
public static void main(String[] args) {
    JFrame frame = new JFrame("Mouse Location");
    frame.add(new MouseCircleLocation());
    frame.setSize(210, 190);
    frame.setLocationRelativeTo(null); // Center the frame   
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);   
}

//draws the circle.
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (insideCircleStatus = true){
        g.drawString("Point is inside the circle", x, y);
        System.out.println("Point is inside the circle");
    }
    if (insideCircleStatus = false){
        g.drawString("Point is outside the circle", x, y);
        System.out.println("Point is outside the circle");
    }

    g.setColor(Color.BLACK);
    g.drawOval(100 - 50, 60 - 50, 50 *2, 50 * 2);
}

}

Upvotes: 0

Views: 3508

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208974

One thing you're forgetting to do is call repaint(). Just because the value of insideCircleStatus changes, doesn't make it automatically repaint(), so you'll want to call repaint() inside the MouseListener

Also, you could use the Shape interface and the method contains for example

Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, 200, 200);
...
addMouseListener(new MouseAdapter(){
     public void mousePressed(MouseEvent e) {
         if (circle.contains(e.getPoint())) {
             System.out.println("Click within Circle");
         }
     }
 });

See full example using MouseMotionListener and the API - Also See Ellips2D

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
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;

public class TestShapeContains extends JPanel {

    private static final int D_W = 500;
    private static final int D_H = 500;

    Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, 300, 300);

    Point p = new Point();
    private boolean insideCircle = false;

    public TestShapeContains() {
         addMouseMotionListener(new MouseMotionAdapter(){
             public void mouseMoved(MouseEvent e) {
                 if (circle.contains(e.getPoint())) {
                     insideCircle = true;
                 } else {
                     insideCircle = false;
                 }   
                 p = e.getPoint();
                 repaint();
             }
         });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.draw(circle);
        if (insideCircle) {
            g.drawString("Mouse in Circle at point " + (int)p.getX() + ", " + (int)p.getY(), 
                (int)p.getX(), (int)p.getY());
        } else {
            g.drawString("Mouse outside Circle at point " + (int)p.getX() + ", " + (int)p.getY(), 
                (int)p.getX(), (int)p.getY());
        }   
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(D_W, D_H);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestShapeContains());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions