Source Code for Java Stopwatch - Lawrence Nguyen

kitewormsSoftware and s/w Development

Nov 3, 2013 (3 years and 9 months ago)

98 views

StopWatchPanel.java

package q3;

import javax.swing.JButton;

import javax.swing.JPanel;

import java.awt.Color;

import java.awt.Dimension;

import javax.swing.Timer;

import javax.swing.JLabel;

import java.awt.BorderLayout;

import java.awt.GridLayout;

import
java.awt.Font;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.text.DecimalFormat;


/*


* This class draws the timer as well as starts, stops and resets the timer


* by using listeners.


*/

/**


*


* @author Lawrence


*
/

public class StopWatchPanel extends JPanel {



/**


* creates a start .


*/


private JButton start;


/**


* creates a reset button.


*/


private JButton reset;


/**


* creates a stop button.


*/


private JButton

stop;


/**


* creates a label to display the time.


*/


private JLabel labelTimer;


/**


* creates a Timer object called stopwatch.


*/


private Timer stopwatch;


/**


* keeps track of the time of the stopwatch.


*/


private double time;


/**


* sets 0.001 value.


*/


private final double decimal = 0.001;


/**


* displays the milliseconds.


*/


private double milliSeconds = decimal;


/**


* displays the value 50.


*/


private final int fifty = 50;


/**


* displays the value 100.


*/


private final int hundred = 100;


/**


* displays the value 300.


*/


private final int threeHundred = 300;



/**


* creates a Stopwatch panel that displa
ys the numbers as well as stop,


* reset and start the timer.


*/


public StopWatchPanel() {



setLayout(new BorderLayout());


start = new JButton("start");


stop = new JButton("stop");


reset = new JButton("reset");



JPanel panel = new JPanel();


labelTimer = new JLabel("00 : 0");


Font font = new Font("Verdana", Font.BOLD, fifty);


labelTimer.setFont(font);


panel.add(labelTimer);


add(panel, BorderLayout.CENTER);



JPanel panel2 = new JPanel();


panel2.setLayout(new GridLayout(1, (2 + 1)));


start.addActionListener(new WatchButtonListener());


panel2.add(start);


stop.addActionListener(new WatchButtonListener());


panel2.add(stop);


reset.addActionListener(new WatchButtonListener());


panel2.add(reset);


setPreferredSize(new Dimension(threeHundred, hundred));


setBackground(Color.yellow);


add(panel2, BorderLayout.SOUTH);



stopwatch = new Ti
mer(hundred, new WatchButtonListener());


}

/**


* uses a listener to start, stop and reset the time when the buttons are


* clicked.


*/


private class WatchButtonListener implements ActionListener {

/**


* When the buttons are clicked, the start bu
tton starts incrementing the time,


* reset button resets the time to 0 and the stop button stops the time.


* @param event when action is preformed (start, stop and reset).


*/


public void actionPerformed(ActionEvent event) {


DecimalFormat fmt = new DecimalFormat("00.0#");


if (event.getSource() == start) {


stopwatch.start();



}




if (event.getSource() == stop) {


stopwatch.stop();


}


if (e
vent.getSource() == reset) {


time = 0;


labelTimer.setText("00.0");


stopwatch.stop();


}


if (event.getSource() == stopwatch) {


time += stopwatch.getDelay() * milliSeconds
;



labelTimer.setText("" + fmt.format(time));




}


}


}

}


StopWatch.java

package q3;

import javax.swing.JFrame;

/**


* <p>This class will display a frame that contains a panel of a stopwatch.</p>


*


* @author Lawrence Nguyen


* @version 1.0


*/

public class StopWatch {


/**


* <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) {



JFrame frame = new JFrame("Timer");


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



frame.getContentPane().add(new StopWatchPanel());


frame.pack();


frame.setVisible(true);




System.out.println("Question three was called and ran sucessfully.");


}

};