본문 바로가기

OM

디지털 시계


DigitalWatch.java=====================================================================================

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class DigitalWatch extends JApplet implements Runnable
{
    Thread clock;
    Font myFont;

    public void init()
    {
        myFont = new Font("Serif", Font.BOLD, 40);
        setBackground(Color.yellow);
    }

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

    public void paint(Graphics g)
    {
        Calendar now = Calendar.getInstance();

        int year  = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH);
        int date  = now.get(Calendar.DATE);
        int hour  = now.get(Calendar.HOUR);
        int min   = now.get(Calendar.MINUTE);
        int sec   = now.get(Calendar.SECOND);

        g.setFont(myFont);

        g.clearRect(0, 0, getWidth(), getHeight());
        g.drawString(year + "-" + digit(month+1) + "-" + digit(date) + " "
            + digit(hour) + ":" + digit(min) + ":" + digit(sec), 10, 40);
    }

    public void stop()
    {
        if(clock!=null && clock.isAlive())
        {
            clock = null;
        }
    }

    public void run()
    {
        while(true)
        {
            try
            {
                clock.sleep(1000);
            }
            catch(InterruptedException ie){}

            repaint();
        }
    }

    private String digit(int num)
    {
        if(num<10)
        {
            return "0" + num;
        }
        else{
            return "" + num;
        }
    }
}

=====================================================================================================

'OM' 카테고리의 다른 글

애니메이션  (0) 2008.05.07
생명게임  (0) 2008.05.05
Card Match 2  (0) 2008.05.04
원 그리기 (MouseListener)  (0) 2008.05.02
숫자 야구 게임  (0) 2008.05.02
가위바위보 게임  (2) 2008.04.28
War zero  (0) 2008.04.14
동영상 플레이어 (FLVPlayback) 2  (0) 2008.04.10
동영상 플레이어 (FLVPlayback)  (0) 2008.04.10