package q3;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* <p>
* This program displays a stop watch that will start and stop. It will also
* reset upon user input.
* </p>
*
* @author
* @version 1.0
*/
public class StopWatch extends JFrame {
/** Size of window X. */
private static final int MAX_X = 300;
/** Size of window Y. */
private static final int MAX_Y = 150;
/** Counter font size. */
private static final int TIME_FONT = 32;
/** Grid layout position 3. */
private static final int THREE = 3;
/** Milliseconds. */
private static final int MILSECS = 100;
/** Minutes and seconds. */
private static final int SIXTY = 60;
/** Label to show the time. */
private JLabel time;
/** Delay. */
private int delay = 10;
/** Timer object to create the time. */
private Timer timer = new Timer();
public StopWatch() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(new StopWatchPanel());
setSize(MAX_X, MAX_Y);
setVisible(true);
}
private class StopWatchPanel extends JPanel {
TimerTask mtt = new ATimerTask();
/** Hours. */
private int hours;
/** Minutes. */
private int minutes;
/** Seconds. */
private int seconds;
/** Milliseconds. */
private int milliseconds;
/**
* If start is pressed more than once. Without this the schedule will
* not stop iterating.
*/
private int count;
/**
* Creates the stop watch panel.
*/
public StopWatchPanel() {
setLayout(new BorderLayout());
JLabel stopwatch = new JLabel("Stop Watch");
stopwatch.setHorizontalAlignment(JLabel.CENTER);
add(stopwatch, BorderLayout.NORTH);
JPanel display = new JPanel();
display.setLayout(new GridLayout(1, 1));
time = new JLabel("00:00:00.00");
time.setFont(new Font("Serif", Font.BOLD, TIME_FONT));
time.setHorizontalAlignment(JLabel.CENTER);
display.add(time);
add(display, BorderLayout.CENTER);
JPanel controls = new JPanel();
controls.setLayout(new GridLayout(1, THREE));
JButton start = new JButton("Start");
start.addActionListener(new StartButtonListener());
JButton stop = new JButton("Stop");
stop.addActionListener(new StopButtonListener());
JButton reset = new JButton("Reset");
reset.addActionListener(new ResetButtonListener());
controls.add(start);
controls.add(stop);
controls.add(reset);
add(controls, BorderLayout.SOUTH);
}
/**
* Action listener to start the stop watch.
* @author
*
*/
private class StartButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
count++;
if (count <= 1) {
mtt = new ATimerTask();
timer.schedule(mtt, 0, delay);
}
}
}
/**
* Action listener to stop the stop watch.
* @author
*
*/
private class StopButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
mtt.cancel();
count = 0;
}
}
/**
* Action listener to reset the stop watch.
* @author
*
*/
private class ResetButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
mtt.cancel();
milliseconds = 0;
seconds = 0;
minutes = 0;
hours = 0;
time.setText("00:00:00.00");
count = 0;
}
}
/**
* The TimerTask used by java.util.Timer creates a schedule.
* That schedule is used to display the stop watch counting.
* @author
*
*/
private class ATimerTask extends TimerTask {
@Override
public void run() {
if (milliseconds < MILSECS) {
milliseconds++;
} else {
milliseconds = 0;
if (seconds < SIXTY) {
seconds++;
} else if (seconds == SIXTY) {
seconds = 0;
if (minutes <= (SIXTY - 1)) {
minutes++;
} else {
minutes = 0;
hours++;
}
}
}
String msecs = Integer.toString((int)(milliseconds));
String secs = Integer.toString((int)(seconds));
String mins = Integer.toString((int)(minutes));
String hrs = Integer.toString((int)(hours));
if (msecs.length() < 2) {
msecs = "0" + msecs;
}
if (secs.length() < 2) {
secs = "0" + secs;
}
if (mins.length() < 2) {
mins = "0" + mins;
}
if (hrs.length() < 2) {
hrs = "0" + hrs;
}
time.setText(hrs + ":" + mins + ":" + secs + "."
+ msecs);
}
}
}
/**
* <p>
* This is the main method (entry point) that gets called by the JVM.
* </p>
*
* @param args
* command line arguments.
*/
public static void main(String[] args) {
new StopWatch();
}
};
2 Replies - 224 Views - Last Post: 11 April 2012 - 11:13 AM
#1
At 1 minute my timer displays 0:60.xx instead of 1:00.xx
Posted 11 April 2012 - 10:33 AM
I've finished up an assignment all except for one little bug that I haven't been able to sort out. At 1 minute my timer displays 0:60.xx instead of 1:00.xx. Once my code reaches 61 seconds it displays 1:00.xx. Any help would be greatly appreciated.
Replies To: At 1 minute my timer displays 0:60.xx instead of 1:00.xx
#2
Re: At 1 minute my timer displays 0:60.xx instead of 1:00.xx
Posted 11 April 2012 - 10:59 AM
If you don't want to show 60, then consider how you might change this code:
Edit: If the problem isn't obvious - sometimes that happens when we stare at something too long - in your head, set seconds to 59 and work through the above logic. What will seconds be after you've gone through it.
if (seconds < SIXTY) {
seconds++;
} else if (seconds == SIXTY) {
seconds = 0;
if (minutes <= (SIXTY - 1)) {
minutes++;
etc. . .
Edit: If the problem isn't obvious - sometimes that happens when we stare at something too long - in your head, set seconds to 59 and work through the above logic. What will seconds be after you've gone through it.
This post has been edited by GregBrannon: 11 April 2012 - 11:07 AM
#3
Re: At 1 minute my timer displays 0:60.xx instead of 1:00.xx
Posted 11 April 2012 - 11:13 AM
GregBrannon, on 11 April 2012 - 10:59 AM, said:
If you don't want to show 60, then consider how you might change this code:
Edit: If the problem isn't obvious - sometimes that happens when we stare at something too long - in your head, set seconds to 59 and work through the above logic. What will seconds be after you've gone through it.
if (seconds < SIXTY) {
seconds++;
} else if (seconds == SIXTY) {
seconds = 0;
if (minutes <= (SIXTY - 1)) {
minutes++;
etc. . .
Edit: If the problem isn't obvious - sometimes that happens when we stare at something too long - in your head, set seconds to 59 and work through the above logic. What will seconds be after you've gone through it.
Thank you GregBannon. I was trying to mess around with that section of code for a quite a while to no avail. With the refreshed suggestion of looking there, I was able to figure it out.
My fix that's tested and works:
if (seconds < SIXTY) {
seconds++;
if (seconds >= 60) {
seconds = 0;
if (minutes <= (SIXTY - 1)) {
minutes++;
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|