Java programming. Please, could you add the comment so I can follow and understa
ID: 3666765 • Letter: J
Question
Java programming.
Please, could you add the comment so I can follow and understand. Thanks
Create a program with JavaFX that displays a bouncing graphics (an image of a ball or the head of Darth Vader or anything else). The object should bounce around in the window. There should also be a button in the win- dow that releases another object (unlimited number should be possible to have in the window, but only one new object should be released for each press of the button). The objects must also bounce on each other when the program is running.
Explanation / Answer
package learn.javafx; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Bounds; import javafx.scene.Scene; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import javafx.util.Duration; public class GamePractice extends Application { public static Circle circle; public static Pane canvas; @Override public void start(final Stage primaryStage) { canvas = new Pane(); final Scene scene = new Scene(canvas, 800, 600); primaryStage.setTitle("Game"); primaryStage.setScene(scene); primaryStage.show(); circle = new Circle(15, Color.BLUE); circle.relocate(100, 100); canvas.getChildren().addAll(circle); final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler() { double deltaX = 3; double deltaY = 3; @Override public void handle(final ActionEvent t) { circle.setLayoutX(circle.getLayoutX() + deltaX); circle.setLayoutY(circle.getLayoutY() + deltaY); final Bounds bounds = canvas.getBoundsInLocal(); final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); final boolean atLeftBorder = circle.getLayoutX() = (bounds.getMaxY() - circle.getRadius()); final boolean atTopBorder = circle.getLayoutY()Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.