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