【问题标题】:Draw circle with lines in it and check if mouse is inside the circle- Java Graphics- Geometry绘制带有线条的圆圈并检查鼠标是否在圆圈内 - Java Graphics- Geometry
【发布时间】:2021-08-11 00:30:26
【问题描述】:

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

我绘制的线条超出了圆圈,当我在这些线条的边界内单击但在圆圈之外时,它假定鼠标在圆圈内,因为我给 x 和 y 的限制实际上形成了一个正方形。 this is how my circle looks like

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

this is how it should actually look like

这是我的代码

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){ 
        }
}

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

【问题讨论】:

    标签: java geometry awt java-2d


    【解决方案1】:

    使用三角函数: 通过使用反余弦函数,您可以获得 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;
    
    

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 2012-08-29
      • 2016-02-08
      • 2015-07-02
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多