//Guillermo Escobar P  cod  256243

	
	import java.applet.*;
	import java.awt.*;
	import java.awt.event.*;
	

	public class minsoranimado extends Applet implements Runnable, ActionListener,  AdjustmentListener {
		
		private static final long serialVersionUID = 1L;
		private Thread thread;
		private Rectangle Space1, Space2, Space3;
		private Rectangle FullSpace;
		private Button start, stop, recargar;
		int width =800;
		int height =800;
		private Image Scratch;
		private Graphics offg;
		private Color BorderColor = Color.green.darker();
		private Color labelcolor =new Color(184, 60, 236);
		private Font labelfont = new Font("ARIAL", Font.BOLD , 12);
		private Item ArrayItem[];
		private int ArraySize = 20;
		private Item temp, floor;
		private int flooridx, currentidx;
		private boolean isRunning = false;
		private boolean sortingdone;
		private int wait = 500;
		private Scrollbar speed;
		private Label status1 = new Label("");
		private Label status2 = new Label("Speed : "+wait/10);
		private Label status3 = new Label("");
		private String line1 = "int CurrentIndex = 0;";
		private String line2 = "int floorIndex = 0;";
		private String line3 = "for (flooridx=0 ; flooridx < ArrayItem.length; flooridx++) {";
		private String line4 = "    temp = ArrayItem[flooridx];";
		private String line5 = "    for ( currentidx = flooridx+1; currentidx < ArrayItem.length; currentidx++) {";
		private String line6 = "        if (ArrayItem[currentidx] < temp){";
		private String line7 = "        ArrayItem[flooridx]=ArrayItem[currentidx]";
		private String line8 = "        ArrayItem[currentidx]=temp";
		private String line9 = "        temp=ArrayItem[flooridx]";
		private String line10 = "        } else {";
		private String line11 ="        ArrayItem[flooridx]=temp ";
		private String line12 ="        }";
		private int line = 0;
		

//metodo que maneja los eventos
	public void actionPerformed(ActionEvent e) {
		
		if (e.getActionCommand().equals("Iniciar")){
			startanimation();	
		}

		if (e.getActionCommand().equals("Deterner")) {
			stopanimation();
			
		}

		if (e.getActionCommand().equals("Recargar")){

			if (!sortingdone) {
				status3.setText("No puede recargar mientras ordena");
			}
			else {
				status3.setText("");
				isRunning=false;
				line = 0;
				defineArray();
				status1.setText("Presione iniciar para ordenar");
			}
			
			repaint();
		}
		}

	public void addCode() {

		if (line == 0) {

		}else if (line == 1) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+27, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+28, 399, 14);
		}else if (line == 2) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+42, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+43, 399, 14);
		}else if (line == 3) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+57, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+58, 399, 14);
		}else if (line == 4) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+72, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+73, 399, 14);
		}else if (line == 5) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+87, 427, 15);
			offg.setColor(new java.awt.Color(300,228,242));
			offg.fillRect(Space1.x+6, Space1.y+88, 426, 14);
			
		}else if (line == 6) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+102, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+103, 399, 14);
		}else if (line == 7) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+117, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+118, 399, 14);
		}else if (line == 8) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+132, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+133, 399, 14);
		}else if (line == 9) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+147, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+148, 399, 14);
		}else if (line == 10) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+162, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+163, 399, 14);
		}else if (line == 11) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+177, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+178, 399, 14);
		}else if (line == 12) {
			offg.setColor(new java.awt.Color(229,201,227));
			offg.drawRect(Space1.x+5, Space1.y+192, 400, 15);
			offg.setColor(new java.awt.Color(249,228,242));
			offg.fillRect(Space1.x+6, Space1.y+193, 399, 14);
		}else {}

		

		offg.setFont(labelfont);
		offg.setColor(Color.black);
		offg.drawString(line1, Space1.x+10, Space1.y+40);
		offg.drawString(line2, Space1.x+10, Space1.y+55);
		offg.drawString(line3, Space1.x+10, Space1.y+70);
		offg.drawString(line4, Space1.x+10, Space1.y+85);
		offg.drawString(line5, Space1.x+10, Space1.y+100);
		offg.drawString(line6, Space1.x+10, Space1.y+115);
		offg.drawString(line7, Space1.x+10, Space1.y+130);
		offg.drawString(line8, Space1.x+10, Space1.y+145);
		offg.drawString(line9, Space1.x+10, Space1.y+160);
		offg.drawString(line10, Space1.x+10, Space1.y+175);
		offg.drawString(line11, Space1.x+10, Space1.y+190);
		offg.drawString(line12, Space1.x+10, Space1.y+205);
		
		
		
	}


	public void addControls() {

		start = new Button("Iniciar");
		stop = new Button("Detener");
		recargar = new Button("Recargar");

		speed = new Scrollbar(Scrollbar.HORIZONTAL, 0,0,0,100);	

		status1.setFont(labelfont);
      status1.setText("Presione inicio");
		status1.setBackground(Color.white);
		status2.setBackground(Color.white);
		status3.setBackground(Color.white);
		status2.setFont(labelfont);
		status2.setText("Velocidad : "+wait/10);
		status3.setFont(labelfont);
		status3.setText("");
		

		add(start);
		add(stop);
		add(recargar);
		add(speed);
		add(status1);
		add(status2);
		add(status3);

		start.addActionListener(this);
		stop.addActionListener(this);
		recargar.addActionListener(this);
		speed.addAdjustmentListener(this);
		speed.setValue(wait/10);

		Rectangle c1 = new Rectangle(Space2.x+5, Space2.y+5, Space2.width-12, Space2.height-(Space2.height*2/3));
		Rectangle c2 = new Rectangle(Space2.x+5, c1.height+8, c1.width, c1.height);
		Rectangle c3 = new Rectangle(c2.x, 2*c2.height+11, c2.width, c2.height-16);

		start.setBounds(c1.x+10, c1.y+c1.height/2-12, 60, 25);
		stop.setBounds(c1.x+80, c1.y+c1.height/2-12, 60,25);
		recargar.setBounds(c1.x+90+60, c1.y+c1.height/2-12, 60,25);
		speed.setBounds(c2.x+10, c2.y+c2.height/2, c2.width-40, 10);
		status1.setBounds(c3.x+10, c3.y+14, c3.width-10, c3.height/3);
		status2.setBounds(c2.x+10, c2.y+c2.height/2-20, c2.width-10,20);
		status3.setBounds(c3.x+10, c3.y+c3.height/3+14, c3.width-10, c3.height/3);

		
		
		
			
	}

	public void adjustmentValueChanged(AdjustmentEvent e) {

		int  w = e.getValue();
		
		status2.setText("Velocidad : "+ w);
	if (w <=20 && w>10){
			wait = (100-w)*10;
		}
		else if ( w <= 10 && w > 5){
			wait = (100-w)*10;
		}
		else if(w<=5 && w>=0){
			wait = (100-w)*10;
		}
		else {
			wait = (100-w)*10;
		}

	}
	public void defineArray() {

		ArrayItem = new Item[ArraySize];
		Rectangle s3 = new Rectangle(Space3.x+40, Space3.y, Space3.width, Space3.height);
		temp = new Item("t" , 0, s3.x+25, s3.y+s3.y/2, new java.awt.Color(249,234,244));
		floor = new Item("f", 1, s3.x+25, s3.y+s3.y/2-60, Color.white);
		int i;
		int j=1;

		for ( i=0; i<ArrayItem.length; i++) {
			
			ArrayItem[i] = new Item(""+(int)((java.lang.Math.random()*100)), i ,s3.x+j*25, s3.y+s3.y/2-30,Color.blue);	
			j++;
			
		}	
		
	}

	public void defineLayout() {

		FullSpace =  new Rectangle(0, 0, width, height);
		Space1 = new Rectangle(0,0, width*2/3, height/2);
		Space2 = new Rectangle(width*2/3, 0, width*1/3, height/2);
		Space3 = new Rectangle(0, height/2, width, height/2);

		
	}
	public void drawArray() {

		int i;
		for (i=0; i<ArrayItem.length; i++) {

			
			offg.setColor(Color.black);
			offg.drawRect(ArrayItem[i].getX()-1, ArrayItem[i].getY()-1, 21,21);
			offg.setColor(ArrayItem[i].getColor());
			offg.fillRect(ArrayItem[i].getX(), ArrayItem[i].getY(),20,20);
			offg.setColor(Color.black);
			offg.drawString(""+ArrayItem[i].getValue(), ArrayItem[i].getX()+2, ArrayItem[i].getY()+15);	
		}

		offg.setColor(Color.black);
		offg.drawRect(ArrayItem[temp.getApos()].getX()-1, temp.getY()-1, 21,21);
		offg.setColor(temp.getColor());
		offg.fillRect(ArrayItem[temp.getApos()].getX(), temp.getY(),20,20);
		offg.setColor(Color.black);
		offg.setFont(labelfont);
		offg.drawString(""+temp.getValue(), ArrayItem[temp.getApos()].getX()+2, temp.getY()+15);
		
		offg.setColor(Color.black);
		offg.setFont(labelfont);
		offg.drawString(""+floor.getValue(), ArrayItem[floor.getApos()].getX()+9, floor.getY()+20);	
		
	}

	public void drawBorder() {
		

		offg.setColor(Color.white);
		offg.fillRect(FullSpace.x, FullSpace.y, FullSpace.width, FullSpace.height);
		offg.fillRect(Space1.x+2, Space1.y+2, Space1.width-4, Space1.height-4);
		offg.fillRect(Space2.x+2, Space2.y+2, Space2.width-6, Space2.height-4);
		offg.fillRect(Space3.x+2, Space3.y+2, Space3.width-6, Space3.height-6);
		
		offg.setColor(BorderColor);
		offg.drawRect(FullSpace.x, FullSpace.y+1, FullSpace.width-1, FullSpace.height-4);

		Rectangle c1 = new Rectangle(Space2.x+5, Space2.y+5, Space2.width-12, Space2.height-(Space2.height*2/3));
		Rectangle c2 = new Rectangle(Space2.x+5, c1.height+8, c1.width, c1.height);
		Rectangle c3 = new Rectangle(c2.x, 2*c2.height+11, c2.width, c2.height-16);
		Rectangle s1 = new Rectangle(Space1.x+2, Space1.y+2, Space1.width-4, Space1.height-4);
		Rectangle s2 = new Rectangle(Space2.x+2, Space2.y+2, Space2.width-6, Space2.height-4);
		Rectangle s3 = new Rectangle(Space3.x+2, Space3.y+2, Space3.width-6, Space3.height-8);
		Rectangle CR = new Rectangle(Space3.x+160, Space3.y+250, Space3.width/2, 40);
		Rectangle c4 = new Rectangle(s3.width-160, s3.y+20, 150, 40);

		offg.setColor(BorderColor);
		offg.drawRect(c1.x, c1.y+8, c1.width, c1.height-8);
		offg.drawRect(c2.x, c2.y+8, c2.width, c2.height-8);
		offg.drawRect(c3.x, c3.y+8, c3.width, c3.height-8);
		offg.drawRect(s1.x, s1.y+8, s1.width, s1.height-8);
		offg.drawRect(s3.x, s3.y+8, s3.width, s3.height-8);
		offg.drawRect(s2.x, s2.y+8, s2.width, s2.height-8);
		offg.drawRect(CR.x, CR.y, CR.width, CR.height);
		offg.drawRect(c4.x, c4.y, c4.width, c4.height);
		offg.setColor(Color.black);
		offg.drawRect(c1.x+9, c1.y+c1.height/2-13, 61, 26);
		offg.drawRect(c1.x+79, c1.y+c1.height/2-13, 61,26);
		offg.drawRect(c1.x+90+59, c1.y+c1.height/2-13, 61,26);
		offg.setColor(Color.white);
		offg.fillRect(c1.x+7,c1.y, 104, 20);
		offg.fillRect(c2.x+7, c2.y, 84,20);
		offg.fillRect(c3.x+7, c3.y, 64,20);
		offg.fillRect(s1.x+7, s1.y+2, 66,20);
		offg.fillRect(s3.x+7, s3.y, 95,20);
		
		offg.setColor(labelcolor);
		offg.setFont(labelfont);
		offg.drawString("Control de la Animacion", c1.x+9, c1.y+12);
		offg.drawString("Control de velocidad", c2.x+9, c2.y+12);
		offg.drawString("Estado", c3.x+9, c3.y+12);
		offg.drawString("Codigo ", s1.x+9, s1.y+12);
		offg.drawString("Animacion ", s3.x+9, s3.y+12);
		offg.drawString(" f = floor Index ", c4.x+20, c4.y+20);
		offg.drawString(" t = Temporal ", c4.x+20, c4.y+35);

			
	}

	public String getAppletInfo() {
		return "InsertionSort\n" + 
			"\n" + 
			"Creation date: (5/9/2001 10:11:58 AM)\n" + 
			"@author: Mohammad Khurshid Ali\n" + 
			"";
	}

	public void init() {

		setLayout(null);
		defineLayout();
		
		Scratch = createImage(FullSpace.width, FullSpace.height);
		offg = Scratch.getGraphics();
		drawBorder();
		addControls();
		addCode();
		defineArray();
		
		
	}
	public void paint(Graphics g) {

		drawBorder();
		drawArray();
		addCode();
		g.drawImage(Scratch,0,0,this);
		
	}
	
	
		public void run() {
		while (true) {
			try {
				Thread.sleep(200);
			}
			catch (InterruptedException e) {}

			

			int ti=50;
			while(!isRunning){}
			line =1;
			currentidx= 0;
			repaint();
			try { Thread.sleep(ti);} catch (InterruptedException e) {}
			flooridx = 0;
			line =2;
			try { Thread.sleep(ti);} catch (InterruptedException e) {}
			repaint();
			
			for (flooridx=0 ; flooridx < ArrayItem.length; flooridx++) {
				line =3;
				try { Thread.sleep(ti);} catch (InterruptedException e) {}
				repaint();
				sortingdone = false;
				while(!isRunning){}
				temp.setValue(ArrayItem[flooridx].getValue());
				temp.setColor(ArrayItem[flooridx].getColor());
				
				
				line = 4;
				try { Thread.sleep(wait);} catch (InterruptedException e) {}
				repaint();
				for ( currentidx = flooridx+1; currentidx < ArrayItem.length; currentidx++) {
					floor.setApos(currentidx);
					line=5;
					try { Thread.sleep(ti);} catch (InterruptedException e) {}
					repaint();
					temp.setApos(flooridx);
					while(!isRunning){}
					try { Thread.sleep(wait);} catch (InterruptedException e) {}
					repaint();
					if (Integer.parseInt(ArrayItem[currentidx].getValue()) < Integer.parseInt(temp.getValue())){
						while(!isRunning){}
						line = 6;
						try { Thread.sleep(wait);} catch (InterruptedException e) {}
						repaint();
						
						ArrayItem[flooridx].setValue(ArrayItem[currentidx].getValue());
						ArrayItem[flooridx].setColor(ArrayItem[currentidx].getColor());
						line = 7;
						while(!isRunning){}
						try { Thread.sleep(wait);} catch (InterruptedException e) {}
						
						ArrayItem[currentidx].setValue(temp.getValue());
						ArrayItem[currentidx].setColor(temp.getColor());
						
						temp.setValue(ArrayItem[flooridx].getValue());
						temp.setColor(ArrayItem[flooridx].getColor());
						line = 8;
						try { Thread.sleep(ti);} catch (InterruptedException e) {}
						repaint();
					
						
					}
					else{
						line = 10;
						try { Thread.sleep(120);} catch (InterruptedException e) {}
						repaint();
						ArrayItem[flooridx].setValue(temp.getValue());
						ArrayItem[flooridx].setColor(temp.getColor());
						while(!isRunning){}
						try { Thread.sleep(wait);} catch (InterruptedException e) {}
					}	
				}
				
			}
		
			status3.setText("Ordenamiento terminado");
			status1.setText("presione el boton de recarga");
			sortingdone=true;
			isRunning = false;
			line = 13;
			repaint();
		}
		
		
		}


	public void start() {
		if (thread == null){
			thread = new Thread(this);
			thread.start();
		}
	}


	public void startanimation() {
		
		status1.setText("Animacion Iniciando");
		status3.setText("");
		isRunning = true;
			
	}

	public void stop() {
		thread = null;
	}

	public void stopanimation() {

		status1.setText("Animation stopped. Press start");
		status3.setText("");
		isRunning = false;	
		
	}

	public final void update(Graphics g) {

		paint(g);	
		
	}
}