gpt4 book ai didi

java - (JavaFX) 将 double 值显示为进度条

转载 作者:行者123 更新时间:2023-12-02 02:42:13 25 4
gpt4 key购买 nike

我在让进度条显示 double 值时遇到问题。我希望随着物体高度的下降而减少燃料棒,并随着按下按钮而减少燃料棒。

    @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

这是计算高度和燃料的地方:

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;
}

}

最佳答案

在 JavaFX 中,进度条值应 >=0 & <=1

意味着如果你想要 65% 的进度,那么你必须编写代码 progressBar.setValue(0.65);

假设 progressBar 是一个有效的初始化 FXML 进度条。

关于java - (JavaFX) 将 double 值显示为进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45266744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com