在圆内绘制随机点

时间:2018-08-09 15:07:11

标签: java swing awt graphics2d

我想在给定的圆圈内绘制50个随机点。问题是圆中不包含点。这是一个可运行的示例:

package mygraphicsshapehomework;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;


public class MyGraphicsShapeHomeWork extends JFrame {


    public static void main(String[] args) {
        new MyGraphicsShapeHomeWork();        
    }


      public MyGraphicsShapeHomeWork() {
        super("Title"); 
        setBounds(600, 400, 700, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;

        g2.drawOval(40, 40, 90, 90); 

        Color newColor = new Color(255, 0, 0); 
        g2.setColor(newColor); 

        for (int i = 0; i < 50; i++) {
            int x =  (int) Math.ceil(Math.random() * 10);
            int y =  (int) Math.ceil(Math.random() * 10);            

                g2.fillOval(i+x, i+y, 3, 3);   // ???          

        }

    }    
}

这是它产生的结果:

enter image description here

如何仅在圆内绘制点?

2 个答案:

答案 0 :(得分:3)

要获得半径为R的圆中的随机点,请找到一个随机角度和一个随机半径:

double a = random() * 2 * PI; 
double r = R * sqrt(random()); 

然后该点的坐标为:

double x = r * cos(a)
double y = r * sin(a)

以下是有关绘图部分的注意事项。您不应直接在诸如JFrame之类的顶级容器上绘画。而是使用JComponentJPanel。覆盖paintComponent()进行绘画而不是paint()进行绘画,请不要忘记调用super.paintComponent(g)

有关更多信息,请参见Performing Custom Painting教程。

请勿使用setBounds(),请覆盖面板的getPreferredSize()pack()框架。另外,您几乎不需要扩展JFrame。

这是一个基本示例,演示了具有亚像素精度的绘图:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestDots extends JPanel{
    public static final int POINTS_NUM = 1000;
    public static final Color POINT_COLOR = Color.RED;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

        double padding = 10;
        double radius = Math.min(this.getWidth(), this.getHeight()) / 2 - padding * 2;

        g2.draw(new Ellipse2D.Double(padding, padding, radius * 2, radius * 2));
        g2.setColor(POINT_COLOR); 

        for (int i = 0; i < POINTS_NUM; i++) {
            double a = Math.random() * 2 * Math.PI;
            double r = radius * Math.sqrt(Math.random());
            double x = r * Math.cos(a) + radius + padding;
            double y = r * Math.sin(a) + radius + padding;

            g2.draw(new Ellipse2D.Double(x, y, 1, 1));
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {   
            public void run() {   
                JFrame frame = new JFrame("TestDots");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.add(new TestDots());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

这是结果:

enter image description here

答案 1 :(得分:0)

对于点的位置,请在外圆的边界内生成随机坐标。为了生成这些坐标,从圆心开始的点的半径必须小于外圆的半径。使用

获取随机角度
float a = Math.random() * Math.PI * 2; 

然后,从外半径中减去一个随机值:

outerR - (Math.sqrt(Math.random()) * outerR)

并将职位分配给:

double x = Math.cos(a)*newR;
double y = Math.sin(a)*newR;

我敢肯定,对此有更多的数学方法,但是我认为这是最简单的方法。