java - 使用监听器和鼠标指针位置来检查指针是否在圆内

标签 java swing mouselistener

我试图在光标所在的点上绘制一行文本,说明光标是在所绘制的圆的内部还是外部。我不完全确定如何做到这一点,并且我不认为我所做的全局 X 和 Y 坐标工作正常。

有谁知道当光标在圆圈内或圆圈外时如何触发绘制字符串的方法?我对 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);
}

}

最佳答案

您忘记做的一件事是调用repaint()。仅仅因为 insideCircleStatus 的值发生变化,并不会自动执行 repaint(),因此您需要在内部调用 repaint() MouseListener

此外,您还可以使用 Shape 接口(interface)和方法 contains 例如

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

查看使用 MouseMotionListenerAPI 的完整示例- 另请参阅 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);
            }
        });
    }
}

关于java - 使用监听器和鼠标指针位置来检查指针是否在圆内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22269951/

相关文章:

java - 一类静态方法中的构造函数

java - GridBagLayout 调整组件大小

java - 如果我有一个使用 mouselistener 的标签数组列表,那么单击标签时如何获取索引?

Java JFrame 鼠标监听器

java - 为什么我的 Apache Camel 条件路由总是执行第一个选项?

java - Android:如何在设备重新启动后运行服务中的线程

java - RESTful 服务中的注释(@EJB、@Resource...)

java - 将数据库中的数据添加到表中

java - 在 CellRenderer 中显示选定的行和列

java - JLabel 中的可点击词