import java.awt.*;
import java.util.*;
import mkgray.gui.util.*;


public class APeopleSpace extends Component {
  DoubleBuffer db;
  Vector people = new Vector();
  Hashtable byName = new Hashtable();

  public static void main(String args[]){
    new APeopleSpace();
  }

  APeopleSpace (){
    APerson[] gang = new APerson[25];
    Frame f = new Frame();
    f.setBounds(0, 0, 500, 500);
    setBounds(0, 0, 500, 500);
    f.add(this);
    Random rnd = new Random();
    for(int ct=0; ct<25;ct++){
      Color rcol;
      if(rnd.nextFloat() > .5)
	rcol = Color.red;
      else
	rcol = Color.blue;
      gang[ct] = new APerson(new Long(rnd.nextLong()).toString(),
			     rcol, this);
    }
    for(int ct=0; ct<25;ct++){
      for(int ct2=0; ct2<3;ct2++){
	int rval = (int) (25*rnd.nextFloat());
	gang[ct].affinity(gang[rval].name);
      }
      for(int ct2=0; ct2<3;ct2++){
	int rval = (int) (25*rnd.nextFloat());
	gang[ct].distaste(gang[rval].name);
      }
    }    
    for(int ct=0; ct<25;ct++){
      addPerson(gang[ct]);
    }    
    f.show();
    while(true){
      for(int ct=0; ct<25;ct++){
	gang[ct].tick();
      }
      repaint();
      f.repaint();
      try { Thread.currentThread().sleep(30); } catch (Exception e) {}
    }
  }

  public void addPerson(APerson ap){
    people.addElement(ap);
    byName.put(ap.name, ap);
  }

  public void update(Graphics g){
    if(db == null)
      db = new DoubleBuffer();
    db.update(this, g);
  }

  public void paint(Graphics g){
    g.translate(this.getSize().height/2, this.getSize().width/2);
    for(Enumeration e = people.elements(); e.hasMoreElements();){
      APerson p = (APerson) e.nextElement();
      p.draw(g);
    }
    g.setColor(Color.white);
  }

  public boolean collision(double x, double y, int radius){
    if((Math.abs(x)+radius) > (this.getSize().width/2))
      return true;
    if((Math.abs(y)+radius) > (this.getSize().height/2))
      return true;
    return false;
  }
}
