From 6aad5842fdb9a27ffc0766c273aea6f81bd434a5 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 3 Feb 2008 04:11:53 +0000 Subject: [PATCH] cs621: proj1: added javadoc to base Clock .java files git-svn-id: svn://anubis/gvsu@8 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs621/proj1/Clock.java | 30 ++++++++++++++++++++++ cs621/proj1/ClockView.java | 51 ++++++++++++++++++++++++++++++++++++++ cs621/proj1/Timestamp.java | 25 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 cs621/proj1/Clock.java create mode 100644 cs621/proj1/ClockView.java create mode 100644 cs621/proj1/Timestamp.java diff --git a/cs621/proj1/Clock.java b/cs621/proj1/Clock.java new file mode 100644 index 0000000..70f2d40 --- /dev/null +++ b/cs621/proj1/Clock.java @@ -0,0 +1,30 @@ + +/** + * A front-end class to display and update a ClockView object. + */ +public class Clock +{ + /** + * This method is the entry point when the user invokes the program. + * @param args Any command-line arguments supplied during invocation. + */ + public static void main (String args[]) + { + ClockView cv = new ClockView(); + cv.setVisible (true); + + // loop about every 0.5 seconds + try + { + for (;;) + { + cv.refreshTimeDisplay (); + Thread.sleep (500); + } + } + catch (Exception e) + { + System.out.println("Error:" + e); + } + } +} diff --git a/cs621/proj1/ClockView.java b/cs621/proj1/ClockView.java new file mode 100644 index 0000000..ecb9a04 --- /dev/null +++ b/cs621/proj1/ClockView.java @@ -0,0 +1,51 @@ + +import javax.swing.*; + +/** + * This class provides a graphical view of the current time + */ +class ClockView extends JFrame +{ + private JLabel tLabel = new JLabel(); + + /** + * Constructor method to make a ClockView object. + */ + ClockView() + { + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setSize(95, 75); + getContentPane().add(tLabel); + refreshTimeDisplay(); + } + + /** + * Helper method to convert an integer to a 0-padded string. + * @param i The integer to convert + * @return A string representing the integer, padded on the + * left by a '0' character if the integer was less than 10. + */ + protected String getDigitsAsString(int i) + { + String str = Integer.toString(i); + if (i < 10) + str = "0" + str; + return str; + } + + /** + * A method to update the time displayed in the ClockView window. + */ + public void refreshTimeDisplay() + { + Timestamp t = new Timestamp(); + t.fillTimes(); + String display = getDigitsAsString(t.hrs) + ":" + + getDigitsAsString(t.mins) + ":" + + getDigitsAsString(t.secs); + tLabel.setText (" " + display); + tLabel.repaint (); + } +} + + diff --git a/cs621/proj1/Timestamp.java b/cs621/proj1/Timestamp.java new file mode 100644 index 0000000..209f1bc --- /dev/null +++ b/cs621/proj1/Timestamp.java @@ -0,0 +1,25 @@ + +/** + * This class holds a time value consisting of hours, minutes, and seconds. + */ +class Timestamp +{ + // data fields + int hrs; + int mins; + int secs; + + /** + * This method updates the hours, minutes, and seconds values + * associated with the Timestamp object. + */ + void fillTimes() + { + java.util.Calendar now; + now = java.util.Calendar.getInstance(); + hrs = now.get(java.util.Calendar.HOUR_OF_DAY); + mins = now.get(java.util.Calendar.MINUTE); + secs = now.get(java.util.Calendar.SECOND); + } +} +