// Fader Java Applet von Sebastian Wallroth
// SeWallroth@aol.com

import java.applet.*;
import java.awt.*;
import java.net.URL;
import java.io.*;
import java.util.*;

public class fader extends Applet implements Runnable
{
	private Thread Animation;
	private Image Buffer;
	private Graphics gBuffer;

	Vector text;
	Color farbe[];
	Font faderfont;
	int spaceTop, spaceLeft, zeilen, y, z, incr, delay;
	String textFile;

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

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

	public void run()
	{
		while (true)
		{
			try
			{
				Animation.sleep(delay);
			}
			catch (Exception e)
			{
			}
			repaint();
		}
	}

	public void update(Graphics g)
	{
		paint(g);
	}

	private String getStringParam(String name, String standardWert)
	{
		String param = getParameter(name);
		if (param==null) return standardWert;
		return param;
	}
	
	private int getIntParam(String name, int standardWert)
	{
		String param = getParameter(name);
		if (param==null) return standardWert;
		return (Integer.parseInt(param, 10));
	}
	
	public void init()
	{
		textFile = getStringParam("textFile","fader.txt");
		String fontName = getStringParam("fontName","Helvetica");	// Courier, Helvetiva, TimesRoman
		int fontStyle = getIntParam("fontStyle",0);					// 0:normal 1:fett 2:kursiv
		int fontSize = getIntParam("fontSize",12);
		spaceTop = getIntParam("spaceTop",10) + fontSize;
		spaceLeft = getIntParam("spaceLeft",10);
		String bgColor = getStringParam("bgColor","#ffffff");
		String fgColor = getStringParam("fgColor","#000000");
		delay = getIntParam("delay",10);

		if (bgColor.startsWith("#")) bgColor = bgColor.substring(1);
		int backR = Integer.parseInt(bgColor.substring(0,2),16);
		int backG = Integer.parseInt(bgColor.substring(2,4),16);
		int backB = Integer.parseInt(bgColor.substring(4,6),16);
		if (fgColor.startsWith("#")) fgColor = fgColor.substring(1);
		int foreR = Integer.parseInt(fgColor.substring(0,2),16);
		int foreG = Integer.parseInt(fgColor.substring(2,4),16);
		int foreB = Integer.parseInt(fgColor.substring(4,6),16);
		faderfont = new Font(fontName,fontStyle,fontSize);
		farbe = new Color[256];
		float fBackR = (float)backR;
		float fBackG = (float)backG;
		float fBackB = (float)backB;
		float fForeR = (float)foreR;
		float fForeG = (float)foreG;
		float fForeB = (float)foreB;
		float incrR = (fBackR - fForeR) / 256f;
		float incrG = (fBackG - fForeG) / 256f;
		float incrB = (fBackB - fForeB) / 256f;
		for (int i=0;i<256;i++)
		{
			farbe[i] = new Color((int)(fBackR), (int)(fBackG), (int)(fBackB));
			fBackR = fBackR - incrR;
			fBackG = fBackG - incrG;
			fBackB = fBackB - incrB;
		}
		text = new Vector();
		try 
		{
			URL url = new URL(getDocumentBase(),textFile);
			InputStream stream=url.openStream();
			DataInputStream datei = new DataInputStream(stream);
			String s;
			while ((s=datei.readLine()) != null) 
			{
				text.addElement(s);
			}
			zeilen = text.size();
		}
		catch(Exception mal) 
		{
			text.addElement("Welcome!");
			text.addElement("Willkommen!");
			text.addElement("Accueil!");
			zeilen = 3;
		}
		incr = 1;
		y = 0;
		z = 0;

		Buffer = createImage(size().width,size().height);
		gBuffer = Buffer.getGraphics();
		gBuffer.setColor(new Color(backR, backG, backB));
		gBuffer.fillRect(0, 0, size().width, size().height);
		gBuffer.setFont(faderfont);
	}

	public void paint(Graphics g)
	{
		String zeile = (String)text.elementAt(y);
		gBuffer.setColor(farbe[z]);
		gBuffer.drawString(zeile , spaceLeft, spaceTop);
		if (z==255) 
		{
			incr = -1;
		}
		if (z==0)
		{
			incr = 1;
			y++;
			if (y==zeilen)
			{
				y=0;
			}
		}
		z = z + (1 * incr);
		g.drawImage(Buffer,0,0,this);

	}
}
