简体   繁体   English

绘制一个半径为圆的圆并围绕边缘指向

[英]Draw a circle with a radius and points around the edge

I'm really stuck on how to go about programming this. 我真的被困在如何编程这个。 How to draw a circle in Java with a radius and points around the edge? 如何在Java中用半径绘制一个圆并围绕边缘指向?

I need to draw a circle within a JFrame with a radius and points around the circumference. 我需要在具有半径的JFrame中绘制一个圆并围绕圆周指向。 i can mathematically calculate how to find the coordinates of the point around the edge but i cant seem to be able to program the circle. 我可以在数学上计算如何找到边缘点的坐标,但我似乎无法编程圆。 I am currently using a Ellipse2D method but that doesn't seem to work and doesn't return a radius, as under my understanding, it doesn't draw the circle from the center rather from a starting coordinate using a height and width. 我目前正在使用Ellipse2D方法,但这似乎不起作用并且不返回半径,因为根据我的理解,它不从中心绘制圆而是使用高度和宽度从起始坐标绘制。

My current code is on a separate frame but I need to add it to my existing frame. 我当前的代码是在一个单独的框架上,但我需要将它添加到我现有的框架。

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

Points on a circle may be specified as a function of the angle θ: 上的点可以指定为角度θ的函数:

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

Here, increments of 2π/8 are shown. 这里,示出了2π/ 8的增量。

Addendum: As suggested in a comment by @Christoffer Hammarström, this revised example reduces the number of magic numbers in the original. 附录:正如@ChristofferHammarström在评论中所建议的那样,这个修改过的例子减少了原始数字中的幻数 The desired number of points becomes a parameter to the constructor. 所需的点数成为构造函数的参数。 It also adapts the rendering to the container's size. 它还使渲染适应容器的大小。

替代文字

/** @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();
            }
        });
    }
}

Try something like this: 尝试这样的事情:

  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());
    }
  }

You can also override the paint method in the frame class, but then the you would have to calculate in the size of the window decorations and it gets dirty there... 你也可以覆盖框架类中的paint方法,但是你必须计算窗口装饰的大小并且它在那里变脏...

使用Minueto

I recommend to take some time to review the "midpoint circle algorithm or Bresenham's circle algorithm". 我建议花一些时间来回顾一下“中点圆算法或Bresenham圆算法”。 The accepted solution is based on very costly math operations like float multiplication and trigonometric functions. 公认的解决方案基于非常昂贵的数学运算,如浮点乘法和三角函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM