画一个半径为圆形的圆点
我真的被困在如何去编程这个。如何用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);
}
}
点可以被指定为角度θ的函数:
X = A + R COS(θ)
计算y = b + R SIN(θ)
这里,2π/ 8为增量示出。
附录:正如@ChristofferHammarströ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();
}
});
}
}
尝试这样:
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方法,但随后你会在窗口装饰的大小来计算,它变脏有.. 。在circle
谢谢。 我想知道是否有一种方法可以在给定坐标的圆的边缘附近添加可见点? – alchemey89 2010-03-24 15:27:56
我建议采取一些时间来审查“中点画圆算法或布氏圈算法”。公认的解决方案基于非常昂贵的数学运算,如浮点乘法和三角函数。
+1为显示正在运行的代码的屏幕截图! – OscarRyz 2010-03-24 17:58:04
谢谢,这就是我正在寻找的 – alchemey89 2010-03-24 18:01:28
@windopal:非常好。查看我编辑的尴尬截断错误。 @Oscar雷耶斯:_¡gracias!_。 – trashgod 2010-03-24 18:26:55