import java.lang.*;
import java.awt.*;
import java.applet.*;


/*
 *	Purpose: the drawing area
 */
class BSearchPanel extends Canvas {
  private int LEFTMARGIN=70;
  private int TOPMARGIN=80;
  private int BOXSIZE=30;
  private int NextStep=0;

  private int UP=-35;
  private int NORMAL=0;
  private int DOWN=35;

  private Font f;
  private long PAUSE_TIME=300;

  private int I=-1;
  private int J=-1;

  public String arr[] = new String[10];
  public Color color[] = new Color[10];;
  public boolean cross[] = new boolean[10];

  private int blue=10;
  private int current=-1;
  private boolean Erase=false;

  private Thread kicker;

  private int MiddleValue=-1;


  public BSearchPanel() {
   /* set background as White */
   setBackground(Color.white);

   f = new Font("Courier", Font.PLAIN, 14);
   reset();

  }	/* End BSearchPanel() */


  /*
   *	Purpose: reset to the initial situation
   */
  public void reset()
  {
   NextStep = 0;
   blue=10;
   current=-1;

   for (int i=0; i<10; i++) {
	arr[i] = "?";
	cross[i] = false;
	color[i] = Color.black;
   }

   repaint();

  }	/* End reset() */


  /*
   *	Purpose: build in function
   */
  public void paint(Graphics g) {

   if (I<0) g.clearRect(0, 0, size().width-1, size().height-1);
   g.drawRect(0, 0, size().width-1, size().height-1);

   g.setFont(f);


   g.drawString("Array", LEFTMARGIN-65, TOPMARGIN+15);
   g.drawString("Content", LEFTMARGIN-65, TOPMARGIN+25);

   for (int i=0; i<10; i++) {
    g.setColor(color[i]);
    DrawElement(g, i);

    if (i==MiddleValue) DrawNotice(g, i);
   }

   g.setColor(Color.black);
   DrawIndex(g);

  }	/* End paint() */



  /*
   *	Purpose: Draw the index of the array
   */
  void DrawIndex(Graphics g) {

    g.drawString("Index", LEFTMARGIN-65, TOPMARGIN+BOXSIZE+30);

    for (int i=0; i<10; i++) {
      g.drawString(Integer.toString(i),
         LEFTMARGIN+(BOXSIZE+1)*i+8, TOPMARGIN+BOXSIZE+30);
    }

  }	/* End Method DrawIndex() */



  /*
   *	Purpose: build in function
   */
  public void update(Graphics g) { 
	this.paint(g); 
  }	/* End update() */



  /*
   *    Parameter: g - Graphics Object
   *               index - index of the element need to be erased
   *    Purpose: Draw the specified element
   */
  public void DrawElement(Graphics g, int index) {

   if (current != index) {
    g.drawRect(LEFTMARGIN+(BOXSIZE+1)*index, TOPMARGIN,
                BOXSIZE, BOXSIZE);
   } else {
    g.drawOval(LEFTMARGIN+(BOXSIZE+1)*index+2, TOPMARGIN+2,
                BOXSIZE-4, BOXSIZE-4);
   }

   if (cross[index]) {
	g.drawLine(LEFTMARGIN+(BOXSIZE+1)*index, TOPMARGIN,
		   LEFTMARGIN+(BOXSIZE+1)*index+BOXSIZE,
		   TOPMARGIN+BOXSIZE);

	g.drawLine(LEFTMARGIN+(BOXSIZE+1)*index+BOXSIZE, TOPMARGIN,
		   LEFTMARGIN+(BOXSIZE+1)*index,
		   TOPMARGIN+BOXSIZE);
   }

/*   g.setColor(Color.black); */
   g.drawString(arr[index],
         LEFTMARGIN+(BOXSIZE+1)*index+8, TOPMARGIN+20);
  }	/* End DrawElement */



  /*
   *	Parameters: g - garphics object
   *                i - where to draw the notice
   *	Purpose: draw the "The Middle Value" notice
   */
  public void DrawNotice(Graphics g, int index) {
   g.drawString(" The", LEFTMARGIN+(BOXSIZE+1)*index-5, TOPMARGIN-35);
   g.drawString("Middle", LEFTMARGIN+(BOXSIZE+1)*index-5, TOPMARGIN-20);
   g.drawString("Value", LEFTMARGIN+(BOXSIZE+1)*index, TOPMARGIN-5);
  }	/* End DrawNotice() */



  /*
   *    Purpose: Do the animation. executed when the "start animation" button
   *              is pressed
   */
  public void Next() {

   switch (NextStep) {
    case 0:
	color[4] = Color.red;
	current = 4;
	arr[4] = "7";
	MiddleValue = 4;
	repaint();
	break;

    case 1:
	current = -1;
	for (int i=0; i<5; i++) {
	  color[i] = Color.blue;
	  cross[i] = true;
	}
	MiddleValue = -1;
	repaint();
	break;

    case 2:
	color[7] = Color.red;
	current = 7;
	arr[7] = "18";
	MiddleValue = 7;
	repaint();
	break;

    case 3:
	current = -1;
	for (int i=7; i<10; i++) {
	  color[i] = Color.blue;
	  cross[i] = true;
	}
	MiddleValue = -1;
	repaint();
	break;

    case 4:
	arr[5] = "9";
	color[5] = Color.blue;
	cross[5] = true;
	current = 6;
	color[6] = Color.red;
	arr[6] = "10";
	repaint();
	break;
	

    default:
   }

   NextStep++;

  }	/* End Next() */

}	/* End Class BSearchPanel */



