Implement an MVC JavaFX GUI for a physics simulator which simulates a ball thrown into the air. Your program will consist of a main application class (Main.java), a Main.fxml and, MainController.java. It will also include a physics engine class (PhysicsEngine.java) which is provided for you.

Physics Engine

The physics engine is provided for you, download PhysicsEngine.java. It has fields to keep track of the ball's height and velocity (speed), and these two methods:

a) start - This starts the simulation by "throwing" the simulated ball into the air at a given velocity (speed) in meters/second.

b) nextStep - Calculates the next step in the simulation, i.e. where the ball will be in 0.1 seconds (100 milliseconds). Returns the ball's current height in meters.

public class PhysicsEngine {

public static final double GRAVITY = -9.8; // The acceleration due to gravity in meters per second
public static final double TIME_STEP = 0.1; // The size of each step in the simulation, in seconds

protected double _height = 0; // The ball's current height in meters
protected double _vel = 0; // The ball's current velocity in meters per second

/**
* Sets initial conditions to start the simulation.
* @param vel The speed the initial velocity in meters per second.
*/
public void start(double vel) {
_vel = vel;
_height = 0;
}

/**
* Calculate the ball's height and velocity for the next step in the simulation.
* @param time The amount of timer between steps in seconds.
* @return The current height in meters.
*/
public double nextStep() {
// Stop simulation when height becomes zero or negative on the way down
if (_height <= 0 && _vel <= 0) {
return 0;
}

_vel += GRAVITY * TIME_STEP;
_height += _vel * TIME_STEP;

if (_height < 0) {
return 0;
}

return _height;
}
}

GUI

The GUI window lets the user start and watch the progress of the simulation. A possible design is shown here. The Start button calls the physics engine start method with an initial velocity entered by the user. A non-editable text field shows the height of the ball as the simulation runs.

Figure: see image.

Your GUI should use a timer to continually update the simulation and the information in the window. The JavaFX Timeline class (javafx.animation.Timeline) can be used to trigger an event handler at regular time intervals. Use the following code in your MainController class's initialize method to initialize a timer which calls an event handler every 100 milliseconds, or ten times per second. _timer should be a field variable of type TimeLine.

_timer = new Timeline(new KeyFrame(Duration.millis(100), new EventHandler< ActionEvent >() {
public void handle(ActionEvent event) {
onTimer();
}
}));

_timer.setCycleCount(Timeline.INDEFINITE);
_timer.play();

In your onTimer method, which will be in the MainController class, you should call the physics engine nextStep method, then display the current height of the ball in the GUI.

Add a ProgressBar to your GUI to show the height of the ball graphically. To update the progress bar use its setProgress method. You can pass the ball height divided by 100 for a reasonable height display. Rotate your progress bar to be vertical, not horizontal.

Figure: see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.