【问题标题】:How to draw a circle with given X and Y coordinates as the middle spot of the circle?如何以给定的 X 和 Y 坐标绘制一个圆作为圆的中间点?
【发布时间】:2013-10-23 14:17:38
【问题描述】:

我开发了一个用于定位信号塔信号强度的电信应用程序。我使用了 java swing,在移动信号发射塔位置的给定点周围绘制圆圈时遇到问题。我已经计算了 X、Y 坐标和半径值。

请找到下面我用来绘制圆圈的代码,但它有问题。

JPanel panelBgImg = new JPanel() {
    public void paintComponent(Graphics g) {
        g.drawOval(X, Y, r, r);
    }
}

问题是,它创建了圆,但没有将 X 和 Y 坐标作为中心点。它将 X 和 Y 坐标作为圆的左上角。

谁能帮我把给定的X和Y坐标作为圆的中心点来画圆。

【问题讨论】:

    标签: java swing jpanel java-2d paintcomponent


    【解决方案1】:

    fillOval 适合矩形内的椭圆,with width=r, height = r 你得到一个圆。 如果您希望fillOval(x,y,r,r) 以 (x,y) 为中心绘制一个圆,则必须将矩形替换一半宽度和一半高度。

    public void drawCenteredCircle(Graphics2D g, int x, int y, int r) {
      x = x-(r/2);
      y = y-(r/2);
      g.fillOval(x,y,r,r);
    }
    

    这将画一个圆心在x,y

    【讨论】:

    • +1 用于注意到 OP 使用 r(半径)作为伪 d(直径)。这些参数在文档中标记为wh
    【解决方案2】:

    所以我们都在做同样的家庭作业?

    奇怪的是投票最多的答案是错误的。请记住,draw/fillOval 将高度和宽度作为参数,而不是半径。因此,要使用用户提供的 x、y 和半径值正确绘制圆并使其居中,您需要执行以下操作:

    public static void drawCircle(Graphics g, int x, int y, int radius) {
    
      int diameter = radius * 2;
    
      //shift x and y by the radius of the circle in order to correctly center it
      g.fillOval(x - radius, y - radius, diameter, diameter); 
    
    }
    

    【讨论】:

    • '奇怪的是投票最多的答案是错误的'。没错,他只是用字母r作为直径,即高度和宽度。这就是 OP 命名他的变量的方式。
    【解决方案3】:

    替换你的画线
    g.drawOval(X - r, Y - r, r, r)
    

    这应该使您的圈子的左上角成为使中心成为(X,Y)的正确位置, 至少只要点 (X - r,Y - r) 的两个分量都在范围内。

    【讨论】:

    • 我很确定即使左上角超出范围也可以正常工作
    • 我从来没有尝试过在屏幕外画东西,所以我相信你的话。当然,如果角在范围内,它确实有效。
    • 我同意@Cruncher
    【解决方案4】:
    drawCircle(int X, int Y, int Radius, ColorFill, Graphics gObj) 
    

    【讨论】:

    【解决方案5】:
    JPanel pnlCircle = new JPanel() {
            public void paintComponent(Graphics g) {
                int X=100;
                int Y=100;
                int d=200;
                g.drawOval(X, Y, d, d);
            }
    };
    

    您可以随意更改 X、Y 坐标和半径。

    【讨论】:

      【解决方案6】:

      两个答案都不正确。它应该是:

      x-=r;
      y-=r;
      
      
      drawOval(x,y,r*2,r*2);
      

      【讨论】:

        【解决方案7】:
        import java.awt.Color;
        import java.awt.Graphics2D;
        import java.awt.Graphics;
        import javax.swing.JFrame;
        
        public class Graphiic
        {   
            public Graphics GClass;
            public Graphics2D G2D;
            public  void Draw_Circle(JFrame jf,int radius , int  xLocation, int yLocation)
            {
                GClass = jf.getGraphics();
                GClass.setPaintMode();
                GClass.setColor(Color.MAGENTA);
                GClass.fillArc(xLocation, yLocation, radius, radius, 0, 360);
                GClass.drawLine(100, 100, 200, 200);    
            }
        
        }
        

        【讨论】:

          【解决方案8】:

          这会在指定的矩形中绘制一个圆心。你可以画,半圆,四分之一圆等。

          g.drawArc(x - r, y - r, r * 2, r * 2, 0, 360)
          

          【讨论】:

            【解决方案9】:

            唯一对我有用的东西:

            g.drawOval((getWidth()-200)/2,(getHeight()-200)/2, 200, 200);    
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2021-12-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多