如何使用JavaFX在随机位置绘制10000个圆? [英] How to draw 10000 circles in Random locations using JavaFX?

查看:378
本文介绍了如何使用JavaFX在随机位置绘制10000个圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaFX中绘制10,000个圆,但似乎不起作用,甚至无法绘制一个圆.实际上,它引发了我一个错误:

I'm trying to draw 10,000 circles in JavaFX but it seems like its not working and I'm not even able to draw a single circle. Actually it trows me an error:

这是我目前拥有的代码:

This is the code that I currently have:

public class RandomCircles extends Application {

    private Random randomNumbers;
    private int count;
    private final double MAX_X = 600;
    private final double MAX_Y = 300;
    private final int FINAL_CIRCLES = 10000;

    public void start(Stage primaryStage){

        Circle initCircle = new Circle();
        initCircle.setStroke(Color.BLACK);
        initCircle.setStrokeWidth(3);
        initCircle.setRadius(1);

        for(count = 0; count <= FINAL_CIRCLES; count++){
            initCircle.setCenterX(randomNumbers.nextInt((int) MAX_X));
            initCircle.setCenterY(randomNumbers.nextInt((int) MAX_Y));
        }

        Group baseDemo = new Group(initCircle);

        // Scene scene = new Scene(baseDemo, MAX_X, MAX_Y);
        Scene scene = new Scene(baseDemo);
        scene.setFill(Color.WHITE);
        scene.getWidth();

        primaryStage.setTitle("10,000");
        primaryStage.setScene(scene);
        primaryStage.setResizable(true);
        primaryStage.show();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        launch(args);
    }
}

有人可以告诉我使用setCenterX/Y是否是在随机位置创建圆的正确方法吗?

Can somebody also tell me if using the setCenterX/Y is the right approach to create the circles in random locations?

谢谢.

更新:对于我的帖子虽然重复但不重复的人.我的问题来自于我在代码中实现的逻辑,而不是来自NullPointerException(不是真的)错误. ,这是错误的.已经有人帮我解决了.

UPDATE: To the person who though of my post as a duplicate, it is not. My problem comes from my logic that I implemented in my code not from a NullPointerException(not really) error. , which was wrong. Some guy already helped me to solve it.

推荐答案

更正了运行时错误之后,您的代码只画了一个圆圈.那是因为您只向场景图添加了一个圆圈. for循环基本上不执行任何操作.圆心的最后X和Y坐标用于绘制单个孤立圆.您需要添加一万个圈子.

After fixing the runtime error you are getting, your code only draws one circle. That's because you only add one circle to your scene graph. The for loop basically does nothing. The last X and Y coordinates for the circle's center are used to draw the single, solitary circle. You need to add ten thousand circles.

在下面的代码中,我将10_000更改为100(一百),因为10_000在您设置的舞台尺寸中有太多重叠的圆圈.我还增加了每个圆的半径.

In the code below, I changed 10_000 to 100 (one hundred) since 10_000 has too many overlapping circles in the stage dimensions that you set. I also increased each circle's radius.

import java.util.Random;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class RandomCircles extends Application {
    private Random randomNumbers = new Random();
    private int count;
    private final double MAX_X = 600;
    private final double MAX_Y = 300;
    private final int FINAL_CIRCLES = 100;

    public void start(Stage primaryStage){
        Group baseDemo = new Group();
        for(count = 0; count <= FINAL_CIRCLES; count++){
            Circle initCircle = new Circle();
            initCircle.setStroke(Color.BLACK);
            initCircle.setStrokeWidth(3);
            initCircle.setRadius(5);
            initCircle.setCenterX(randomNumbers.nextInt((int) MAX_X));
            initCircle.setCenterY(randomNumbers.nextInt((int) MAX_Y));
            baseDemo.getChildren().add(initCircle);
        }

        Scene scene = new Scene(baseDemo);
        scene.setFill(Color.WHITE);
        scene.getWidth();

        primaryStage.setTitle("100");
        primaryStage.setScene(scene);
        primaryStage.setResizable(true);
        primaryStage.show();
    }

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

这篇关于如何使用JavaFX在随机位置绘制10000个圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆