import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.Date;
public class DigitalClock extends Frame
{
String dateStr = "";
int hour;
int min;
int second;
Image[] numImg = new Image[10];
public DigitalClock()
{
super("AWT와 스레드를 이용한 디지털 시계");
ClockPaintThread cpt = new ClockPaintThread();
for(int i = 0; i < numImg.length; i++)
{
numImg[i] = Toolkit.getDefaultToolkit().getImage("img/num"+ i + ".png");
}
setSize(500, 200);
setBackground(Color.GREEN);
setVisible(true);
cpt.start();
}
//---------------------------------페인트---------------------------------
public void paint(Graphics g)
{
super.paintComponents(g);
drawNumImg(g, hour, min, second);
g.drawString(dateStr, 30, 50);
}
private void drawNumImg(Graphics g, int hour, int min, int second)
{
//시
g.drawImage(numImg[(int)hour/10], 0, 70, this);
g.drawImage(numImg[(int)hour%10], 70, 70, this);
//분
g.drawImage(numImg[(int)min/10], 170, 70, this);
g.drawImage(numImg[(int)min%10], 240, 70, this);
//초
g.drawImage(numImg[(int)second/10], 340, 70, this);
g.drawImage(numImg[(int)second%10], 410, 70, this);
}
//---------------------------------클럭 스레드---------------------------------
public class ClockPaintThread extends Thread
{
public void run()
{
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Date d = new Date();
hour = d.getHours();
min = d.getMinutes();
second = d.getSeconds();
dateStr = new Date().toString();
repaint();
}
}
}
public static void main(String[] ar)
{
new DigitalClock();
}
}