/*
 * Bubbles Applet - Bubbles.java
 * Copyright (c) 1996 Aggressive Enterprises, INC. All rights reserved.
 *
 * By Leyth Kedidi
 *
 * $Id: Bubbles.java,v 1.1 1996/04/22 04:29:15 leyth Exp $
 *
 * $Log: Bubbles.java,v $
 * Revision 1.1  1996/04/22 04:29:15  leyth
 * Initial revision
 *
 * Revision 1.1  1996/04/22 04:08:57  leyth
 * Initial revision
 *
 *
 */

import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.Date;
import java.util.Random;
import Bubble;
import AboutDialog;

public class Bubbles extends Applet implements Runnable {
   private AboutDialog dialog;
   Image bubbles[];
   Image image;
   Graphics IG;
   Color bgcolor;
   int nbubbles = 4, maxbubbles = 15;
   int hscroll = 3, vscroll = 5;
   Boolean firstwave;
   Bubble b[];
   Dimension d;
   Thread thread;;
   int stage;
   Random random;

   public void init() {
      dialog = new AboutDialog(getAppletContext(), "Bubbles Applet v1.0");

      String parameter;

      bgcolor = Color.black;
      parameter = getParameter("bgcolor");
      if (parameter != null)
         bgcolor = getRGBColor(parameter);
      setBackground(bgcolor);

      parameter = getParameter("maxbubbles");
      if (parameter != null)
         maxbubbles = Integer.valueOf(parameter).intValue();

      parameter = getParameter("hscroll");
      if (parameter != null)
         hscroll = Integer.valueOf(parameter).intValue();

      parameter = getParameter("vscroll");
      if (parameter != null)
         vscroll = Integer.valueOf(parameter).intValue();
      
      Date now = new Date();
      random = new Random(now.getSeconds());
      bubbles = new Image[nbubbles];
      bubbles[1] = getImage(getDocumentBase(), "images/bubble1.gif");   
      bubbles[2] = getImage(getDocumentBase(), "images/bubble2.gif");   
      bubbles[3] = getImage(getDocumentBase(), "images/bubble3.gif");   
      bubbles[0] = bubbles[3];
      d = size();
      image = createImage(d.width, d.height);
      b = new Bubble[maxbubbles];
      for (int i=0;i<maxbubbles;i++)
         b[i] = new Bubble(random, d.width, d.height+(i+1)*20, nbubbles);
      IG = image.getGraphics();
      stage = 0;
   }

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

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

   public void run() {
      while (true) {
      try {Thread.currentThread().sleep(100);}
         catch (InterruptedException e){}
         scroll(hscroll, vscroll);
      }
   }

   synchronized void scroll(int dx, int dy) {
      for (int i=0;i<maxbubbles;i++) {
         b[i].x += random.nextInt()%dx;
         b[i].y -= Math.abs(random.nextInt()%dy);
         if (b[i].y < - bubbles[b[i].type].getHeight(this))
            b[i].Reset(d.width, d.height, nbubbles);
      }
      repaint();
   }

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

   public void paint(Graphics g) {
      if (stage == 0) {
         g.setColor(bgcolor);
         g.fillRect(0, 0, d.width, d.height);
         stage++;
         return;
      }
      else {
         IG.setColor(bgcolor);
         IG.fillRect(0, 0, d.width, d.height);
         for (int i=0;i<maxbubbles;i++)
            IG.drawImage(bubbles[b[i].type], b[i].x, b[i].y, this);
         g.drawImage(image, 0, 0, this);
      }
   }

   protected Color getRGBColor(String s) {
      Color color = Color.black;

      if (s == null)
         return(color);

      if (s.length() != 7 || !s.startsWith("#"))
         return(color);

      int hex = 0, c = 0, idx = 1;
      int colors[] = new int[3];
      for(int i=0;i<3;i++) {
         colors[i] = 0;
         for (int j=1;j>=0;j--) {
            c = Character.toUpperCase(s.charAt(idx));
            if (c >= 'A' && c <= 'F')
               hex = c - 'A' + 10;
            else if (c >= '0' && c <= '9')
               hex = c - '0';
            else
               return(color);
            idx++;

            colors[i] += j > 0 ? 16*hex : hex;
         }
      }
      color = new Color(colors[0], colors[1], colors[2]);
      return(color);
   }

   public boolean mouseDown(Event event, int x, int y) {
      dialog.show();
      return(true);
   }
}
