使用javafx在窗格中放置圆圈

时间:2017-08-13 21:03:34

标签: javafx geometry

尝试使用Javafx动态创建一系列圆圈。输入圈数后我得到了这个:enter image description here

但实际上我希望我的圈子处于那个位置:enter image description here

这是我的代码,感谢任何提示!!

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

}

1 个答案:

答案 0 :(得分:0)

这是我的解决方案:

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

更新:我试图在每个圈子中添加标签,问题是屏幕上的圈数不正确我不知道为什么! 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);