Reputation: 13
I'm having an issue getting my progressBar to display a double value. I'd like the bar to decrease as the height of an object goes down and the fuel bar to decrease as a button is being held down.
@FXML
private ProgressBar progFuel; //Progress Bar to represent the remaining
fuel
@FXML
private ProgressBar progHeight; //Progress Bar to represent the altitude
of the ship
private void onTimer() {
/**
* Method to calculate fuel,velocity,gravity,height sets the initial
* height of the lander's height sets the initial fuel of the lander to 100%
*/
progHeight.setProgress(INITIAL_HEIGHT);
progFuel.setProgress(1.0);
pe.nextStep(btnThrust.isPressed()); //calls the nextStep method from PhysicsEngine; passes pressed state of Thrust Button
progFuel.setProgress(pe.getFuel());
progHeight.setProgress(pe.getHeight());
/**
* Sets the string value of velocity and height to corresponding TextFields
*/
_txtVelocity.setText(String.valueOf(pe.getVelocity()));
_txtHeight.setText(String.valueOf(pe.getHeight()));
progFuel.setStyle("-fx-accent: green"); //sets color of Fuel Progress Bar to green
This is where the calculations for height and fuel are made:
public class PhysicsEngine {
public static final double GRAVITY = -1.622; // The acceleration due to gravity on the moon in meters per second per second (m/s/s), negative means down
public static final double THRUST_STRENGTH = 5.0; // The strength of the lander's rocket in meters per second per second (m/s/s)
public static final double INITIAL_HEIGHT = 500; // The initial height in meters (m). The real Apollo 11 lunar module started powered descent at height 11853 m.
public static final double SAFE_LANDING_SPEED = 5.0; // The safe landing speed below which we don't crash in meters per second (m/s)
public static final double TIME_STEP = 0.1; // The size of each step in the simulation, in seconds (s)
private double _height; // The lander's current height in meters, height of zero means the simulation is stopped
private double _vel; // The lander's current velocity in meters per second, negative means moving down
private double _fuel; // The amount of fuel remaining as a percent, where 100 means full and 0 means empty
private double _elapsedTime; // The simulation elapsed time in seconds
/**
* Getter for height.
* @return The lander's height in meters (m).
*/
public double getHeight() {
return _height;
}
/**
* Getter for velocity.
* @return The lander's velocity in meters per second (m/s), where positive means up and negative means down.
*/
public double getVelocity() {
return _vel;
}
/**
* Getter for fuel amount.
* @return The amount of fuel remaining as a percent.
*/
public double getFuel() {
return _fuel;
}
/**
* Getter for elapsed time.
* @return The simulation elapsed time in seconds.
*/
public double getElapsedTime() {
return _elapsedTime;
}
/**
* Starts the simulation by setting initial conditions.
*/
public void start() {
_elapsedTime = 0;
_vel = 0;
_height = INITIAL_HEIGHT;
_fuel = 100;
}
/**
* Calculates the lander's height and velocity for the next step in the simulation.
* In each step this method updates
* - The lunar lander's velocity, based on engine thrust and gravity
* - The lander's height, based on velocity
* - The lander's fuel amount, based on thrust
* @param thrust true means the lander's rocket is firing, false means not firing.
* @return true if the simulation is finished (lander has landed), false if simulation is still running.
*/
public boolean nextStep(boolean thrust) {
boolean result = false; // The value to return from this method, false unless set otherwise
// Return immediately if simulation is already stopped
if (_height <= 0) {
return result;
}
// If there is fuel left then apply thrust from the engine and decrease the fuel amount
double actualThrust = 0;
if (_fuel > 0 && thrust) {
actualThrust = THRUST_STRENGTH;
_fuel -= actualThrust/7.5; // Decrease the amount of fuel, engine is thrusting
if (_fuel < 0) {
_fuel = 0; // Make sure fuel amount doesn't go negative
}
}
// Update the lunar lander's velocity and height. Also update the simulation clock (elapsed time).
_vel += (GRAVITY + actualThrust) * TIME_STEP;
_height += _vel * TIME_STEP;
_elapsedTime += TIME_STEP;
// Stop the simulation when height becomes zero or negative
if (_height <= 0) {
_height = 0; // Make sure height does not go negative
result = true;
}
return result;
}
}
Upvotes: 0
Views: 420
Reputation: 565
In JavaFX, progressbar value should be >=0 & <=1
Means if you want 65% progress then you have to write code progressBar.setValue(0.65);
Assume progressBar
is a valid initialized FXML progressbar.
Upvotes: 3