PLEASE READ CAREFULLY STEP BY STEP I NEED IT TO STUDY FOR FINALS CIT239 Java Pro
ID: 3832311 • Letter: P
Question
PLEASE READ CAREFULLY STEP BY STEP I NEED IT TO STUDY FOR FINALS
CIT239 Java Programming Lab Exercise 15c Pendulum This lab exercise is adapted from Programming Exercise 15.31 Due Date You must demonstrate the solution for this lab exercise to the instructor by Sunday, April 16, 2017, in order to receive credit for this work. Pendulum Write a JavaFX application that animates a pendulum swinging, as shown in the images below. pendulum Pendulum Let the user pause/start the animation by clicking a mouse button. Let the user increase the animation speed with the UP-amow key, and decrease the animation speed with the DOWN-arrow key. This lab may take a bit of trial-and-error to get right. Do not be afraid to experiment! Design Suggestion Pendulum Class Create a Pendulum class which extends Application. In the start method of the Pendulum class, create a PendulumPane object Also in the start method, create a Timeline object as shown below: animation new Timeline new KeyFrame (Duration .millis (100 e pane next animation setCycleCount (Timeline. INDEFINITE) animation play i Start animation CIT239-SU Lab15c Pendulum 20170409. docx 4/8/2017 11:54 AMExplanation / Answer
PendulumPane.java
import javafx.scene.layout.Pane;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.scene.shape.Line;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.util.Duration;
public class PendulumPane extends Pane{
/** Header circle */
private double radiusOne=10,circleOneCenterX=250,circleOneCenterY=20;
private Circle circleOne=new Circle (circleOneCenterX,circleOneCenterY,radiusOne);
/** Create circle two and set its properties*/
private double circleTwoCenterX=circleOneCenterX;
private double circleTwoCenterY=250;
private double radiusTwo=30;
private Circle circleTwo=new Circle (circleTwoCenterX,circleTwoCenterY,radiusTwo);
/**line which represent pendulum radius and set its properties */
Line line=new Line (circleOneCenterX,circleOneCenterY,circleTwoCenterX,circleTwoCenterY);
/** Pendulum radius*/
private double pendulumRadius=circleOneCenterY+circleTwoCenterY;
/** Pendulum angle*/
public double angle=(Math.PI/2);
/** Angle distance*/
public double dAngle=0.1;
/** Animation*/
private Timeline animation;
//default constructor
public PendulumPane ()
{
circleOne.setFill(Color.RED);
line.setStroke (Color.RED);
circleTwo.setFill(Color.RED);
getChildren().addAll(circleOne,line,circleTwo);
//Set style
setStyle("-fx-background-color:black;");
//Create animation
animation =new Timeline(new KeyFrame (Duration.millis(100),e->movePendulum()));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
}
public void play()
{
animation.play();
}
public void pause()
{
animation.pause();
}
public void increaseSpeed()
{
animation.setRate (animation.getRate()+0.1);
}
public void decreaseSpeed()
{
animation.setRate(animation.getRate ()>0 ? animation.getRate()-0.1:animation.getRate());
}
public void movePendulum()
{
if (angle<=Math.PI/4 || angle>=(3*Math.PI)/4)
dAngle*=-1;
angle-=dAngle;
circleTwoCenterX=circleOneCenterX+pendulumRadius*Math.cos(angle);
circleTwoCenterY=circleOneCenterY+pendulumRadius*Math.sin(angle);
circleTwo.setCenterX(circleTwoCenterX);
circleTwo.setCenterY(circleTwoCenterY);
line.setEndX(circleTwoCenterX);
line.setEndY(circleTwoCenterY);
}
}
Pendulum.java
/**
* A JavaFX application that animates a pendulum swinging.
* The user pause and start the animation by clicking a mouse button
* The user increase the animation speed with UP-arrow key, and decrease the animation speed with the DOWN-arrow key.
*/
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.input.KeyCode;
public class Pendulum extends Application {
@Override
public void start(Stage primaryStage) {
//Create a PendulumPane object
PendulumPane pp=new PendulumPane ();
//Register the event handlers
//The user pause and start the animation by clicking a mouse button
pp.setOnMousePressed(e->pp.pause());
pp.setOnMouseReleased(e->pp.play());
//Increase the animation speed with UP-arrow key, and decrease the animation speed with the DOWN-arrow key.
pp.setOnKeyPressed(e->{
if(e.getCode()==KeyCode.UP)
pp.increaseSpeed();
else if (e.getCode()==KeyCode.DOWN)
pp.decreaseSpeed();
});
//Create a scene and place it in the stage
Scene scene=new Scene (pp,500,500);
primaryStage.setTitle ("Pendulum");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.