import java.awt.*;

public class ScrollingBanner
        extends java.applet.Applet implements Runnable {

  protected Thread bannerThread;
  protected String text;
  protected Font font =
    new java.awt.Font("Sans-serif", Font.BOLD, 24);
  protected Color color=Color.green;
  protected int x, y;
  protected int delay = 10;
  protected int xoffset = 1;
  protected int yoffset = 0;
  protected Dimension d;

  public void init() {
    // get parameters "delay" and "text"
    String att = getParameter("delay");
    if (att != null) {
      delay = Integer.parseInt(att);
    }
    att = getParameter("text");
    if (att != null) {
      text = att;
    } else {
      text = "Scrolling banner.";
    }
    att = getParameter("FONT");

	if ("Serif".equals(att)) {
	    font = new Font("Serif", Font.BOLD,48);
	} else if ("SansSerif".equals(att)) {
	    font = new Font("SansSerif", font.BOLD,48);
	} else if ("Dialog".equals(att)) {
		font = new Font("Dialog", Font.BOLD,48);
	} else if ("DialogInput".equals(att)) {
		font = new Font("DialogInput", Font.BOLD,48);
	} else {
	    font = new Font("Monospaced", Font.BOLD,48);
	}
	att = getParameter("color");

    if ("red".equals(att)) {
        color = Color.red;
    } else if ("blue".equals(att)) {
        color = Color.blue;
	} else if ("yelow".equals(att)) {
        color = Color.yellow;
    } else if ("orange".equals(att)) {
		color = Color.orange;
    } else {
	    color = Color.green;
	}
	d = getSize();
	x = d.width;
    y = font.getSize();
    att = getParameter("direction");

	if ("left_to_right".equals(att)) {
    	x = 0;
	    xoffset= -1;
	} else if ("upwards".equals(att)) {
		x = 10;
		y = d.height;
		xoffset = 0;
		yoffset = 1;
    } else if ("downwards".equals(att)) {
		x = 10;
		y = 0;
		xoffset = 0;
		yoffset = -1;
    } else if ("diagonally".equals(att)) {
		x = 0;
		y = 0;
		xoffset = -1;
		yoffset = -1;
    }
    // set initial position of the text

  }


  public void paint(Graphics g) {
    // get the font metrics to determine the length of the text
    g.setFont(font);
    FontMetrics fm = g.getFontMetrics();
    int length = fm.stringWidth(text);
    int height = fm.getHeight();

    // adjust the position of text from the previous frame
    x -= xoffset;
    y -= yoffset;

    // if the text is completely off to the left end
    // move the position back to the right end

    if ((x < -length) && (yoffset == 0))
      x = d.width;

    if ((y < -height) && (xoffset == 0))
      y = d.height;

    if ((x > d.width) && (yoffset ==0 ))
      x = -length;

    if ((y > d.height) && (xoffset == 0))
      y = -height;

    if (((x > d.width)||(y > d.height)) && ((xoffset==-1)&&(yoffset==-1))){
		x = 0;
		y = 0;
    }
    // set the pen color and draw the background
    g.setColor(Color.black);
    g.fillRect(0,0,d.width,d.height);

    // set the pen color, then draw the text
    g.setColor(color);
    g.drawString(text, x, y);
  }

  public void start() {
    bannerThread = new Thread(this);
    bannerThread.start();
  }

  public void stop() {
    bannerThread = null;
  }

  public void run() {
    while (Thread.currentThread() == bannerThread) {
      try {
        Thread.currentThread().sleep(delay);
      }
      catch (InterruptedException e) {}
      repaint();
    }
  }
}