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

public class DigitalWatch extends Applet implements Runnable
{
Thread m_DigitalWatch = null;

String m_string = "Bernd Nötscher";
int m_size = 8;
Font m_font;
int m_pics_sec = 2;
Image m_image1;
Image m_image2;
Graphics m_graphics1;
Graphics m_graphics2;
int m_breite;
int m_hoehe;
int m_breite2;
int m_hoehe2;
int m_blocksize = 2;
int[] pix;
Color Color1;
Date Date1;
Integer nHours;
Integer nMinutes;
Integer nSeconds;
PixelGrabber pp;

public DigitalWatch()
{
}

public String getAppletInfo()
{
return "Name: DigitalWatch\r\n" +
"Author: BN\r\n";
}

public void init()
{
m_breite = 96;
m_hoehe = 22;

m_image1 = this.createImage(m_breite, m_hoehe);
if(m_image1!=null){
m_graphics1 = m_image1.getGraphics();

m_image2 = this.createImage(m_breite, m_hoehe);
if(m_image2!=null){
m_graphics2 = m_image2.getGraphics();

m_font = new Font("Helvetica", Font.PLAIN, m_size);
this.setFont(m_font);
m_breite2 = m_breite / m_blocksize;
m_hoehe2 = m_hoehe / m_blocksize;
pix = new int[m_breite2 * m_hoehe2];
}
}
}

public void destroy()
{
}

public void update(Graphics g)
{
if(m_image1!=null && m_image2!=null){
m_graphics1.setColor(Color.black);
m_graphics1.fillRect(0, 0, m_breite, m_hoehe);
Date1 = new Date();
nHours = new Integer(Date1.getHours());
nMinutes = new Integer(Date1.getMinutes());
nSeconds = new Integer(Date1.getSeconds());
m_string =
(nHours.intValue()<10 ? "0" + nHours.toString() : nHours.toString())
+ ":"
+ (nMinutes.intValue()<10 ? "0" + nMinutes.toString() : nMinutes.toString())
+ ":"
+ (nSeconds.intValue()<10 ? "0" + nSeconds.toString() : nSeconds.toString());

m_graphics1.setColor(Color.yellow);
m_graphics1.drawString(m_string, 0, 10);
}
paint(g);
}

public final static Color handleSinglePixel(int pixel){
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
return (new Color(red, green, blue));
}

public void paint(Graphics g)
{
int x, y;

if(m_image1!=null && m_image2!=null){
m_graphics2.setColor(Color.black);
m_graphics2.fillRect(0, 0, m_breite, m_hoehe);

pp = new PixelGrabber(m_image1, 0, 0, m_breite2, m_hoehe2, pix, 0, m_breite2);
try
{
pp.grabPixels();
}
catch (InterruptedException e)
{
System.out.println("InterruptedException");
return;
}
if((pp.status() & ImageObserver.ABORT)!=0){
System.out.println("(pp.status() & ImageObserver.ABORT)!=0");
return;
}

for(y=0;y<m_hoehe2;y++){
for(x=0;x<m_breite2;x++){
Color1 = handleSinglePixel(pix[m_breite2 * y + x]);
if(Color1.equals(Color.black)){
m_graphics2.setColor(Color.darkGray);
} else {
m_graphics2.setColor(Color1);
}
m_graphics2.fillRect(x * m_blocksize, y * m_blocksize, (m_blocksize - 1), (m_blocksize - 1));
}
}
g.drawImage(m_image2, 0, 0, null);
}
}

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

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

public void run()
{
while (true)
{
try
{
repaint();
Thread.sleep(1000 / m_pics_sec);
}
catch (InterruptedException e)
{
stop();
}
}
}
}