Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The task: We will simulate a pendulum that swings as these pictures show: Pendulum in left position Pendulum in the down position Pendulum in position
The task: We will simulate a pendulum that swings as these pictures show: Pendulum in left position Pendulum in the down position Pendulum in position right The pendulum starts in the left position at a specified angle, turns to the right and turns when it reaches a specified angle to the right. 0 degrees is East, 180 degrees is West. The pendulum should swing from 120 degrees to 60 degrees. To create the animation, we create a Timeline object like this: Timeline animation = new Timeline (new KeyFrame (Duration.millis (100), e -> {pane.next ();})); This will cause the method next () in the object pane to be called every tenth of a second. next () has the task of drawing the "thread" in which the ball hangs, as well as the pendulum ball itself (the "suspension" is drawn in the same place each time). See examples in the book of how to pause / start the animation. The pendulum must be described in a class PendulumPane that inherits Pane. You need two Circle objects and a Line object. Each time next () is called from the animation object (Timeline), move the pendulum one degree, but never past 60/120 degrees. If xCeil and yCeil are the coordinates of the center suspension point (the small sphere at the top). and xBall and yBall are the center of the pendulum ball, so we calculate new xBall and yBall as follows: double xBall = xCeil + pendulumRadius * Math.cos (Math.toRadians (angle)); double yBall = yCeil + pendulumRadius * Math.sin (Math.toRadians (angle)); Now we can set new values on all data for the two circles and the line (line start, line end, center and radius suspension, center and radius pendulum ball). NB: remember to add the two circles and the line to the panel's children list, for example in PendulumPane's constructor: public PendulumPane () { getChildren (). addAll (line, c1, c2); next (); } Our pendulum must also have the following properties: - up arrow should increase speed, down arrow should decrease speed (animation.setRate ()) - mouse click in PendulumPane should stop / start the animation Code skeleton for the start method:
public void start(Stage primaryStage) { PendulumPane pane = new PendulumPane(); // You have to make the class PendulumPane Scene scene = new Scene(pane, 300, 200); primaryStage.setTitle("Pendulum"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage Timeline animation = new Timeline(new KeyFrame(Duration.millis(100), e -> {pane.next();})); animation.setCycleCount(Timeline.INDEFINITE); animation.play(); // Start animation pane.setOnMouseClicked(e -> { //your code } }); pane.requestFocus(); pane.setOnKeyPressed(e -> { // your code }); } class PendulumPane extends Pane {// your code}Pendulum Pendulum Pendulum
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started