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.
|
* A front-end class to display and update a ClockView object.
|
||||||
*/
|
*/
|
||||||
public class Clock
|
public class Clock
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* This method is the entry point when the user invokes the program.
|
* This method is the entry point when the user invokes the program.
|
||||||
* @param args Any command-line arguments supplied during invocation.
|
* @param args Any command-line arguments supplied during invocation.
|
||||||
*/
|
*/
|
||||||
public static void main (String args[])
|
public static void main (String args[])
|
||||||
{
|
{
|
||||||
ClockView cv = new ClockView();
|
ClockView cv = new ClockView();
|
||||||
cv.setVisible (true);
|
cv.setVisible (true);
|
||||||
|
|
||||||
// loop about every 0.5 seconds
|
// loop about every 0.5 seconds
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
cv.refreshTimeDisplay ();
|
cv.refreshTimeDisplay ();
|
||||||
Thread.sleep (500);
|
Thread.sleep (500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
System.out.println("Error:" + 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.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class provides a graphical view of the current time
|
* This class provides a graphical view of the current time
|
||||||
*/
|
*/
|
||||||
public class ClockView extends JFrame
|
public class ClockView extends JFrame
|
||||||
{
|
{
|
||||||
private JLabel tLabel = new JLabel();
|
private JLabel tLabel = new JLabel();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor method to make a ClockView object.
|
* Constructor method to make a ClockView object.
|
||||||
*/
|
*/
|
||||||
public ClockView()
|
public ClockView()
|
||||||
{
|
{
|
||||||
super("Clock");
|
super("Clock");
|
||||||
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
setSize(100, 70);
|
setSize(100, 70);
|
||||||
tLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
tLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
getContentPane().add(tLabel);
|
getContentPane().add(tLabel);
|
||||||
refreshTimeDisplay();
|
refreshTimeDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper method to convert an integer to a 0-padded string.
|
* Helper method to convert an integer to a 0-padded string.
|
||||||
* @param i The integer to convert
|
* @param i The integer to convert
|
||||||
* @return A string representing the integer, padded on the
|
* @return A string representing the integer, padded on the
|
||||||
* left by a '0' character if the integer was less than 10.
|
* left by a '0' character if the integer was less than 10.
|
||||||
*/
|
*/
|
||||||
protected String getDigitsAsString(int i)
|
protected String getDigitsAsString(int i)
|
||||||
{
|
{
|
||||||
String str = Integer.toString(i);
|
String str = Integer.toString(i);
|
||||||
if (i < 10)
|
if (i < 10)
|
||||||
str = "0" + str;
|
str = "0" + str;
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A method to update the time displayed in the ClockView window.
|
* A method to update the time displayed in the ClockView window.
|
||||||
*/
|
*/
|
||||||
public void refreshTimeDisplay()
|
public void refreshTimeDisplay()
|
||||||
{
|
{
|
||||||
Timestamp t = new Timestamp();
|
Timestamp t = new Timestamp();
|
||||||
t.fillTimes();
|
t.fillTimes();
|
||||||
String display = getDigitsAsString(t.hrs) + ":" +
|
String display = getDigitsAsString(t.hrs) + ":" +
|
||||||
getDigitsAsString(t.mins) + ":" +
|
getDigitsAsString(t.mins) + ":" +
|
||||||
getDigitsAsString(t.secs) + " " +
|
getDigitsAsString(t.secs) + " " +
|
||||||
t.am_pm;
|
t.am_pm;
|
||||||
tLabel.setText(" " + display);
|
tLabel.setText(" " + display);
|
||||||
tLabel.repaint();
|
tLabel.repaint();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,30 +1,30 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This class holds a time value consisting of hours, minutes, and seconds.
|
* This class holds a time value consisting of hours, minutes, and seconds.
|
||||||
*/
|
*/
|
||||||
public class Timestamp
|
public class Timestamp
|
||||||
{
|
{
|
||||||
// data fields
|
// data fields
|
||||||
public int hrs;
|
public int hrs;
|
||||||
public int mins;
|
public int mins;
|
||||||
public int secs;
|
public int secs;
|
||||||
public java.lang.String am_pm;
|
public java.lang.String am_pm;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method updates the hours, minutes, and seconds values
|
* This method updates the hours, minutes, and seconds values
|
||||||
* associated with the Timestamp object.
|
* associated with the Timestamp object.
|
||||||
*/
|
*/
|
||||||
public void fillTimes()
|
public void fillTimes()
|
||||||
{
|
{
|
||||||
java.util.Calendar now;
|
java.util.Calendar now;
|
||||||
now = java.util.Calendar.getInstance();
|
now = java.util.Calendar.getInstance();
|
||||||
hrs = now.get(java.util.Calendar.HOUR);
|
hrs = now.get(java.util.Calendar.HOUR);
|
||||||
hrs = hrs == 0 ? 12 : hrs;
|
hrs = hrs == 0 ? 12 : hrs;
|
||||||
mins = now.get(java.util.Calendar.MINUTE);
|
mins = now.get(java.util.Calendar.MINUTE);
|
||||||
secs = now.get(java.util.Calendar.SECOND);
|
secs = now.get(java.util.Calendar.SECOND);
|
||||||
am_pm = now.get(java.util.Calendar.AM_PM) == java.util.Calendar.AM
|
am_pm = now.get(java.util.Calendar.AM_PM) == java.util.Calendar.AM
|
||||||
? "AM"
|
? "AM"
|
||||||
: "PM";
|
: "PM";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user