-1

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

}

Frosty619
  • 1,381
  • 4
  • 23
  • 33
  • If you have two distinct questions, ask two distinct questions. It's not polite to ask two in one, as that conflates the actual problem(s) you're having. – Makoto Aug 28 '15 at 22:03
  • I don't want to flood you with another answer, but the other answers don't seem to address your question about Circle properly. Assuming you are calling javafx.scene.shape.Circle(double, double, double), then no, they are not equivalent. The call to that Circle constructor is a bit more complicated than that. You can trace through the code here: http://grepcode.com/file/repo1.maven.org/maven2/net.java.openjfx.backport/openjfx-78-backport/1.8.0-ea-b96.1/javafx/scene/shape/Circle.java#Circle.%3Cinit%3E%28double%2Cdouble%2Cdouble%29 – searchengine27 Aug 28 '15 at 22:15
  • Thank you for the response. My apologies, I will make sure to ask one question at a time from now. – Frosty619 Aug 29 '15 at 02:10

3 Answers3

0

1) Regarding this:

protected void moveBall() {
    for (Node node: this.getChildren()) {
        Ball ball = (Ball)node;

The for loop in this case is more of a foreach, where it goes through each node in the children. For each node, a Ball object is being created by casting the node to Ball. In plainer english:

For each node in the set of children this object has, cast each to become a Ball.

Casting essentially is saying that one data type (in this case Node) is to be treated as another (in this case, Ball). For more information on what casting is, reference: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

2) super() calls the parent constructor, which in this case would be Circle. Since there is no constructor attached, it would only be guesswork, but it's logical to believe that the Circle constructor would do the functions you specified. A good reference for super() is here: super() in Java

Hope this helped!

Community
  • 1
  • 1
ihgann
  • 505
  • 3
  • 11
0

This decodes to:

Get all children of this using this.getChildren() assign the first one of them to node.

Then the next line shall be run, this line creates a local variable ball which is equal to the node, after it is casted to a Ball.

Do other code.

Repeat until no more children are left.

As for the circle, as you are calling super it may not be that simple, the super constructor may set variables, or it may do some other logic. So it would be safer to call the constructor, in my opinion.

  • Culip
Curlip
  • 395
  • 1
  • 3
  • 15
  • "Get all children of this using this.getChildren() assign of them to node." This is wrong. The for loop iterates through each child one at a time, and individually sets 'node' to a specific child (in an order that is determined by the iterator), performs the logic within the block following the for loop, and when it is completed, assigns the next child to the variable 'node'. – searchengine27 Aug 28 '15 at 22:06
  • @searchengine27 That's what I said... Opps I accidentally left out the word one... – Curlip Aug 28 '15 at 22:08
0

Presuming correct syntax on the following:

protected void moveBall() {
    for (Node node:this.getChildren()) {
        Ball ball = (Ball)node;
    }
}

This can be read as:

"For each element in the iterable/array from the result of a call to getChildren(), referenced here as node and of type Node, assign the manual cast of node to a type Ball to a variable of type Ball called ball."

Second:

Without seeing the declaration for Circle it's impossible to say. However, if the declaration in Circle actually does accept those values carte-blanche, and does assign them to the those specific parent fields, then yes, it would be equivalent.

Makoto
  • 104,088
  • 27
  • 192
  • 230