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



/*
 *      Purpose: The main program of the 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, this));

  }
}


/*
 *      Purpose: The container panel for the whole applet
 */
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);
  }

}



/*
 *      Purpose: Drawing the text description   
 */
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][6];

  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,";
   Description[0][1] = "(10) and exchange it with the value stored in the";
   Description[0][2] = "last array location (5).";
   Description[0][3] = "";
   Description[0][4] = "";
   Description[0][5] = "";
   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.";
   Description[1][5] = "";
   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] = "";
   Description[2][5] = "";
   AnimationNext[2] = false;

   Description[3][0] = "Now, repeat the 'select and exchange' process,";
   Description[3][1] = "but each time we don't need to search the blue ";
   Description[3][2] = "elements and we exchange the largest value in ";
   Description[3][3] = "the black elements with last black element. After";
   Description[3][4] = "each exchange, the last black element become ";
   Description[3][5] = "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] = "";
   Description[4][5] = "";
   AnimationNext[4] = true;

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

  }

  /*
   *	Purpose: build in function
   */
  public void paint(Graphics g) {
   g.drawRect(0, 0, size().width-1, size().height-1);

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


  /*
   *    Purpose: executed when the next button is pushed
   */
  public void Next() {
   NextStep++;
   repaint();
  }


  /*
   *    Purpose: reset this panel to the begining situation
   */
  public void reset() {
   NextStep=0;
   repaint();
  }

}



/*
 *      Purpose: the control panel
 */
class SControlPanel extends Panel {
  SMainPanel MainArea;
  Button b1, b2, b3, bsound;
  Checkbox cb_sound;
  Applet applet;
  boolean voice=false;
  AudioClip ac=null;

  String sound[] = new String[6];

  public SControlPanel(SMainPanel m, Applet app) {
   MainArea = m;
   applet = app;

   sound[0] = "sound/select1.au";
   sound[1] = "sound/select2.au";
   sound[2] = "sound/select3.au";
   sound[3] = "sound/select4.au";
   sound[4] = "sound/select5.au";
   sound[5] = "sound/select6.au";

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

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


  /*
   *    build in function to control events
   */
  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();
	cb_sound.disable();
	MainArea.panel1.Next();
	cb_sound.enable();
	b2.enable();
     } else if (str.equals("  Next  ")) {
	b2.disable();
	MainArea.panel2.Next();
	if (voice) {
/*	  applet.play(applet.getCodeBase(), sound[MainArea.panel2.NextStep]);
*/
          if (ac != null) ac.stop();
          ac = applet.getAudioClip(applet.getCodeBase(), sound[MainArea.panel2.NextStep]);
          ac.play();
        }

	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 {
		b2.setLabel("Restart");
		b2.enable();
	   }
	} else {
	   b2.enable();
	}
     } else if (str.equals("Restart")) {
	MainArea.panel1.reset();
	MainArea.panel2.reset();
	b2.setLabel("  Next  ");
	reset();
     } else if (str.equals("Voice Description")) {
        System.out.println(applet.getCodeBase());
        applet.play(applet.getCodeBase(), sound[MainArea.panel2.NextStep]);
     } else if (str.equals("Disable Voice")) {
	voice = false;
        bsound.setLabel("Enable Voice");
     } else if (str.equals("Enable Voice")) {
	voice = true;
	bsound.setLabel("Disable Voice");
        applet.play(applet.getCodeBase(), sound[MainArea.panel2.NextStep]);
     }

   } else if (e.target instanceof Checkbox) {
     Checkbox cb = (Checkbox)e.target;
     String str = cb.getLabel();
     
     if (str.equals("Voice Description")) {
       if (cb.getState()) {
         voice = true;
/*         applet.play(applet.getCodeBase(), sound[MainArea.panel2.NextStep]);
*/
         if (ac != null) ac.stop();
         ac = applet.getAudioClip(applet.getCodeBase(), sound[MainArea.panel2.NextStep]);
         ac.play();
         
       } else {
         voice = false;
         ac.stop();
       }
     }
 
   }

   return true;
  }


  /*
   *    Purpose: reset this panel to the initial situation
   */
  public void reset() {
   b1.enable();
   b2.disable();
  }

}
