/* histog.java - jpq - 10/11/2000
 * affichage d'un tableau
 * modification et ajout de main() le 13/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;

public class histog extends java.applet.Applet implements Runnable {
  static final long serialVersionUID = 180213L;
  Thread t;
  Font f;
  gtab gt;
  Image img;
  Graphics g;
  int np1, wm2, hm2;
  boolean retrace;

  public void init ()
  { f = new Font ("Arial", Font.PLAIN, 10);
    setBackground (Color.white);
    setFont (f);
  }

  public void start ()
  { if (t == null) { t = new Thread (this); t.start (); } }

  public void stop ()
  { t = null; }

  public void run ()
  { while (true)
    { if (gtab.change)
      { gtab.change = false;
        retrace = true;
        repaint ();
      }
      try { Thread.sleep (100); } catch (Exception e) { } 
    }
  }

  public String getAppletInfo ()
  { return "histog par j.-p. Quelen"; }

  public void update (Graphics g1)
  { paint (g1); }

  public void paint (Graphics g1)
  {  if (img == null || wm2 + 2 != getSize().width || hm2 + 2 != getSize().height)
      { wm2 = getSize().width;
        hm2 = getSize().height;
        img = createImage (wm2, hm2);
        g = img.getGraphics ();
        g.setColor (Color.black);
        g.drawRect (0, 0, -- wm2, -- hm2);
        g.setColor (Color.white);
        g.fillRect (1, 1, -- wm2, -- hm2);
      }
    if (retrace)
    { retrace = false;
      g.setColor (Color.white);
      g.fillRect (1, 1, wm2, hm2);
      g.setColor (Color.yellow);
      int hm20 = hm2 - 18;
      int hm40 = hm20 - 20;
      int wm20 = wm2 - 18;
      int wm30 = wm20 - 10;
      g.drawLine (20, hm20, wm20, hm20);
      g.drawLine (20, 20, 20, hm20);
      g.setColor (Color.blue);
      g.drawString ("0", 5, hm20);
      int gtdim = gtab.dim;
      int jmin = 0;
      while ((jmin < gtdim) && (gtab.tableau [jmin] == 0)) jmin ++;
      int jmax = gtdim - 1;
      while ((jmax >= 0) && (gtab.tableau [jmax]) == 0) jmax --;
      if (jmax == jmin) jmax ++;
      jmax ++;
      if (jmax > gtdim) jmax = gtdim;
      int tmax = 0;
      for (int j = jmin; j < gtdim; j ++)
       if (gtab.tableau [j] > tmax) tmax = gtab.tableau [j];
      g.drawString (Integer.toString (tmax), 2, 20);
      int jminmax = jmax - jmin;
      g.drawString (Integer.toString (jmin), 5, hm2 - 8);
      if (jminmax > 1) g.drawString (Integer.toString (jmax - 1), wm20, hm2 - 8);
      for (int j = jmin; j < jmax; j ++)
      { int x = 20 + (j - jmin) * wm30 / jminmax;
        int y = hm20 - gtab.tableau [j] * hm40 / tmax;
        g.setColor (Color.blue);
        g.setColor (Color.black);
        g.drawLine (x, y, x, hm20);
      }
    }
    g1.drawImage (img, 0, 0, this);
  }

}
////////////////////////////////////////////////////////////////////////////////
class gtab {
  static final long serialVersionUID = 180213L;
  static boolean change;
  static int dim;
  static int [] tableau;

  static public void maj (int d, int [] tab)
  { dim = d;
    tableau = tab;
    change = true;
  }

}
