根据用户输入显示多个动画形状 (JavaFX)
Display multiple animated shapes based off of user input (JavaFX)
我正在编写一个代码来获取用户输入的整数,它将获取窗格周围具有随机大小和位置的圆圈数。例如,如果用户想要 5 个圆圈,他们输入 5,将显示 5 个圆圈并设置动画。该程序的动画部分有效。问题是,一旦我将它们实现到 for 循环中,圆圈现在就不会出现(因此我可以根据用户的输入有多个圆圈。每当我按下按钮显示动画圆圈时,只有一个出现,然后IDE 底部的框给我随机错误。我不确定如何解决这个问题。
这是我的代码:
private class buttonEvent implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
String countCircles = numOfCircles.getText();
int circleCount = Integer.parseInt(countCircles);
String countDuration = duration.getText();
int speed = Integer.parseInt(countDuration);
double widthRand = ((Math.random() * (800 - 0)) + 0);
double lengthRand = ((Math.random() * (600 - 0)) + 0);
double radiusRand = Math.floor(Math.random() * (100 - 10 + 1) + 10);
Circle circle = new Circle(widthRand, lengthRand, radiusRand);
circle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
ScaleTransition scaleTr = new ScaleTransition();
scaleTr.setDuration(Duration.millis(speed));
scaleTr.setFromX(1);
scaleTr.setFromY(1);
scaleTr.setToX(0.001);
scaleTr.setToY(0.001);
scaleTr.setCycleCount(3);
scaleTr.setAutoReverse(true);
group = new GroupLayout.Group(circle);
scaleTr.setNode(group);
scaleTr.play();
for (int i = 0; i < circleCount; i++) {
display.getChildren().addAll(group);
}
}
}
例外情况是“java.lang.IllegalArgumentException:子项:已添加重复的子项:parent = Pane@...”。这是因为您一遍又一遍地添加同一个组。修改代码如下解决问题:
private ScaleTransition createTransition(int speed) {
double widthRand = ((Math.random() * (800 - 0)) + 0);
double lengthRand = ((Math.random() * (600 - 0)) + 0);
double radiusRand = Math.floor(Math.random() * (100 - 10 + 1) + 10);
Circle circle = new Circle(widthRand, lengthRand, radiusRand);
circle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
ScaleTransition scaleTr = new ScaleTransition();
scaleTr.setDuration(Duration.millis(speed));
scaleTr.setFromX(1);
scaleTr.setFromY(1);
scaleTr.setToX(0.001);
scaleTr.setToY(0.001);
scaleTr.setCycleCount(3);
scaleTr.setAutoReverse(true);
group = new Group(circle);
scaleTr.setNode(group);
return scaleTr;
}
private class buttonEvent implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
String countCircles = numOfCircles.getText();
int circleCount = Integer.parseInt(countCircles);
String countDuration = duration.getText();
int speed = Integer.parseInt(countDuration);
for( int i = 0; i < circleCount; i++){
ScaleTransition scaleTr = createTransition(speed);
display.getChildren().addAll(scaleTr.getNode());
scaleTr.play();
}
}
}
我正在编写一个代码来获取用户输入的整数,它将获取窗格周围具有随机大小和位置的圆圈数。例如,如果用户想要 5 个圆圈,他们输入 5,将显示 5 个圆圈并设置动画。该程序的动画部分有效。问题是,一旦我将它们实现到 for 循环中,圆圈现在就不会出现(因此我可以根据用户的输入有多个圆圈。每当我按下按钮显示动画圆圈时,只有一个出现,然后IDE 底部的框给我随机错误。我不确定如何解决这个问题。
这是我的代码:
private class buttonEvent implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
String countCircles = numOfCircles.getText();
int circleCount = Integer.parseInt(countCircles);
String countDuration = duration.getText();
int speed = Integer.parseInt(countDuration);
double widthRand = ((Math.random() * (800 - 0)) + 0);
double lengthRand = ((Math.random() * (600 - 0)) + 0);
double radiusRand = Math.floor(Math.random() * (100 - 10 + 1) + 10);
Circle circle = new Circle(widthRand, lengthRand, radiusRand);
circle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
ScaleTransition scaleTr = new ScaleTransition();
scaleTr.setDuration(Duration.millis(speed));
scaleTr.setFromX(1);
scaleTr.setFromY(1);
scaleTr.setToX(0.001);
scaleTr.setToY(0.001);
scaleTr.setCycleCount(3);
scaleTr.setAutoReverse(true);
group = new GroupLayout.Group(circle);
scaleTr.setNode(group);
scaleTr.play();
for (int i = 0; i < circleCount; i++) {
display.getChildren().addAll(group);
}
}
}
例外情况是“java.lang.IllegalArgumentException:子项:已添加重复的子项:parent = Pane@...”。这是因为您一遍又一遍地添加同一个组。修改代码如下解决问题:
private ScaleTransition createTransition(int speed) {
double widthRand = ((Math.random() * (800 - 0)) + 0);
double lengthRand = ((Math.random() * (600 - 0)) + 0);
double radiusRand = Math.floor(Math.random() * (100 - 10 + 1) + 10);
Circle circle = new Circle(widthRand, lengthRand, radiusRand);
circle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
ScaleTransition scaleTr = new ScaleTransition();
scaleTr.setDuration(Duration.millis(speed));
scaleTr.setFromX(1);
scaleTr.setFromY(1);
scaleTr.setToX(0.001);
scaleTr.setToY(0.001);
scaleTr.setCycleCount(3);
scaleTr.setAutoReverse(true);
group = new Group(circle);
scaleTr.setNode(group);
return scaleTr;
}
private class buttonEvent implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
String countCircles = numOfCircles.getText();
int circleCount = Integer.parseInt(countCircles);
String countDuration = duration.getText();
int speed = Integer.parseInt(countDuration);
for( int i = 0; i < circleCount; i++){
ScaleTransition scaleTr = createTransition(speed);
display.getChildren().addAll(scaleTr.getNode());
scaleTr.play();
}
}
}