Display a Clock In Java Applet

Create a Clock In Java Applet

Now, here we’ll create a clock in java applet. Java method that you first have to understand is :

  • The Thread
  • The Calendar Class, and
  • Graphics g

After that, you can create a clock app just like this :

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

public class ClockApp 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.setColor(Color.BLACK);
g.drawString(hour + “:” + minute + “:” + second, 20, 30);
setBackground(Color.GREEN);
}
}

This code will output something just like this :

clock in java applet

 

Published by

Rafif Santika

The programming language that I specialize in is Java, because I think Java programming language is more universal and of course because I like it regardless of any reason.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.