0

I need to spawn multiple shapes with different properties in javafx but when I run the program none of the shapes are being added. Any guidance in the right direction is appreciated. I would create individual objects but I need to continuously remove and recreate the objects with random properties once they go out of bounds.

package game

import java.util.ArrayList;
import javafx.animation.AnimationTimer;
import static javafx.application.Application.launch;
import javafx.scene.shape.Circle;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;




public class Game extends Application {

static ArrayList <Circle> circles = new ArrayList();

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

@Override
public void start(Stage primaryStage) {
    Pane root = new Pane();
    Scene scene = new Scene(root,800, 600); 
    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            spawnCircles(); 
        }
    };

    timer.start();

    root.getChildren().addAll(circles);
    primaryStage.setScene(scene);
    primaryStage.show();
}


void spawnCircles() {
    if (circles.size() < 20) {
    createCircle();
}
}

void createCircle() {
    Circle circle = new Circle(Math.random() * 100, Color.RED);
    circle.setCenterX(Math.random()* 800);
    circle.setCenterY(Math.random()* 600);
    circles.add(circle);
}


}

1 Answer 1

2

In your start method you add an empty ArrayList to your root pane and later your AnimationTimer adds circles to your ArrayList. How do you expect them to magically show up in your root pane. Just add them to the root pane instead of the ArrayList or make this a Group instead of an ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

now they display on the screen but there is no method to get the size of a group so that I can continuously recreates circles if they go out of bounds and are deleted.
Oh come on. What about group.getChildren().size()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.