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

public class Leuchttafel extends Applet implements Runnable
{

private Thread m_Leuchttafel = null;
String m_string = "Bernd Noetscher";
String m_string2 = "";

int nZeilenAnzahl=4;
String m_font = "Helvetica";
int m_size = 12;
int m_fps = 10;
private int m_nOffset;
private int m_nMax;
private int m_nMin;
int nCounter=0;
boolean nHochzaehlen=true;
Image m_image;
Image m_image2;
Graphics m_g;
Graphics m_g2;
int m_breite;
int m_hoehe;
int nX;
Font font;
int x=0, y=0, w=0, h=0;
int[] pixels;
Integer test;
int nWie=0;
int bRicht=0;
Color Color1;


public Leuchttafel()
{
}


public String getAppletInfo()
{
return "BN\r\n" +
"Copyright (c) 1998";
}

public void init()
{
//resize(320, 20);
Font font = new Font(m_font, Font.PLAIN, m_size);
setFont(font);

//this.setForeground(Color.red);
//this.setBackground(Color.white);

FontMetrics fm = getFontMetrics(font);
m_nMin = -fm.stringWidth(m_string);
//m_nMin = 1;
m_nMax = size().width/3;
m_nOffset = 1;
m_breite = size().width;
m_hoehe = size().height;
m_image = createImage(m_breite, m_hoehe);
m_image2 = createImage(m_breite, m_hoehe);
m_g = m_image.getGraphics();
m_g2 = m_image2.getGraphics();

w=m_breite;
h=m_hoehe/2;
pixels = new int[w * h];

}


public void destroy()
{
}


public void update(Graphics g)
{
int nVertOffset = size().height;
int nWert;

/*
if(nHochzaehlen==true){
nCounter++;
} else {
nCounter--;
}
if(nCounter>255){
nCounter = 255;
nHochzaehlen = false;
} else if(nCounter<0){
nCounter = 0;
nHochzaehlen = true;
}
Color Color1 = new Color(nCounter, 0, 0);


nWert = nCounter;
if(nWert<220){
nWert = 220;
}
Color Color2 = new Color(nWert, nWert, nWert);
m_g.setColor(Color2);
m_g.fillRect(0, 0, m_breite, m_hoehe);
*/
m_g.setColor(Color.black);
//m_g.setColor(new Color(255, 230, 255));

m_g.fillRect(0, 0, m_breite, m_hoehe);

nVertOffset = (nVertOffset - m_size) / 2;
// m_g.setColor(Color1);
m_g.setColor(Color.yellow);
//m_g.setColor(new Color(0x80, 0x00, 0x80));
m_g.drawString(m_string, m_nOffset, (nVertOffset + m_size)/2);
m_nOffset -= 1;
// m_nOffset--;
if(m_nOffset<m_nMin){
m_nOffset = m_nMax;
}
paint(g);
}
/*
public void init()
{
//resize(320, 40);
font = new Font(m_font, Font.PLAIN, m_size);
setFont(font);


m_nMin = -(nZeilenAnzahl * m_size);
m_nMax = size().height;
m_nOffset = m_nMax;
m_breite = size().width;

m_hoehe = size().height;
m_image = createImage(m_breite, m_hoehe);
m_image2 = createImage(m_breite, m_hoehe);
m_g = m_image.getGraphics();
m_g2 = m_image2.getGraphics();

w=m_breite;
h=m_hoehe/2;
pixels = new int[w * h];
//pg = new PixelGrabber(m_image, x, y, w, h, pixels, 0, w);


}

public void destroy()
{
}


public void update(Graphics g)
{

FontMetrics fm = getFontMetrics(font);
m_g.setColor(Color.black);
m_g.fillRect(0, 0, m_breite, m_hoehe);

m_g.setColor(Color.green);
// m_g.drawString("Hallo", m_nMax - m_nOffset, 12);
// m_g.drawString("Hallo" + m_nOffset, 12, 12);
m_g.drawString("Hallo" + m_nOffset, 12 + m_nMax - m_nOffset, 12);

nX = (m_breite - fm.stringWidth(m_string)) / 2 / 2; m_g.drawString(m_string, nX, m_nOffset + m_size);
nX = (m_breite - fm.stringWidth(m_string2)) / 2 / 2; m_g.drawString(m_string2, nX, m_nOffset + (m_size * 2));
nX = (m_breite - fm.stringWidth(m_string3)) / 2 / 2; m_g.drawString(m_string3, nX, m_nOffset + (m_size * 3));
nX = (m_breite - fm.stringWidth(m_string4)) / 2 / 2; m_g.drawString(m_string4, nX, m_nOffset + (m_size * 4));

m_nOffset -= 2;
// m_nOffset--;
if(m_nOffset<m_nMin){
m_nOffset = m_nMax;
}
paint(g);
}
*/

public static final Color handlesinglepixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
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 nWert;
// int x=0, y=0, w=m_breite, h=m_hoehe/2;
// int[] pixels = new int[w * h];


if(m_image!=null && m_image2!=null){

m_g2.setColor(Color.black);
m_g2.fillRect(0, 0, m_breite, m_hoehe);


PixelGrabber pg = new PixelGrabber(m_image, x, y, w/3, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
//System.err.println("interrupted waiting for pixels!");
return;
}

if ((pg.status() & ImageObserver.ABORT) != 0) {
//System.err.println("image fetch aborted or errored");
return;
}


for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {

Color1 = handlesinglepixel(x+i, y+j, pixels[j * w + i]);
if(Color1.equals(Color.black)){
//m_g2.setColor(Color.gray);
/*
if (bRicht==0){
nWie++;
} else {
nWie--;
}
if (nWie==60){
bRicht = (bRicht==0) ? 1 : 0;
} else if(nWie==0){
bRicht = (bRicht==0) ? 1 : 0;
}
*/
nWert = 20 + j * 10;
// m_g2.setColor(new Color(nWert, 20 + nWert + nWie, nWert));
m_g2.setColor(new Color(nWert, nWert, 40 + nWert + nWie));
} else if(Color1.equals(Color.yellow)){
// m_g2.setColor(Color1);
m_g2.setColor(new Color(0, 160 + j * 6, i / 10));
} else {
m_g2.setColor(Color1);
}
m_g2.fillRect(i * 3, j * 3, 2, 2);
}
}


g.drawImage(m_image2, 0, 0, null);
}
}


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


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

}


public void run()
{


while (true)
{
int nSleepValue = 1000 / m_fps;

try
{
repaint();
Thread.sleep(nSleepValue);
}
catch (InterruptedException e)
{
stop();
}
}
}

}