/*
   draw: base implementation of drawing applet
*/

import java.awt.*;

public class Draw extends BufferedApplet
{
   int w = 0, h = 0;
   Graphics g;

   int nItems = 0;
   Item item[] = new Item[100];

   Item newItem() { return (Item)(new Item()); }

   public void render(Graphics g) {
      this.g = g;
      if (w == 0)
         renderInit();
      renderBg();
      renderFg();
   }

   public void renderInit() {
      w = bounds().width;
      h = bounds().height;
   }

   public void renderBg() {
      g.setColor(bgColor());
      g.fillRect(0, 0, w, h);
   }

   public Color bgColor() {
      return bgColorData;
   }

   public void renderFg() {
      g.setColor(Color.black);
      for (int i = 0 ; i < nItems ; i++)
         item[i].draw(g);
   }

   public boolean mouseDown(Event e, int x, int y) {
      startItem(x, y);
      damage = true;
      return true;
   }

   public void startItem(int x, int y) { 
      item[nItems++] = newItem();
      item[nItems-1].add(x, y);
   }

   public boolean mouseDrag(Event el, int x, int y) {
      item[nItems-1].add(x, y);
      damage = true;
      return true;
   }

   public boolean mouseUp(Event el, int x, int y) {
      damage = true;
      return true;
   }

   Color bgColorData = Color.white;
}


