Sunday, February 14, 2016

Digital Clock Applet - Java

import java.applet.Applet;
import java.awt.Graphics;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class clock extends Applet implements Runnable{
Thread t,t1;

public void start(){

t = new Thread(this);
t.start();
}

public void run(){
t1= Thread.currentThread();

while(t1==t){

repaint();
try{

t1.sleep(1000);

}

catch(InterruptedException e){
}

}
}
public void paint(Graphics g){

Calendar cal = new GregorianCalendar();
String hour = String.valueOf(cal.get(Calendar.HOUR));
String min = String.valueOf(cal.get(Calendar.MINUTE));
String sec = String.valueOf(cal.get(Calendar.SECOND));

g.drawString(hour+" : "+min+" : "+sec,20,30);
}

}

For running applet create a file name clock.html and edit it like below and Run applet

<APPLET CODE="clock.class" HEIGHT ="400" WIDTH = "400"></APPLET>

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)