Digital clock in Applet

Digital clock can be created by using the Calendar and SimpleDateFormat class. Let’s see the simple example:

Example of Digital clock in Applet:

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. import java.text.*;
  5. public class DigitalClock extends Applet implements Runnable {
  6.    Thread t = null;
  7.    int hours=0, minutes=0, seconds=0;
  8.    String timeString = “”;
  9.    public void init() {
  10.       setBackground( Color.green);
  11.    }
  12.    public void start() {
  13.         t = new Thread( this );
  14.         t.start();
  15.    }
  16.    public void run() {
  17.       try {
  18.          while (true) {
  19.             Calendar cal = Calendar.getInstance();
  20.             hours = cal.get( Calendar.HOUR_OF_DAY );
  21.             if ( hours > 12 ) hours -= 12;
  22.             minutes = cal.get( Calendar.MINUTE );
  23.             seconds = cal.get( Calendar.SECOND );
  24.             SimpleDateFormat formatter = new SimpleDateFormat(“hh:mm:ss”);
  25.             Date date = cal.getTime();
  26.             timeString = formatter.format( date );
  27.             repaint();
  28.             t.sleep( 1000 );  // interval given in milliseconds
  29.          }
  30.       }
  31.       catch (Exception e) { }
  32.    }
  33.   public void paint( Graphics g ) {
  34.       g.setColor( Color.blue );
  35.       g.drawString( timeString, 5050 );
  36.    }
  37. }
In the above example, getX() and getY() method of MouseEvent is used to get the current x-axis and y-axis. The getGraphics() method of Component class returns the object of Graphics.

myapplet.html

  1. <html>
  2. <body>
  3. <applet code=“DigitalClock.class” width=“300” height=“300”>
  4. </applet>
  5. </body>
  6. </html>
download this example.
Next TopicAnalog Clock In Applet
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Digital clock can be created by using the Calendar and SimpleDateFormat class. Let’s see the simple example: Example of Digital clock in Applet: import java.applet.*; import java.awt.*; import java.util.*; import java.text.*; public class DigitalClock extends Applet implements Runnable {    Thread t = null;    int hours=0, minutes=0, seconds=0;    String timeString = “”;    public void init() {       setBackground( Color.green);    }    public void start() {         t = new Thread( this );         t.start();    }    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 );  // interval given in milliseconds          }       }       catch (Exception e) { }    } […]

Tags:

....
...