/*
   My First Java Applet
   Copyright 1999 Parker Mundy. All rights reserved.


   Version 1.2  12/06/1999
   Change:
     Intial point is blanked, instead of centered.
     Sizing is dynamic, based on HTML tag, instead of fixed.

   Version 1.1  12/04/1999
   Add:
     Mouseclick will reset screen and change color.

   Version 1.0  12/04/1999
   Features:
     Dot will travel sinusoidal path in both X and Y.
     Increment of X and Y are slightly different so phase varies.
     Screen is not reset so trail can be seen.

     <!-- FreeFind No Index Page -->
*/

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;

public class Lissa extends java.applet.Applet
  implements Runnable
{

  Thread runner;
  int ptx, pty,
      rval, gval, bval,
      appheight, appwidth;
  double x = 0,
         y = 0;
  boolean reset = false;

  public void init() {
    ptx = 0;
    appheight = size().height;
    appwidth = size().width;
  }

  public void start() {
    if (runner == null) {
      runner = new Thread(this);
      runner.start();
    }
  }

  public void stop() {
    if (runner != null) {
      runner.stop();
      runner = null;
    }
  }

  public void update(Graphics g) {
    if (reset) {
      g.setColor(getBackground());
      g.fillRect(0, 0, appwidth, appheight);
      rval = (int)Math.floor(Math.random() * 256);
      gval = (int)Math.floor(Math.random() * 256);
      bval = (int)Math.floor(Math.random() * 256);
      reset = false;
    }
    paint(g);
  }

  public boolean mouseDown(Event evt, int x, int y) {
    reset = true;
    return true;
  }

  public void run() {
    while (true) {
      x += Math.PI / 49;
      ptx = (int) ((appwidth/2) + Math.sin(x) * ((appwidth/2)-10));
      y += Math.PI / 51;
      pty = (int) ((appheight/2) + Math.sin(y) * ((appheight/2)-10));
      repaint();
      try { Thread.sleep(5); }
      catch (InterruptedException e) { }
      if (x == 2 * Math.PI) x = -(2 * Math.PI);
      if (y == 2 * Math.PI) y = -(2 * Math.PI);
    }
  }

  public void paint(Graphics g) {
    if (ptx != 0) {
      g.setColor(new Color(rval,gval,bval));
      g.fillOval(ptx, pty, 3, 4);
    }
  }

}

