簡體   English   中英

(JavaFX)將double值顯示為progressBar

[英](JavaFX) Display double value as a progressBar

我在獲取progressBar以顯示雙精度值時遇到問題。 我希望隨着對象高度的下降,條形減小,而按住按鈕時,燃料條形減小。

    @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中,progressbar值應為> = 0和<= 1

意味着如果您想要65%的進度,則必須編寫代碼progressBar.setValue(0.65);

假定progressBar是一個有效的初始化FXML progressbar。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM