Advanced Java Applet program to display digital clock
Simple Advanced Java Applet program to display digital clock
Code:
package javaapplication7;import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class JavaApplication7 extends Applet implements Runnable {
Thread t = null;
int hours=0, minutes=0, seconds=0;
String timeString = "";
public void init() {
setBackground( Color.white);
}
public void start() {
t = new Thread( this );
t.start();
}
public void paint( Graphics g ) {
g.setColor( Color.black );
g.drawString( timeString, 100, 100 );
}
public void run() {
try {
while (true) {
Calendar cal = Calendar.getInstance();
hours = cal.get( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = cal.get( Calendar.MINUTE );
seconds = cal.get( Calendar.SECOND );
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
Date date = cal.getTime();
timeString = formatter.format( date );
repaint();
t.sleep( 1000 );
}
}
catch (Exception e) { e.getMessage();}
}
}
Comments
Post a Comment