diff --git a/cs621/proj1/Clock.java b/cs621/proj1/Clock.java index 70f2d40..055dbfd 100644 --- a/cs621/proj1/Clock.java +++ b/cs621/proj1/Clock.java @@ -1,30 +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); - } - } -} + +/** + * 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/ClockApplet.html b/cs621/proj1/ClockApplet.html new file mode 100644 index 0000000..edb2c1f --- /dev/null +++ b/cs621/proj1/ClockApplet.html @@ -0,0 +1,10 @@ + + Clock Applet + +
+

Clock Applet

+ +
+ + + diff --git a/cs621/proj1/ClockApplet.java b/cs621/proj1/ClockApplet.java new file mode 100644 index 0000000..33005f4 --- /dev/null +++ b/cs621/proj1/ClockApplet.java @@ -0,0 +1,88 @@ + +import javax.swing.JApplet; // import class JApplet +import java.awt.Graphics; // import class Graphics +import java.awt.*; + +/** + * This class provides an applet displaying the current time. + */ +public class ClockApplet extends JApplet +{ + private Thread animator = null; + + /** + * Called to stop the applet. + */ + public void stop() + { + if (animator != null) + animator.interrupt(); + animator = null; + } + + /** + * Method called to begin the applet. + */ + public void start() + { + if (animator == null) + { + animator = new Thread() { + public void run() { + updateTime(); + } + }; + animator.start(); + } + } + + /** + * The method run by the animator thread. + */ + public void updateTime() + { + try + { + while (!Thread.interrupted()) + { + repaint(); + Thread.sleep(500); + } + } + catch (InterruptedException e) { } + } + + /** + * This function draws the applet's contents. + * @param g the graphics object to draw with + */ + public void paint( Graphics g ) + { + Timestamp t = new Timestamp(); + t.fillTimes(); + String display = getDigitsAsString(t.hrs) + ":" + + getDigitsAsString(t.mins) + ":" + + getDigitsAsString(t.secs) + " " + + t.am_pm; + + g.setColor(Color.white); + g.fillRect(0, 0, 150, 60); + g.setColor(Color.black); + g.drawString(display, 10, 50); + } + + /** + * 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; + } +} + diff --git a/cs621/proj1/ClockView.java b/cs621/proj1/ClockView.java index 554b1ad..51008d4 100644 --- a/cs621/proj1/ClockView.java +++ b/cs621/proj1/ClockView.java @@ -1,54 +1,54 @@ - -import javax.swing.*; - -/** - * This class provides a graphical view of the current time - */ -public class ClockView extends JFrame -{ - private JLabel tLabel = new JLabel(); - - /** - * Constructor method to make a ClockView object. - */ - public ClockView() - { - super("Clock"); - setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - setSize(100, 70); - tLabel.setHorizontalAlignment(SwingConstants.CENTER); - 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) + " " + - t.am_pm; - tLabel.setText(" " + display); - tLabel.repaint(); - } -} - - + +import javax.swing.*; + +/** + * This class provides a graphical view of the current time + */ +public class ClockView extends JFrame +{ + private JLabel tLabel = new JLabel(); + + /** + * Constructor method to make a ClockView object. + */ + public ClockView() + { + super("Clock"); + setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + setSize(100, 70); + tLabel.setHorizontalAlignment(SwingConstants.CENTER); + 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) + " " + + t.am_pm; + tLabel.setText(" " + display); + tLabel.repaint(); + } +} + + diff --git a/cs621/proj1/Timestamp.java b/cs621/proj1/Timestamp.java index 3299c9a..7804fb3 100644 --- a/cs621/proj1/Timestamp.java +++ b/cs621/proj1/Timestamp.java @@ -1,30 +1,30 @@ - -/** - * This class holds a time value consisting of hours, minutes, and seconds. - */ -public class Timestamp -{ - // data fields - public int hrs; - public int mins; - public int secs; - public java.lang.String am_pm; - - /** - * This method updates the hours, minutes, and seconds values - * associated with the Timestamp object. - */ - public void fillTimes() - { - java.util.Calendar now; - now = java.util.Calendar.getInstance(); - hrs = now.get(java.util.Calendar.HOUR); - hrs = hrs == 0 ? 12 : hrs; - mins = now.get(java.util.Calendar.MINUTE); - secs = now.get(java.util.Calendar.SECOND); - am_pm = now.get(java.util.Calendar.AM_PM) == java.util.Calendar.AM - ? "AM" - : "PM"; - } -} - + +/** + * This class holds a time value consisting of hours, minutes, and seconds. + */ +public class Timestamp +{ + // data fields + public int hrs; + public int mins; + public int secs; + public java.lang.String am_pm; + + /** + * This method updates the hours, minutes, and seconds values + * associated with the Timestamp object. + */ + public void fillTimes() + { + java.util.Calendar now; + now = java.util.Calendar.getInstance(); + hrs = now.get(java.util.Calendar.HOUR); + hrs = hrs == 0 ? 12 : hrs; + mins = now.get(java.util.Calendar.MINUTE); + secs = now.get(java.util.Calendar.SECOND); + am_pm = now.get(java.util.Calendar.AM_PM) == java.util.Calendar.AM + ? "AM" + : "PM"; + } +} +