NushJa
NushJa

Reputation: 13

Adding Objects to the list and adding those list objects to the Pane with button

So, I'm working on a small night school project and we are "manipulating" with one existing circle. By clicking button 4 (btn4) I need to add few (number is not important) concentric circles to the StackPane. I added them to the list and trying to make it work but I just can't. Everything works except that button.

In btn4 action: Tried putting all code there with for loop, foreach but add() method doesn't accept setFill there so I've put in a list everything outside of the setOnAction()

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {


        HBox hb1 = new HBox(20);
        hb1.setAlignment(Pos.CENTER);
        hb1.setStyle("-fx-background-color:darkgrey;");

        Button btn1 = new Button("+");
        Button btn2 = new Button("-");
        Button btn3 = new Button("Change color");
        Button btn4 = new Button("Add concentric circles");

        hb1.getChildren().addAll(btn1, btn2, btn3, btn4);

        StackPane sp = new StackPane();
        sp.setStyle("-fx-background-color:lightblue;");
        Circle circle = new Circle(75);
        circle.setFill(RED);

        sp.getChildren().add(circle);

        BorderPane bp = new BorderPane();
        bp.setBottom(hb1);
        bp.setCenter(sp);

        btn1.setOnAction(e -> {
            circle.setRadius(circle.getRadius() + 10);
        });

        btn2.setOnAction(e -> {
            circle.setRadius(circle.getRadius() - 10);
        });

        btn3.setOnAction(e -> {
            circle.setFill(Color.color(Math.random(), Math.random(), Math.random(), 1));
        });

        ArrayList <Circle> moreCircles = new ArrayList<>();

        for (int i = 1; i <= 10; i++) {
            new Circle(circle.getRadius() - (i * 5)).setFill(Color.color(Math.random(), Math.random(), Math.random(), 1));
        }

        btn4.setOnAction(e -> {
        for(Circle c : moreCircles) {
                sp.getChildren().add(c);
            }
        });

       Scene scene = new Scene(bp, 500, 350);

        primaryStage.setTitle("Game with circles");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

}

I want to make those new concentric circles slightly smaller from existing one. To add them all at once and I want of course for them to be visible so I added some colors.

Thanks to @anthony yaghi for solving this small problem for me

Button action needs to look like this, and Array above is up for some deleting:

btn4.setOnAction(e -> {
        for (int i = 1; i <= 10; i++) {
            sp.getChildren().add(new Circle(circle.getRadius() - (i*5), Color.color(Math.random(), Math.random(), Math.random(), 1)));
        }
    });

Upvotes: 0

Views: 90

Answers (1)

anthony yaghi
anthony yaghi

Reputation: 550

First of all the for loop just after the creation of the array list is useless. You are creating new circles and doing nothing with them, I guess you were trying to add the to the list but you can't do "new Circle(...) and then .setFill, if you look at the available constructor of the Circle object you can see that there is one that takes a radius and a color for the fill so you can do instead:

new Circle(circle.getRadius() - (i*5), Color.color(Math.random(), Math.random(), Math.random(), 1));

Also if you just want to add n circles inside the current one and don't care what happens later (if the main circle gets bigger or smaller) then you don't need to add the to a list and keep track of them just add them to the StackPane directly.

    /*
    ArrayList<Circle> moreCircles = new ArrayList<>();

    for (int i = 1; i <= 10; i++) {
        new Circle(circle.getRadius() - (i * 5)).setFill(Color.color(Math.random(), Math.random(), Math.random(), 1));
    }
    */

    btn4.setOnAction(e -> {
        for (int i = 1; i <= 10; i++) {
            sp.getChildren().add(new Circle(circle.getRadius() - (i*5), Color.color(Math.random(), Math.random(), Math.random(), 1)));
        }
    });

Upvotes: 4

Related Questions