cs621: proj1: ClockApplet added, completed
git-svn-id: svn://anubis/gvsu@10 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
2d610b1c83
commit
c5eb553073
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
cs621/proj1/ClockApplet.html
Normal file
10
cs621/proj1/ClockApplet.html
Normal file
@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head><title>Clock Applet</title></head>
|
||||
<body>
|
||||
<center>
|
||||
<h1><b>Clock Applet</b></h1>
|
||||
<applet code="ClockApplet.class" width=150 height=60></applet>
|
||||
</center>
|
||||
</body>
|
||||
</html>
|
||||
|
88
cs621/proj1/ClockApplet.java
Normal file
88
cs621/proj1/ClockApplet.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user