Java program to implement Applet color that dispaly current time.

Below is the code for java applet that displays yhe current time and change its color every 2 sec:

import java.applet.*;
import java.awt.*;      
import java.util.Date;
import java.util.Random;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/*
<applet code = appletColor.class height=300 width=300 >
</applet>
*/
public class appletColor extends Applet implements Runnable 
{

    Color c;
    DateFormat df;
    Date dateobj;
    Random rand;
    int max=255;
    int min=0;

    public void init() 
    {
        repaint();
        Thread th = new Thread(this);
        df = new SimpleDateFormat("HH:mm:ss");
        rand=new Random();
        th.start();
    }
    
    public void run() 
    {
        try 
        {
            while(true
            {
                float r=rand.nextFloat();
                float g=rand.nextFloat();
                float b=rand.nextFloat();
                c=new Color(r,g,b);
                repaint();
                Thread.sleep(2000);
            }
        } 
        catch (Exception ex
        {
            ex.printStackTrace();
        }
    }
    public void update(Graphics g
    {
        paint(g);
    }
    public void paint(Graphics g
    {
        dateobj = new Date();
        g.drawString("Current time is " + df.format(dateobj),20,20);
        setBackground(c);
    }

Thank you.

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT