adsense


Showing posts with label JAVA EXAMPLES DISPLAY A CLOCK USING APPLET. Show all posts
Showing posts with label JAVA EXAMPLES DISPLAY A CLOCK USING APPLET. Show all posts

Sunday, 1 November 2015

JAVA EXAMPLES DISPLAY A CLOCK USING APPLET PAGE 200

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES DISPLAY A CLOCK USING APPLET






Java Examples - display a clock using Applet?



Problem Description:
How to display clock using Applet?
Solution:
Following example demonstrates how to display a clock using valueOf() mehtods of String Class. & using Calender class to get the second, minutes & hours.
import java.awt.*;
import java.applet.*;

import java.applet.*;
import java.awt.*;
import java.util.*;

public class ClockApplet 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 minute = String.valueOf(cal.get(Calendar.MINUTE));
      String second = String.valueOf(cal.get(Calendar.SECOND));
      g.drawString(hour + ":" + minute + ":" + second, 20, 30);
   }
}
Result:
The above code sample will produce the following result in a java enabled web browser.
View in Browser.