-2

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: enter image description here

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);
    }
}

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

Thanks.

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.

Kirasiris
  • 523
  • 10
  • 37
  • Pasting the error text into your post is much more helpful than a link to the image. Thanks! – Gene Z. Ragan Oct 01 '19 at 05:08
  • 3
    Variable `randomNumbers` is not initialized. – Abra Oct 01 '19 at 05:09
  • yes, thats the code, I just changed the class name. The error comes from the for where I'm trying to randomly position the new circles. – Kirasiris Oct 01 '19 at 05:19
  • [Why not upload images of code on SO when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) Please explain why you posted an image of the stack trace and the actual code as text. – Abra Oct 01 '19 at 05:45
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Impulse The Fox Oct 01 '19 at 06:12
  • @Abra most people don't feel comfortable clicking to a different browser tab just to check X file. – Kirasiris Oct 01 '19 at 06:17

2 Answers2

5

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.

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);
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
1

Change this line:

private Random randomNumbers;

to this:

private Random randomNumbers = new Random();

Your code is assuming the the Random object will be allocated like the other member variables, but it is an object and must be created with new.

Gene Z. Ragan
  • 2,643
  • 2
  • 31
  • 41
  • Yes, I would say so, the error youre talking about comes from the inside the circle where I'm trying to randomly position the 'generated'(if there actually any) circles – Kirasiris Oct 01 '19 at 05:15