Wednesday, February 12, 2014

Java Programming Applet Animation

import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;


             /*<applet code="DigitalClock" width=800 height=800>
             </applet>*/

public class DigitalClock extends Applet
    implements Runnable
{

    Font theFont;
    Date theDate;
    Thread runner;
    int x1;
    int y1;
    int x2;
    int y2;
    int cx1;
    int cy1;
    int cx2;
    int cy2;

    public DigitalClock()
    {
        theFont = new Font("TimesRoman", 1, 24);
    }

    public void init()
    {
        x1 = 100;
        y1 = 100;
        x2 = 150;
        y2 = 100;

    }

    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void stop()
    {
        if(runner != null)
        {
            runner.stop();
            runner = null;
        }
    }

    public void run()
    {
        do
        {
            theDate = new Date();
            x1 = x1 + 100;
            x2 = x2 + 100;
            if(x2 > 1000)
            {
                x2 = 150;
                x1 = 100;
            }
            repaint();
            try
            {
                Thread.sleep(200L);
            }
            catch(InterruptedException interruptedexception) { }
        } while(true);
    }

    public void paint(Graphics g)
    {
        g.drawLine(x1, y1, x2, y2);
        g.drawOval(x1, y1, x2, y2);
        g.setFont(theFont);
        g.drawString(theDate.toString(), 10, 50);
    }
}

No comments: