java - 在圆圈内绘制随机点

标签 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

如何只在圆圈内绘制点?

最佳答案

要获得半径为 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

关于java - 在圆圈内绘制随机点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51770447/

相关文章:

java - 使用哪种 Javax.awt 布局?

java - 如何使用 Swing Timer 延迟进度条的加载

java - 为什么(在某些情况下)即使禁用日志记录级别,logback 也会调用 toString() ?

java - 当字符串呈现为 HTML 时,如何获取字符串的大小?

Java从另一个线程更新jtable行

java - 在 Java 中处理循环事件的优雅方式?

java - 在卡片布局中切换卡片时 JMenuBar 不显示

java - 处理自己滚动的 JTabel 替代方案

java 无法从 cmd 运行

java - 尝试理解已经制作的 JApplet 代码