【问题标题】:Draw a circle with a radius and points around the edge画一个圆的半径和点周围的边缘
【发布时间】:2011-01-31 07:26:38
【问题描述】:

我真的被困在如何进行编程上。如何在Java中绘制一个半径和边缘周围的圆?

我需要在 JFrame 中绘制一个圆,其半径和点围绕圆周。我可以数学计算如何找到边缘周围点的坐标,但我似乎无法对圆进行编程。我目前正在使用 Ellipse2D 方法,但这似乎不起作用并且不返回半径,据我了解,它不会从中心绘制圆,而是使用高度和宽度从起始坐标绘制圆。

我当前的代码在一个单独的框架上,但我需要将它添加到我现有的框架中。

import java.awt.*; 
import javax.swing.*; 
import java.awt.geom.*; 

public class circle extends JFrame { 
  public circle() { 
     super("circle"); 
     setSize(410, 435); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     Panel sp = new Panel(); 
     Container content = getContentPane(); 
     content.add(sp); 
     setContentPane(content); 
     setVisible(true); 
 } 

 public static void main (String args[]){
  circle sign = new circle(); 
 } 
} 

class Panel extends JPanel { 
 public void paintComponent(Graphics comp) { 
     super.paintComponent(comp); 
     Graphics2D comp2D = (Graphics2D) comp; 

     comp2D.setColor(Color.red); 
     Ellipse2D.Float sign1 = new Ellipse2D.Float(0F, 0F, 350F, 350F); 
     comp2D.fill(sign1); 
 } 
}

【问题讨论】:

    标签: java swing drawing geometry paintcomponent


    【解决方案1】:

    circle 上的点可以指定为角度 θ 的函数:

    x = a + r cos(θ)
    y = b + r sin(θ)

    这里显示了2π/8的增量。

    附录:正如@Christoffer Hammarström 在评论中所建议的那样,这个revised 示例减少了原始magic numbers 的数量。所需的点数成为构造函数的参数。它还使渲染适应容器的大小。

    /** @see https://stackoverflow.com/questions/2508704 */
    public class CircleTest extends JPanel {
    
        private static final int SIZE = 256;
        private int a = SIZE / 2;
        private int b = a;
        private int r = 4 * SIZE / 5;
        private int n;
    
        /** @param n  the desired number of circles. */
        public CircleTest(int n) {
            super(true);
            this.setPreferredSize(new Dimension(SIZE, SIZE));
            this.n = n;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(Color.black);
            a = getWidth() / 2;
            b = getHeight() / 2;
            int m = Math.min(a, b);
            r = 4 * m / 5;
            int r2 = Math.abs(m - r) / 2;
            g2d.drawOval(a - r, b - r, 2 * r, 2 * r);
            g2d.setColor(Color.blue);
            for (int i = 0; i < n; i++) {
                double t = 2 * Math.PI * i / n;
                int x = (int) Math.round(a + r * Math.cos(t));
                int y = (int) Math.round(b + r * Math.sin(t));
                g2d.fillOval(x - r2, y - r2, 2 * r2, 2 * r2);
            }
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new CircleTest(9));
            f.pack();
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    create();
                }
            });
        }
    }
    

    【讨论】:

    • 谢谢你,这正是我想要的
    • @windopal:太好了。请参阅我的编辑以了解令人尴尬的截断错误。 @Oscar Reyes:¡gracias!.
    • 如果你不介意我问。当你在圆上画点的时候,你为什么要从 x 和 y 坐标减去 4?
    • 好问题:xy 是圆on 上的点。如果我想要一个围绕给定点的 8x8 圆,我需要将边界的顶部和左侧坐标调整一半的宽度和高度。
    • 把这些神奇的数字放在命名良好的常量中,而不是解释它们。
    【解决方案2】:

    试试这样的:

      public class CirclePanel extends JPanel
      {
        public static void main(String[] args) throws Exception
        {
            JFrame f = new JFrame();
    
            f.setContentPane(new CirclePanel());
            f.setSize(700,500);
            f.setVisible(true);
        }
    
        public void paint(Graphics g)
        {
            super.paint(g);
            //Draws the line
            g.drawOval(0,0,this.getWidth(), this.getHeight());
    
            //draws filled circle
            g.setColor(Color.red); 
            g.fillOval(0,0,this.getWidth(), this.getHeight());
        }
      }
    

    你也可以重写框架类中的paint方法,但是你必须计算窗口装饰的大小,它会在那里变脏......

    【讨论】:

    • 谢谢。我想知道是否有办法在给定坐标的圆边缘周围添加一个可见点?
    【解决方案3】:

    使用Minueto

    【讨论】:

      【解决方案4】:

      我建议花一些时间来复习一下“中点圆算法或 Bresenham 圆算法”。公认的解决方案基于非常昂贵的数学运算,例如浮点乘法和三角函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-11
        • 1970-01-01
        • 2023-02-09
        • 1970-01-01
        相关资源
        最近更新 更多