import java.awt.*;
import java.io.*;

public class CentralPerk extends Canvas implements Runnable {

  private String msg;
  private int x, y, width, height;
  private int rate = 100;
  private Thread mover;
  private boolean stopped = false;

  public CentralPerk () {
    this ("Coffee: 10 kurus");
  }
  public CentralPerk (String s) {
    this (s, 200, 200);
  }
  public CentralPerk (String s, int width, int height) {
    msg = s;
    this.width = width;
    this.height = height;
    setSize (getPreferredSize());
    initMoving();
  }
  private void initMoving() {
    mover = new Thread(this);
    mover.start();
  }
  public Dimension getPreferredSize () {
    return new Dimension (width, height);
  }
  public void paint (Graphics g) {
    Dimension d = getSize();
    if (x <= 0) {
      // Restart scrolling
      x = d.width;
      y = d.height / 2;
    } else {
      x--;
    }
    g.drawString (msg, x, y);
  }

// Properties

  public void setMessage(String message) {
    msg = message;
    repaint();
  }
  public String getMessage () {
    return msg;
  }

// Animation

  public void start () {
    startMoving();
  }
  public void stop () {
    stopMoving();
  }
  public void run () {
    try {
      while(true) {
        // First wait until the animation is not stopped.
        synchronized (this) {
          while (stopped || !isEnabled()) {
            wait();
          }
        }
        // Now draw the current frame.
	  repaint();
        mover.sleep(rate);
      }
    } catch (InterruptedException e) {
      // Ignore
    }
  }
  public synchronized void setEnabled(boolean x) {
    super.setEnabled(x);
    notify();
  }
  public synchronized void startMoving() {
    stopped = false;
    notify();
  }
  public synchronized void stopMoving() {
    stopped = true;
  }

// Property Changing


// Event Listeners


// Serialization

}

