如何使用 JavaFX 在随机位置绘制 10000 个圆圈?
How to draw 10000 circles in Random locations using JavaFX?
我试图在 JavaFX 中绘制 10,000 个圆圈,但它似乎不起作用,我什至无法绘制一个圆圈。实际上它给我一个错误:
这是我目前拥有的代码:
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 是否是在随机位置创建圆圈的正确方法吗?
谢谢。
更新: 对于那些认为我的 post 是重复的人,事实并非如此。我的问题来自于我在代码中实现的逻辑,而不是来自 NullPointerException(不是真的)错误。 ,这是错误的。已经有人帮我解决了。
更改此行:
private Random randomNumbers;
对此:
private Random randomNumbers = new Random();
您的代码假定 Random 对象将像其他成员变量一样分配,但它是一个对象,必须使用 new 创建。
修复您遇到的运行时错误后,您的代码只绘制了一个圆圈。那是因为您只向场景图中添加了一个圆圈。 for
循环基本上什么都不做。圆心的最后一个 X 和 Y 坐标用于绘制单个单独的圆。你需要加一万个圈子。
在下面的代码中,我将 10_000 更改为 100(一百),因为 10_000 在您设置的舞台尺寸中有太多重叠圆圈。我还增加了每个圆的半径。
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 中绘制 10,000 个圆圈,但它似乎不起作用,我什至无法绘制一个圆圈。实际上它给我一个错误:
这是我目前拥有的代码:
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 是否是在随机位置创建圆圈的正确方法吗?
谢谢。
更新: 对于那些认为我的 post 是重复的人,事实并非如此。我的问题来自于我在代码中实现的逻辑,而不是来自 NullPointerException(不是真的)错误。 ,这是错误的。已经有人帮我解决了。
更改此行:
private Random randomNumbers;
对此:
private Random randomNumbers = new Random();
您的代码假定 Random 对象将像其他成员变量一样分配,但它是一个对象,必须使用 new 创建。
修复您遇到的运行时错误后,您的代码只绘制了一个圆圈。那是因为您只向场景图中添加了一个圆圈。 for
循环基本上什么都不做。圆心的最后一个 X 和 Y 坐标用于绘制单个单独的圆。你需要加一万个圈子。
在下面的代码中,我将 10_000 更改为 100(一百),因为 10_000 在您设置的舞台尺寸中有太多重叠圆圈。我还增加了每个圆的半径。
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);
}
}