-1

Trying to create dynamically a series of circles with Javafx. After typing the number of circles i got this:enter image description here

But actually i want that my circles be in that position:enter image description here

Here is my code and thanks for any hints!!

   int k = 5;
        for (int i = 0; i < nbNoeuds; i++) {

            Noeudfx circle = new Noeudfx(k * 2, k * 2, 1, String.valueOf(i));
            Label id = new Label(String.valueOf(i));
            noeuds.getChildren().add(id);
            id.setLayoutX(k * 2 - 20);
            id.setLayoutY(k * 2 - 20);
            id.setBlendMode(BlendMode.DIFFERENCE);
            k += 10;
            FillTransition ft1 = new FillTransition(Duration.millis(300), circle, Color.RED, Color.BLACK);
            ft1.play();
            noeuds.getChildren().add(circle);
            ScaleTransition tr = new ScaleTransition(Duration.millis(100), circle);
            tr.setByX(10f);
            tr.setByY(10f);
            tr.setInterpolator(Interpolator.EASE_OUT);
            tr.play();

        }

    }




public class Noeudfx extends Circle {

Noeud noeud;
Point point;
Label distance = new Label("distance : infinite");
boolean isSelected = false;
List<Noeudfx> circles = new ArrayList<>();

public Noeudfx(double a, double b, double c, String nom) {
    super(a, b, c);
    noeud = new Noeud(nom, this);
    point = new Point((int) a, (int) b);

    circles.add(this);
}

}

Mira
  • 53
  • 2
  • 9

1 Answers1

0

Here is my solution:

  int nbNoeuds = Integer.parseInt(nodeID.getText());
        System.out.println("nnnnn"
                + nbNoeuds);
        final Timeline animation = new Timeline(
                new KeyFrame(Duration.seconds(.5), (ActionEvent actionEvent) -> {

                    while (noeuds.getChildren().size() <= nbNoeuds) {
                        // noeuds.getChildren().remove(0);

                        int radius =10 ;
                        noeuds.getChildren().add(
                                new Circle(
                                        rnd.nextInt(SCENE_SIZE - radius * 2) + radius, rnd.nextInt(SCENE_SIZE - radius * 2) + radius,
                                        radius,
                                        Color.GRAY
                                )
                        );

                    }
                })
        );
        animation.setCycleCount(Animation.INDEFINITE);
        animation.play();
        animation.setOnFinished((ActionEvent actionevent) -> {
            animation.stop();
        });

Update: i tried to add label to each circle, the problem was that the number of circles in the screen is not correct i don't know why! see here

Label id = new Label(String.valueOf(i));
                        id.setTextFill(Color.CADETBLUE);
                        id.setAlignment(Pos.CENTER);

                        Circle circle = new Circle(
                                rnd.nextInt(SCENE_SIZE - radius * 2) + radius, rnd.nextInt(SCENE_SIZE - radius * 2) + radius,
                                radius,
                                Color.GRAY
                        );
                        Double a = circle.getCenterX();
                        Double b = circle.getCenterY();
                        id.setLayoutX(a - 20);
                        id.setLayoutY(b - 20);
                        id.setBlendMode(BlendMode.DIFFERENCE);

                         noeuds.getChildren().add(id);
                        noeuds.getChildren().add(circle);
Mira
  • 53
  • 2
  • 9