For the sake of completeness, I have provided the entire code below.
Please help me translate the following in english:
protected void moveBall() {
for (Node node:this.getChildren()) {
Ball ball = (Ball)node;
//some other code.
Here is my (incorrect) interpretation:
Get a reference to Node, and set the reference equal to all the nodes in this class.
Get a reference to Ball, and set it equal to node, which is to be cast as a Ball.
Besides not knowing how to interpret the aforementioned code, I also do not really understand what it does.
The second question is regarding the circle class :
class Ball extends Circle {
private double dx = 1;
private double dy = 1;
public Ball (double x, double y, double radius, Color color) {
super(x,y,radius);
setFill(color);
}
}
Would invoking the super method be equivalent to the following:
this.x = x;
this.y = y;
this.radius = radius;
why or why not?
public class MultipleBounceBalls extends Application {
public void start(Stage primaryStage) {
MultipleBallsPane ballsPane = new MultipleBallsPane();
Button btnAddBall = new Button("+");
Button btnRemoveBall = new Button("-");
HBox hBox = new HBox();
hBox.getChildren().addAll(btnAddBall, btnRemoveBall);
hBox.setAlignment(Pos.CENTER);
//add or remove ball
btnAddBall.setOnMousePressed(e -> ballsPane.add());
btnRemoveBall.setOnMousePressed(e -> ballsPane.subtract());
//resume and pause animation
ballsPane.setOnMousePressed(e -> ballsPane.pause());
ballsPane.setOnMouseReleased(e -> ballsPane.play());
//scroll bar to control animation speed
ScrollBar sbSpeed = new ScrollBar();
sbSpeed.setMax(20);
sbSpeed.setMin(10);
ballsPane.rateProperty().bind(sbSpeed.valueProperty());
HBox hBox2 = new HBox();
hBox2.getChildren().addAll(sbSpeed);
hBox2.setAlignment(Pos.CENTER);
BorderPane pane = new BorderPane();
pane.setTop(hBox2);
pane.setCenter(ballsPane);
pane.setBottom(hBox);
Scene scene = new Scene(pane,250, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
// inner class MultipleBallsPane
private class MultipleBallsPane extends Pane {
private Timeline animation;
public MultipleBallsPane() {
//create an animation for moving the ball
animation = new Timeline(
new KeyFrame(Duration.millis(50), e->moveBall()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}
public void add() {
Color color = new Color(Math.random(),
Math.random(), Math.random(), 0.5);
getChildren().add(new Ball(30,30,20, color));
}
public void subtract() {
if (getChildren().size() > 0 ) {
getChildren().remove(getChildren().size() - 1);
}
}
public void play() {
animation.play();
}
public void pause() {
animation.pause();
}
public void increaseSpeed() {
animation.setRate(animation.getRate() + 0.5);
}
public void decreaseSpeed() {
if (animation.getRate() > 0) {
animation.setRate(animation.getRate() - 0.5);
}
}
public DoubleProperty rateProperty() {
return animation.rateProperty();
}
protected void moveBall() {
for (Node node:this.getChildren()) {
Ball ball = (Ball)node;
//check boundaries
if (ball.getCenterX() < ball.getRadius() ||
ball.getCenterX() > getWidth() - ball.getRadius()) {
ball.dx*= -1;
}
if (ball.getCenterY() < ball.getRadius() ||
ball.getCenterY() >getHeight() - ball.getRadius()) {
ball.dy*= -1;
}
ball.setCenterX(ball.dx + ball.getCenterX());
ball.setCenterY(ball.dy + ball.getCenterY());
}
} // end of method
class Ball extends Circle {
private double dx = 1;
private double dy = 1;
public Ball (double x, double y, double radius, Color color) {
super(x,y,radius);
setFill(color);
}
}
} // end of MultipleBallsPane class
}