首页 > 解决方案 > 绘制带有线条的圆圈并检查鼠标是否在圆圈内 - Java Graphics- Geometry

问题描述

使用 Java 图形,我尝试画一个圆,在里面画线,然后检查鼠标是否在圆内并打印鼠标的位置。

我画的线超出了圆圈,当我在这些线的边界内单击时,但在圆圈外,它假定鼠标在圆圈内,因为我给 x 和 y 的限制实际上形成了一个正方形。 这就是我的圈子的样子

如何找到 x 和 y 的限制,使线条不会超过圆圈,并且只有当鼠标在圆圈内时才会打印“内部”?

这就是它的实际样子

这是我的代码

public class Circle extends JFrame implements MouseListener {
    
    int x,y,i;
    JLabel label =new JLabel();


    public void SetLayout(int x, int y, int w, int l, Component c){
        c.setLocation(x, y);
        c.setSize(w, l);
    }
    
    public Circle()
    {   
            Container con = getContentPane();
            setLayout(null);
            SetLayout(10, 10, 70, 20, label);
            con.add(label); 
            this.setSize(1000,500);
            this.setResizable(false);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            addMouseListener(this); 
    }
    public void paint(Graphics g)
    {
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, 1000, 1000);
        g.setColor(Color.BLACK);   
        
        g.drawOval(300,50,400,400);

        g.setColor(Color.cyan);       
        for (i = 2 ; i < 400 ; i += 5){
              g.drawLine(300+i, 50 , 300+i, 450);
        }
                
        g.setColor(Color.BLACK);    
                label.setText( ((double)x-500)/100+"  "+((double)y-250)/-100 );
                
        g.drawLine(0, 250, 1000, 250);
        g.drawLine(500, 0, 500, 500);
        g.drawString("(0,0)",501,265);
        g.drawString("1",601,265); 
        g.drawString("-1",401,265); 
        g.drawString("2",701,265); 
        g.drawString("-2",301,265); 
        g.drawString("3",801,265); 
        g.drawString("-3",201,265); 
        g.drawString("1",503,165);
        g.drawString("-1",503,365);
        g.drawString("2",503,65);
        g.drawString("-2",503,465);
             
    }
    @Override
    public void mouseClicked (MouseEvent e) {
        x = e.getX();
        y = e.getY();       
        
        if ((y >= 50 && y <= 450 && x >= 300) & x <= 700){
                    JOptionPane.showMessageDialog(null, "Inside");
                }    
                else{
                    JOptionPane.showMessageDialog(null, "Outside");
                }
                    
                repaint();
    }
        
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Circle();
    }
        
    @Override
    public void mouseEntered(MouseEvent arg0) { 
        }
    @Override
    public void mouseExited(MouseEvent arg0)  { 
        }
    @Override
    public void mousePressed(MouseEvent arg0) {
    }
    @Override
    public void mouseReleased(MouseEvent arg0){ 
        }
}

也许可以有一个解决方案,比如计算点击点到中心的距离并检查距离是否小于半径。但是,这仍然不能解决超出线的问题。

标签: javageometryawtjava-2d

解决方案


使用三角函数:通过使用反余弦函数,您可以获得 x 轴与该 x 坐标的圆上点之间的角度。然后通过将该角度插入 sin 函数,您可以获得该点在圆上的 y 位置。

Point circleCenter;
int radius;
int lineDistance;


for (int x = -radius; x < radius; x += lineDistance) {

    float double = Math.acos(x / radius);

    int y = Math.sin(angle) * radius;

    int top = circleCenter.y + y;
    int bottom = circleCenter.y - y;


    g.drawLine(x + circleCenter.x, top, x + circleCenter.y, bottom);

}

如果您有时间,不妨研究一下三角学 https://www.mathsisfun.com/algebra/trigonometry.html 这确实是一个引人入胜的主题,对任何程序员来说都是必不可少的。

检查鼠标是否在圆圈内:

Point mousePosition;
Point circleCenter;
int radius;

Point centerToMouse = new Point(mousePosition.x - circleCenter.x, mousePosition.y - circleCenter.y);

bool mouseInside = (int)Math.sqrt(centerToMouse.x * centerToMouse.x + centerToMouse.y * centerToMouse.y) <= radius;


推荐阅读