15.25 (Animation: ball on curve) Write a program that animates a ball moving alo
ID: 3694795 • Letter: 1
Question
15.25 (Animation: ball on curve) Write a program that animates a ball moving along
a sine curve, as shown in Figure 15.32. When the ball gets to the right border,
it starts over from the left. Enable the user to resume/pause the animation with
a click on the left/right mouse button.
Exerrisel 5-25 Exercise15 25 FIGURE 15.32 The program animates a ball traveling along a sine curve.Explanation / Answer
import javafx.animation.PathTransition; import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.input.MouseButton; import javafx.scene.layout.Pane; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.scene.shape.Polyline; import javafx.scene.text.Font; import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.util.Duration; public class ball_animation extends Application { private static double WIDTH = 600; private static double HEIGHT = 400; private static double CENTER_X = WIDTH / 2; private static double CENTER_Y = HEIGHT / 2; @Override public void start(Stage primaryStage) throws Exception { Pane pane = new Pane(); Polyline xAxishere = new Polyline(); todrawXAxisfunction(pane, xAxishere); Polyline yAxishere = new Polyline(); todrawYAxisfunction(pane, yAxishere); // simulate the ball animation on the sin curve String[] pi = new String[] {"-3u03c0", "-2u03c0", "-u03c0", "0" , "u03c0", "2u03c0", "3u03c0"}; int index = 0; Polyline polyline1 = new Polyline(); ObservableList list10 = polyline1.getPoints(); for (int x = -170; x = 99.9 && y < 100.01) { Text textnew = new Text(x + CENTER_X, CENTER_Y / 0.9, pi[index++]); pane.getChildren().add(textnew); } } Circle point = new Circle(list.get(0),list10.get(1),10); PathTransition path = new PathTransition(Duration.millis(4000), polyline1, point); path.setCycleCount(PathTransition.INDEFINITE); pane.getChildren().addAll(xAxishere, yAxishere, polyline1, point); pane.setOnMouseClicked(e-> { if (e.getButton() == MouseButton.PRIMARY) { path.play(); } else if (e.getButton() == MouseButton.SECONDARY) { path.pause(); } }); primaryStage.setScene(new Scene(pane, WIDTH, HEIGHT)); primaryStage.setTitle(" now lets Plot the sine and cosine functions"); primaryStage.show(); } // to create x axis private static void todrawXAxisfunction(Pane pane, Polyline xAxishere) { ObservableList createxAxisList = xAxishere.getPoints(); double limit = WIDTH * 0.95; for (double x = 0; xRelated 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.