cs621: proj1: added javadoc to base Clock .java files

git-svn-id: svn://anubis/gvsu@8 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-02-03 04:11:53 +00:00
parent 64206b4ecf
commit 6aad5842fd
3 changed files with 106 additions and 0 deletions

30
cs621/proj1/Clock.java Normal file
View File

@ -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);
}
}
}

View File

@ -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 ();
}
}

View File

@ -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);
}
}