使用侦听器和鼠标指针位置来检查指针是否在圆圈内

时间:2014-03-08 13:28:06

标签: 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);
}

}

1 个答案:

答案 0 :(得分:2)

您忘记做的一件事就是致电repaint()。仅仅因为insideCircleStatus的值发生了变化,并不会自动repaint(),所以您需要在repaint()

内调用MouseListener

此外,您可以使用Shape界面和方法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);
            }
        });
    }
}