import java.awt.*;
import java.util.*;

/**
 * Photo Java
 * BR Juin 96
 *
 */
public
class Photo extends java.applet.Applet implements Runnable {
  private MediaTracker tracker;
  private Window window;
  private Label label;
  private Thread animator;
  private String imageName, cursorName, imageMort;
  private Image image;
  private Image cursor;
  private Image mort;
  private Image offImage, baseImage;
  private Graphics offGraphics, baseGraphics;
  private int delay;
  private int cursorX = 0;
  private int cursorY = 0;
  private int cursorXOld = 0;
  private int cursorYOld = 0;
  private int cursorWidth;
  private int cursorHeight;
  private int imageWidth;
  private int imageHeight;
  private int mortWidth;
  private int mortHeight;
  private boolean mouseLocked = false;
  private int clippingX = 0;
  private int clippingY = 0;
  private int clippingWidth = 0;
  private int clippingHeight = 0;
  private boolean clippingOn = false;
  private boolean init = true;
  private Vector listePersonne;
  
  public String getAppletInfo() {
    return "La page des Cooperants Copyright Benoit Rigaut Juin 1996";
  }

  private Personne getPersonne(String name) {
    int x, y;
    String n;
    String value = getParameter(name);
    if (value == null)
      return null;
    try {
      StringTokenizer st = new StringTokenizer(value, ",");
      n = st.nextToken();
      x = Integer.parseInt(st.nextToken());
      y = Integer.parseInt(st.nextToken());
    } catch (NoSuchElementException e) return null;
    catch (NumberFormatException e) return null;
    return new Personne(n, x, y);
  }

  public void init() {
    int i = 0;
    Personne r;
    listePersonne = new Vector();
    while ((r = getPersonne("personne" + i)) != null) {
      listePersonne.addElement(r);
      i++;
    }

    System.out.println("La page des Cooperants Copyright Benoit Rigaut Juin 1996");
    tracker = new MediaTracker(this);
    window = new Frame("Nom de la personne");
    label = new Label("", Label.CENTER);
    window.setLayout(new GridLayout(1, 1));
    window.add(label);
    window.resize(200, 50);
 
    imageName = getParameter("imageName");
    imageMort = getParameter("imageMort");
    cursorName = getParameter("cursorName");
    String delayString = getParameter("delay");
    delay = (delayString != null) ? Integer.parseInt(delayString) : 250;
    image = getImage(getCodeBase(), imageName);
    tracker.addImage(image, 0);
    cursor = getImage(getCodeBase(), cursorName);
    tracker.addImage(cursor, 0);
    mort = getImage(getCodeBase(), imageMort);
    tracker.addImage(mort, 0);
  }
  
  public void start() {
    if (window != null)
      window.show();
    
    if (animator == null) {
      animator = new Thread(this);
      animator.start(); 
    }
  }

  public void run() {

    while (Thread.currentThread() == animator) {
      long tm = System.currentTimeMillis();
      
      try 
        tracker.waitForID(0);
      catch (InterruptedException e) {
        animator.stop();
        animator = null;
      }
      
      
      
      if ((cursor != null) && (image != null) && (mort != null)) {
        cursorWidth = cursor.getWidth(this);
        cursorHeight = cursor.getHeight(this);
        imageWidth = image.getWidth(this);
        imageHeight = image.getHeight(this);
        mortWidth = mort.getWidth(this);
        mortHeight = mort.getHeight(this);
        
        if (offGraphics == null) {
          baseImage = createImage(imageWidth, imageHeight);
          baseGraphics = baseImage.getGraphics();
          baseGraphics.drawImage(image, 0, 0, null);
          offImage = createImage(imageWidth, imageHeight);
          offGraphics = offImage.getGraphics(); 
          offGraphics.drawImage(baseImage, 0, 0, null);
          offGraphics.setColor(Color.black);
          offGraphics.setFont(new Font("Helvetica", Font.PLAIN, 40));
          clippingOn = false;
          repaint();
        }
        else if (lockingMouse()) {
          offGraphics.drawImage(baseImage, 0, 0, null);
          offGraphics.drawImage(cursor, cursorX, cursorY, null);  
          clippingOn = true;
          clippingX = Math.min(cursorXOld, cursorX) - mortWidth/2;
          clippingY = Math.min(cursorYOld, cursorY) - mortHeight/2;
          clippingWidth = Math.abs(cursorXOld - cursorX) + cursorWidth + mortWidth/2;
          clippingHeight = Math.abs(cursorYOld - cursorY) + cursorHeight + mortHeight/2;
          
          int c = cooperantUntel(cursorX, cursorY, true);
          if (c != -1)
            label.setText(((Personne)listePersonne.elementAt(c)).name());
          else
            label.setText("Tous MORTS !");
          cursorXOld = cursorX;
          cursorYOld = cursorY;
          repaint();
        }
      }
      getAppletContext().showStatus("La page des Cooperants");
     
      tm += delay;
      try {
        Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
      } catch (InterruptedException e) {
        break;
      }    
    }
  }
  
  private int cooperantUntel(int x, int y, boolean b) {
    int min = Integer.MAX_VALUE;
    int lequel = -1;
    for (int i = 0; i < listePersonne.size(); i++) 
      if (!(((Personne) listePersonne.elementAt(i)).estMort() && b)) {
        int d = (int) ( Math.pow(x-((Personne) listePersonne.elementAt(i)).posX(),2) + 
                       Math.pow(y-((Personne) listePersonne.elementAt(i)).posY(),2) );
        if (d < min) {
          min = d;
          lequel = i;
        }
    }
    return lequel;
  }

  private synchronized boolean lockingMouse() {
    if (mouseLocked)
      return false;
    else {
      mouseLocked = true;
      return true;
    }
  }
  private void unlockingMouse() {
    mouseLocked = false;
  }


  public boolean mouseMove(Event evt, int x, int y) {
    if (lockingMouse()) {
      cursorX = x;
      cursorY = y;
      unlockingMouse();
    }
    return true;
  } 
  
  public void stop() {
    if (animator != null) {
      animator.stop();
      animator = null;
    }
    window.hide();
}
 
  public void paint(Graphics g) {
    if (offGraphics != null)
      g.drawImage(offImage, 0, 0, null);
    unlockingMouse();
  }
  
  public void update(Graphics g) {
    if (clippingOn) 
      g.clipRect(clippingX, clippingY, clippingWidth, clippingHeight);
    paint(g);
  }
  
  public boolean mouseDown(Event e, int x, int y) {
    int c = cooperantUntel(x, y, false);
    if (c != -1)
      ((Personne) listePersonne.elementAt(c)).tue();
    if ((tracker.statusID(0, false) & MediaTracker.COMPLETE) != 0)
      baseGraphics.drawImage(mort,
                             x - mortWidth/2, y - mortHeight/2, 
                             null);
    return true;
  }
}

class Personne {
  private String name;
  private int posX;
  private int posY;
  private boolean estMort;
  
  Personne(String n, int x, int y) {
    name = new String(n);
    posX = x;
    posY = y;
    estMort = false;
  }
  public String name() {
    return name;
  }
  public int posX() {
    return posX;
  }
  public int posY() {
    return posY;
  }
  public boolean estMort() {
    return estMort;
  }
  public void tue() {
    estMort = true;
  }
}







