检测鼠标悬停在非 JComponent 的 object 上

Detecting a mouse hover over an object that is not a JComponent

如标​​题所述,我正在尝试检测鼠标悬停在不是 JComponent 的 object 上。 现在我有一个带有绿色 JPanel 的 window。当您在此 JPanel 上 left-click 时,您创建了一个点。

我想做的是当我将鼠标悬停在这些点上时显示额外的信息。但是,如果我将鼠标悬停在某个点上,我什至不知道如何开始检测。我尝试查看 MouseListener 界面,但找不到任何使用带有 object 的 MouseListener 的示例。我只看到人们将 MouseListener 与 JComponents 一起使用。如果可能的话,我希望在我的 Point class 中加入这个鼠标悬停检测代码,以保持我的代码干净。

J面板代码

class Map extends JPanel implements MouseListener {
    
    public static ArrayList<Point> points = new ArrayList<Point>(); //array for the points
    
    public Map() {
        this.setBackground(Color.green);
        this.setPreferredSize(new Dimension(1280, 720));
        this.addMouseListener(this);
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        
        drawPoints(graphics);
    }
    
    private void drawPoints(Graphics2D graphics) {
        for(int i = 0; i < points.size(); i++) {
            points.get(i).drawPoint(graphics);
        }
    }
    
    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1) { //Left Click
            points.add(new Point(e.getX(), e.getY()));
            repaint();
        }
        else if(e.getButton() == MouseEvent.BUTTON3) { //right click
            
            for(int i = points.size() - 1; i >= 0; i--) { //loop backwards so if points overlap remove the one on top first
                Point current = points.get(i);
                if( Math.abs( e.getX() - current.x ) < current.size/2 && Math.abs( e.getY() - current.y ) < current.size/2 ) {
                    points.remove(i);
                    repaint();
                    break;
                }
            }
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
    
}

点码

public class Point {
    public int x, y;
    public int size = 10;
    
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public Point() {
        this.x = 0;
        this.y = 0;
    }
    
    public void drawPoint(Graphics2D graphics) {
        graphics.setPaint(Color.black);
        graphics.setStroke(new BasicStroke(5));
        graphics.drawOval(x - (size/2), y - (size/2), size, size);
        graphics.setPaint(Color.red);
        graphics.fillOval(x - (size/2), y - (size/2), size, size);
    }
    
    public void drawInfo(Graphics2D graphics) {
        graphics.drawString("test", x, y);
    }
    
}

我在类似 Packet-Tracer 的程序中遇到了同样的问题,我在其中绘制了矩形。

如果只是点,我会检查鼠标移动时鼠标线是否与点线相同。

@Override
public void mouseMoved(MouseEvent e) {
    entered = false;
    
    if(point.x == e.getX() && point.y == e.getY()){
        entered = true;
    }
}

虽然像我的情况一样,绘制的对象有宽度和高度,但它会变得更乱。

@Override
public void mouseMoved(MouseEvent e) {
    entered = false;
    
    if((e.getX() <= point.x+width) && (e.getX() >= point.x)){
        if((e.getY() <= point.y+height) && (e.getY() >= point.y)){
            entered = true;
        }
    }
}