CS1103 Programming Assignment Unit 7 - A stopwatch

Questions

Please write a custom component that acts as a simple stop-watch. Specifically, when the user clicks on it, this component starts timing; when the user clicks again, it displays the time between the two clicks; clicking a third time starts another timer, etc; while it is timing, the label displays the elapsed time since the timer was started.

1. Programs

StopWatchLabel.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.awt.event.*;
import javax.swing.*;
/**
* A custom component that acts as a simple stop-watch.
* When the user clicks on it, this component starts timing.
* When the user clicks again, it displays the time between the two clicks.
* Clicking a third time starts another timer, etc.
* While it is timing, the label displays the elapsed time since the timer was started.
*/
public class StopWatchLabel extends JLabel implements MouseListener, ActionListener {
private long startTime; // Start time of timer. Time is measured in milliseconds.
private boolean running; // True when the timer is running.
private Timer timer; // Create a timer

/**
* Constructor sets initial text on the label to "Click to start timer."
* And sets up a mouse listener, so the label can respond to clicks.
*/
public StopWatchLabel() {
super(" Click to start timer. ", JLabel.CENTER);
addMouseListener(this);
}
/**
* Tells whether the timer is currently running.
*/
public boolean isRunning() {
return running;
}

/**
* Compute the elapsed time since the timer was started.
*/
public void actionPerformed(ActionEvent evt) {
long time = (System.currentTimeMillis() - startTime) / 1000;
setText("Timing: " + time + " seconds");
}

/**
* React when the user presses the mouse by starting or stopping the timer,
* and displaying the elapsed time that is shown on the label.
*/
public void mousePressed(MouseEvent evt) {
if (running == false) {
// Record the time and create the timer when the stopwatch is started
running = true;
startTime = evt.getWhen(); // Time when mouse was clicked.
if (timer == null) {
// Use a delay of 200 milliseconds for the timer
timer = new Timer(200,this);
// Call the start() to run the timer
timer.start();
}
else
timer.restart();
}
else {
// Stop the timer, and compute the elapsed time since the timer was started and display it.
timer.stop();
running = false;
long endTime = evt.getWhen();
double seconds = (endTime - startTime) / 1000.0;
setText("Time: " + seconds + " seconds");
}
}
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
}

StopWatchLabelTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import javax.swing.*;
import java.awt.*;

/**
* Tests the custom component classes StopWatchLabel by adding them both to a panel.
*/
public class StopWatchLabelTest extends JPanel {

private StopWatchLabel timer;

/**
* The StopWatchLabel define components.
* But components don’t stand on their own. We have to add them to a panel.
*/
public StopWatchLabelTest() {
// Uses a FlowLayout as its layout manager
setLayout(new FlowLayout());

timer = new StopWatchLabel();
timer.setBackground(Color.white);
timer.setForeground(Color.blue);
timer.setOpaque(true);
timer.setFont(new Font("Serif", Font.PLAIN, 20));
add(timer);
}

/**
* The main routine simply opens a window that shows a StopWatchLabelTest panel.
*/
public static void main(String[] args) {
JFrame window = new JFrame("StopWatchLabelTest");
StopWatchLabelTest content = new StopWatchLabelTest();
window.setContentPane(content);
window.setSize(350, 90);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation((screenSize.width - window.getWidth()) / 2, (screenSize.height - window.getHeight()) / 2);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
}
}

2. Outputs

Before timing:
Before timing

During timing:
During timing_0
During timing_1
During timing_2
During timing_3

After timing:
After timing

​


This is the ending, thanks for reading.