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



public class SelectionSort extends Applet {
  public void init() {
   setLayout(new BorderLayout());
   STextPanel WritingArea = new STextPanel();
   SSortPanel DrawingArea = new SSortPanel();
   SMainPanel MainArea = new SMainPanel(DrawingArea, WritingArea);
   add("Center", MainArea);
   add("North", new SControlPanel(MainArea));

  }
}



class SMainPanel extends Panel {
  public SSortPanel panel1;
  public STextPanel panel2;

  public SMainPanel(SSortPanel p, STextPanel t) {
   panel1 = p;
   panel2 = t;

   setLayout(new GridLayout(2, 1));	/* 2 rows, 1 column */
   add(p);
   add(t);
  }

}



class STextPanel extends Panel {
  private int TOPMARGIN=50;
  private int LEFTMARGIN=30;

  public boolean AnimationNext[] = new boolean[15];
  public int NextStep=0;

  private Font f;
  private String Description[][] = new String[15][5];

  public STextPanel() {
   setBackground(new Color(255, 248, 220));	/* set background as cornsilk */

   f  = new Font("Helvetica", Font.PLAIN, 16);


   Description[0][0] = "Search through the array, find the largest value (10),";
   Description[0][1] = "and exchange it with the value stored in the last";
   Description[0][2] = "array location (5).";
   Description[0][3] = "";
   Description[0][4] = "";
   AnimationNext[0] = true;

   Description[1][0] = "Find the second-largest value in the array (9), and";
   Description[1][1] = "exchange it with the value stored in the second last";
   Description[1][2] = "array location (1). This is identical to the previous"; 
   Description[1][3] = "step, except that we don't search the last value --";
   Description[1][4] = "we already know it is the largest.";
   AnimationNext[1] = true;

   Description[2][0] = "The last two blue elements will not be changed";
   Description[2][1] = "further because they are already the largest and";
   Description[2][2] = "the second largest elements in the array.";
   Description[2][3] = "";
   Description[2][4] = "";
   AnimationNext[2] = false;

   Description[3][0] = "Now, repeat the 'select and exchange' process, but";
   Description[3][1] = "each time we don't need to search the blue elements";
   Description[3][2] = "and we exchange the largest values in the black";
   Description[3][3] = "elements with last black element. After each exchange,";
   Description[3][4] = "the last black element become blue.";
   AnimationNext[3] = true;

   Description[4][0] = "The blue part of the array is alreadly sorted and";
   Description[4][1] = "will not be changed further.";
   Description[4][2] = "";
   Description[4][3] = "";
   Description[4][4] = "";
   AnimationNext[4] = true;

   Description[5][0] = "The whole array is sorted now.";
   Description[5][1] = "";
   Description[5][2] = "";
   Description[5][3] = "";
   Description[5][4] = "";
   AnimationNext[5] = true;

  }

  public void paint(Graphics g) {
   g.drawRect(0, 0, size().width-1, size().height-1);

   g.setFont(f);
   for (int i=0; i<5; i++) {
    g.drawString(Description[NextStep][i], LEFTMARGIN, TOPMARGIN+18*i); 
   }
  }


  public void Next() {
   NextStep++;
   repaint();
  }

  public void reset() {
   NextStep=0;
   repaint();
  }

}


class SControlPanel extends Panel {
  SMainPanel MainArea;
  Button b1, b2, b3;
  Checkbox cb_sound;

  public SControlPanel(SMainPanel m) {
   MainArea = m;

   setLayout(new FlowLayout(FlowLayout.LEFT));
   setBackground(Color.lightGray);
   b1 = new Button("Start Animation");
   b2 = new Button("Next");
   b3 = new Button("Restart");
   reset();
   add(b1);
   add(b2);
   add(b3);

   cb_sound = new Checkbox("Voice Description");
   add(cb_sound);
  }


  public boolean action(Event e, Object arg) {
   
   if (e.target instanceof Button) {
     String str = ((Button)(e.target)).getLabel();
     if (str.equals("Start Animation")) {
	b1.disable();
	MainArea.panel1.Next();
	b2.enable();
     } else if (str.equals("Next")) {
	b2.disable();
	MainArea.panel2.Next();

	switch (MainArea.panel2.NextStep) {
	  case 1:
		MainArea.panel1.Next();
		break;
	  default:
	}

	if (MainArea.panel2.AnimationNext[MainArea.panel2.NextStep]) {
	   if (MainArea.panel2.NextStep < 5) b1.enable();
	   else b3.enable();
	} else {
	   b2.enable();
	}
     } else if (str.equals("Restart")) {
	MainArea.panel1.reset();
	MainArea.panel2.reset();
	reset();
     }
   }

   return true;
  }

  public void reset() {
   b1.enable();
   b2.disable();
   b3.disable();
  }

}
