Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在圆内随机画点_Java_Swing_Awt_Graphics2d - Fatal编程技术网

Java 在圆内随机画点

Java 在圆内随机画点,java,swing,awt,graphics2d,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 st

我想在给定的圆内随机画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);   // ???          

        }

    }    
}
包装mygraphicsshapehomework;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入javax.swing.JFrame;
公共类MyGraphicsShapeHomeWork扩展了JFrame{
公共静态void main(字符串[]args){
新MyGraphicsShapeHomeWork();
}
公共MyGraphicsShapeHomeWork(){
超级(“标题”);
立根(600400700400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(真);
}
@凌驾
公共空间涂料(图g){
超级油漆(g);
图形2d g2=(图形2d)g;
g2.drawOval(40,40,90,90);
颜色newColor=新颜色(255,0,0);
g2.设置颜色(新颜色);
对于(int i=0;i<50;i++){
intx=(int)Math.ceil(Math.random()*10);
int y=(int)Math.ceil(Math.random()*10);
g2.椭圆形(i+x,i+y,3,3);/???
}
}    
}
以下是它产生的结果:


如何仅在圆内绘制点?

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

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;

我相信有一种更为数学化的方法,但在我看来这是最简单的。

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

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;

我确信有一种更为数学化的方法,但在我看来这是最简单的。

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

double a = random() * 2 * PI; 
double r = R * sqrt(random()); 
那么点的坐标是:

double x = r * cos(a)
double y = r * sin(a)
以下是有关绘图零件的一些注释。您不应该直接在顶级容器(如
JFrame
)上绘制。相反,请使用
JComponent
JPanel
。覆盖用于绘制的
paintComponent()
,而不是
paint()
,并且不要忘记调用
super.paintComponent(g)

有关更多信息,请参阅教程

不要使用
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);
            }
        });
    }
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.RenderingHints;
导入java.awt.geom.Ellipse2D;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类TestDots扩展了JPanel{
公共静态最终整数点_NUM=1000;
公共静态最终色点_Color=Color.RED;
@凌驾
公共维度getPreferredSize(){
返回新维度(400400);
}
@凌驾
受保护组件(图形g){
超级组件(g);
图形2d g2=(图形2d)g;
g2.setRenderingHint(renderingHits.KEY\u ANTIALIASING,renderingHits.VALUE\u ANTIALIAS\u ON);
g2.setRenderingHint(renderingHits.KEY\u STROKE\u控件,renderingHits.VALUE\u STROKE\u PURE);
双填充=10;
double radius=Math.min(this.getWidth(),this.getHeight())/2-padding*2;
g2.绘制(新的椭圆E2D.Double(填充,填充,半径*2,半径*2));
g2.设置颜色(点颜色);
对于(int i=0;i
结果如下:


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

double a = random() * 2 * PI; 
double r = R * sqrt(random()); 
那么点的坐标是:

double x = r * cos(a)
double y = r * sin(a)
以下是有关绘图零件的一些注释。您不应该直接在顶级容器(如
JFrame
)上绘制。相反,请使用
JComponent
JPanel
。覆盖用于绘制的
paintComponent()
,而不是
paint()
,并且不要忘记调用
super.paintComponent(g)

有关更多信息,请参阅教程

不要使用
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);
            }
        });
    }
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.RenderingHints;
导入java.awt.geom.Ellipse2D;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类TestDots扩展了JPanel{