Wednesday 28 December 2016

Chapter 16 Exercise 20, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

16.20 (Count-up stopwatch)
Write a program that simulates a stopwatch,
as shown in Figure 16.45a. When the user
clicks the Start button, the button’s lbl
is changed to Pause, as shown in Figure 16.45b.
When the user clicks the Pause button, the button’s
lbl is changed to Resume, as shown in Figure 16.45c. The
Clear button resets the count to 0 and resets the button’s lbl to Start.


public class StopWatch {

    private long mStartTime;
    private long mEndTime;
    private long mElapsedPause;
    private int mLastSecond = 0;

    private boolean mIsOn;
    private boolean mIsPaused;

    private int mSeconds;
    private int mMinutes;
    private int mHours;

    public StopWatch() {
        mStartTime = System.currentTimeMillis();
    }

    public long getStartTime() {
        return mStartTime;
    }

    public long getEndTime() {
        return mEndTime;
    }

    public void start() {
        mIsOn = true;
        mStartTime = System.currentTimeMillis();
    }

    public void stop(){
        mEndTime = System.currentTimeMillis();
        mIsOn = false;
    }

    public long getElapsedTime() {
        return mEndTime - mStartTime;
    }

    public long peek() {
        return System.currentTimeMillis() - mStartTime;
    }
    public void pause() {
        mIsPaused = true;
        mElapsedPause = System.currentTimeMillis() - mStartTime;
    }

    public void resume() {
        mIsPaused = false;
        mStartTime = System.currentTimeMillis() - mElapsedPause;
    }

    public boolean isOn() {
        return mIsOn;
    }
    public boolean nextSecond() {
        updateTime();
        if (mSeconds != mLastSecond) {
            mLastSecond = mSeconds;
            return true;
        } else {
            return false;
        }
    }
    public boolean nextFiveSeconds() {
        updateTime();
        return mSeconds % 5 == 0;
    }

    public int getHour(){
        updateTime();
        return mHours;
    }

    public int getMinute(){
        updateTime();
        return mMinutes;
    }

    public int getSeconds(){
        updateTime();
        return mSeconds;
    }

    private void updateTime() {

        long currentTime = peek() / 1000;
        mSeconds = (int)(currentTime % 60);
        currentTime = currentTime / 60;

        mMinutes = (int) (currentTime % 60);
        currentTime = currentTime / 60;

        mHours = (int)(currentTime % 24);

    }

    @Override
    public String toString() {

        updateTime();
        String hours = getTimeFormat(mHours);
        String minutes = getTimeFormat(mMinutes);
        String seconds = getTimeFormat(mSeconds);

        return hours + ":" + minutes + ":" + seconds;
    }

    private String getTimeFormat(int time) {
        return (time > 9) ? time + "" : "0" + time;
    }

    public void reset(){
        stop();
        mHours = 0;
        mMinutes = 0;
        mSeconds = 0;
        mStartTime = 0;
        mEndTime = 0;
    }


    public boolean isPaused() {
        return mIsPaused;
    }
}


import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Exercise_20 extends Application {

    StopWatch stopWatch = new StopWatch();
    Label mLabel = new Label("00:00:00");
    @Override
    public void start(Stage primaryStage) {


        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000), e-> {
            mLabel.setText(stopWatch.toString());
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);

        // bottom pane
        Button btStartPause = new Button("Start");
        btStartPause.setOnAction(event -> {
            btStartPause.setText((btStartPause.getText().equals("Pause") ? "Start" : "Pause"));
            if (!stopWatch.isOn()) {
                stopWatch.start();
                timeline.play();
            } else
            if (stopWatch.isPaused()) {
                timeline.play();
                stopWatch.resume();
            } else {
                stopWatch.pause();
                timeline.pause();
            }
        });

        Button btClear = new Button("Clear");
        btClear.setOnAction(event -> {
            stopWatch.stop();
            timeline.stop();
            btStartPause.setText("Start");
            mLabel.setText("00:00:00");
        });
        HBox bottomPane = new HBox(btStartPause, btClear);
        bottomPane.setAlignment(Pos.CENTER);
        bottomPane.setSpacing(10);
        bottomPane.setPadding(new Insets(10));
        mLabel.setFont(Font.font(50));

        StackPane centerPane = new StackPane(mLabel);
        centerPane.setPadding(new Insets(10));

        BorderPane borderPane = new BorderPane(centerPane);
        borderPane.setBottom(bottomPane);
        primaryStage.setScene(new Scene(borderPane));
        primaryStage.setTitle("Stopwatch");
        primaryStage.show();

    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

No comments :

Post a Comment