/*
   <Applet code = "cferguso.assign4.Main_Kruskal"
           width = 750 height = 600>
   </applet>
 */


/*This version of Main_Kruskal.java has error checking*/ 
package cferguso.assign4; 
import java.awt.*; 
import java.awt.image.*; 
import java.applet.*; 
import java.util.*; 
import java.io.*; 
import java.lang.*; 
import jotsa.*; 

/**
*This class creates the applet that illustrates Kruskals Algorithm 
*Additionally, the layout is determined in this class.  
*For example:
*<pre>
*	Main_Kruskal main_kruskal = new Main_Kruskal();
*</pre>
*@see		awt.Button
*@see		Method#init() 	
*@see  	 	Method#setup_layout()
*@see		Method#intro()
*@see		Method#action(Event e, Object arg) 
*@version	0.1 30 April 1997
*author		Candy Ferguson
*/   
public class Main_Kruskal extends JotsaAnimationApplet{  
	private static int number_of_nodes;
	private static int number_of_edges; 
	private static  final int TRUE = 1; 
	private static  final int FALSE = 0; 
	Original_Tree otree;  
	Minimized_Tree mtree;  
	ZeroMinimized_Tree motree; 
	ZeroOriginal_Tree Ootree; 
	Exit exit; 
	AudioClip[] clip = new AudioClip[2]; 
	Image external_image; 
	int numclip = 2;  
	int i = 0; 
	int j; 
	int node; 
	int lowest_weight; 
	int node1; 
	int node2; 
	int max_weight; 
	int index_low; 
	int index_of_T; 
	int index = 0; 
	int width; 
	int min_weight; 
	int total_weight; 
	int instance;  
	String intro,exitstring; 
	Button Introduction; 
	Button Initial_Graph;  
	Button Minimized_Graph; 
	Button graph_traversals;  
	Button about_applet; 
	Button Popup;  
	int size = 15;  
	int str_start = 150; 
	String str1, str2, str3, str4, str5, str6, str7, str8, str9, str10; 
	int start_x = 350; 
	int start_y = 300; 
	int st_x = 100; 
	int st_y = 30; 
	int sts_x = 360; 
	int sts_y = 30; 
	int oval_width = 50;  
	int oval_height = 30;  
	int zero = 0; 
	public static JotsaSlider speed_control; 
	Choice Imagetype;  
	Choice Sound; 
	int image_type=-1; 
	int popup_count = 0; 
	int scaled_count = 0; 
	JotsaAnimationObject introbj; 
	JotsaAnimationObject obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9,obj10,
			     obj11,obj12,obj13,obj14,obj15,obj16,obj17,obj18,obj19,   
			     obj20,obj21,obj22,obj23,obj24,obj25,obj26; 
	JotsaAnimationObject line12,line13,line14,line23,line25,line43,line35,line36,
			     line46,line56,sline12,sline13,sline14,sline23,sline25,
			     s1line25,sline43,sline35,sline36,s1line13,s2line13,s3line13,
			     sline46,s1line46,s2line46,sline56; 
	JotsaAnimationObject stobj12,stobj13,stobj14,stobj23,stobj25,stobj43,stobj35,stobj36,
			     stobj46,stobj56,sstobj12,sstobj13,sstobj14,sstobj23,sstobj25,sstobj36;
	JotsaAnimationObject sstobj46,ss1obj13,ss2obj13,ss3obj13,ss1obj46,ss1obj25; 



	//Define zero parameter constructor
	public Main_Kruskal(){} 


	/**
	*Creates the applet, calls methods from the Kruskal class to 
	*perform Kruskals Algorithm. Checks if input file is valid 
	*@see Method#Kruskal.Getdata()
	*@see Method#Kruskal.sort()
	*@see Method#Kruskal.Do_Algorithm()
	*
	*
	*/  
	//Setup Applet 
	public void init(){ 
        super.init();
        setup_layout();
        JotsaInitImages();
        width = bounds().width;
	intro(); 
	Kruskal.Getdata();   
	if(Kruskal.getNodes() > 3 && Kruskal.getNodes()<13 && Kruskal.getEdges()> 0
		&& Kruskal.getEdges()<100)  
	{
	   Kruskal.sort(); 
	   Kruskal.Do_Algorithm();  
	} 
	else 
	{ 
	     exit = new Exit(this);  
	}  
	}//End init  
	

	/**
	*Sets up the layout of the applet. Creates buttons, sliders and
	*choice buttons
	*@return Void
	*
	*
	*/ 
	//Set Layout up 
	private void setup_layout(){ 
	setLayout(new BorderLayout()); 
	speed_control = new JotsaSlider(5000,0,10000,1,200,"Tree Speed",this);  
	Imagetype = new Choice(); 
	Sound = new Choice();  
	Imagetype.addItem("ABOUT THIS APPLET"); 
	Imagetype.addItem("GRAPH TRAVERSALS");  
	Imagetype.addItem("KRUSKAL THE MAN");  
	Sound.addItem("SOUND ON"); 
	Sound.addItem("SOUND OFF"); 
	Panel p = new Panel(); 
	Panel q = new Panel(); 
	Panel r = new Panel(); 
	Panel t = new Panel();  
	p.setLayout(new GridLayout(2,1)); 
	q.setLayout(new GridLayout(1,2));  
	r.setLayout(new GridLayout(1,2));  
	t.setLayout(new GridLayout(1,3));  
	q.add(Introduction = new Button("Introduction")); 
	r.add(Initial_Graph = new Button("Original Tree")); 
	r.add(Minimized_Graph = new Button("Minimized Tree")); 
	t.add(Imagetype);     
	t.add(Sound); 
	p.add(q); 
	p.add(r); 
	p.add(t);   
	p.add(speed_control); 
	add("South",p); 
	add("Center",JotsaDefaultCanvas); 
	validate(); 
	} 
	



	/**
	*Create an introductory screen for the applet user. The introductory screen
	*includes test and JotsaAnimationObjects by Dr. Steven Robbins.  This method
	*returns void 
	*
	*
	*/ 
	public void intro(){ 
	JotsaRemoveAllObjects(); 
	intro = new String("An algorithm is a sequence of instructions that terminates on any\n");
	intro +="input.  Thus, a program is an algorithm as long as it never\n";  
	intro +="enters an infinite loop on any input.  Kruskals Algorithm is a \n";  
	intro +="Minimum Cost Spanning tree Algorithm. A typical application \n";  
	intro +="for minimum-cost spanning trees occurs in the design of\n";  
	intro +="communication networks.   Suppose G = (V,E) is a connected\n";  
	intro +="graph in which each edge(u,v) in E has a cost c(u,v) attached\n";  
	intro +="to it. A spanning tree for G is a free tree that connects all\n";  
	intro +="the vertices in V.  The cost of a spanning tree is the sum of\n";  
	intro +="the costs of the edges in the tree. Select finding a minimum\n";  
	intro +="-cost spanning tree for the graph labeled G.\n";  
	intro +="This applet was written by Candy Ferguson.\n"; 
	intro +="\n";  
	intro +="\n";  
	intro +="\n";  
	intro +="\n";  
	introbj = new JotsaAnimationObject(str_start,50,1,99,this); 
	introbj.set_draw_strings(intro,size,Color.blue); 
	str1 = "1"; 
	obj1 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	obj1.set_draw_centered_oval_string(oval_width,oval_height,str1,Color.blue,Color.black); 
	str2 = "2"; 
	obj2 = new JotsaAnimationObject(start_x - 90,start_y + 50,1,1,this); 
	obj2.set_draw_centered_oval_string(oval_width,oval_height,str2,Color.blue,Color.black); 
	str3 = "3"; 
	obj3 = new JotsaAnimationObject(start_x ,start_y + 90,1,1,this); 
	obj3.set_draw_centered_oval_string(oval_width,oval_height,str3,Color.blue,Color.black); 
	str4 = "4"; 
	obj4 = new JotsaAnimationObject(start_x + 90,start_y + 50,1,1,this); 
	obj4.set_draw_centered_oval_string(oval_width,oval_height,str4,Color.blue,Color.black); 
	str5 = "5"; 
	obj5 = new JotsaAnimationObject(start_x - 60,start_y + 180,1,1,this); 
	obj5.set_draw_centered_oval_string(oval_width,oval_height,str5,Color.blue,Color.black); 
	str6 = "6"; 
	obj6 = new JotsaAnimationObject(start_x + 60,start_y + 180,1,1,this); 
	obj6.set_draw_centered_oval_string(oval_width,oval_height,str6,Color.blue,Color.black); 
	line12 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line12.set_draw_line(obj1, -12, 13, obj2, 12, -12, Color.green); 
	line13 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line13.set_draw_line(obj1, 0, 14, obj3, 0, -13, Color.green); 
	line14 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line14.set_draw_line(obj1, 12, 13, obj4, -12, -12, Color.green); 
	line23 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line23.set_draw_line(obj2, 25, -1, obj3, -25, -5, Color.green); 
	line43 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line43.set_draw_line(obj4, -25, -1, obj3, 25, -5, Color.green); 
	line25 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line25.set_draw_line(obj2, 0, 14, obj5, -2, -13, Color.green); 
	line35 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line35.set_draw_line(obj3, -12, 13, obj5, 12, -13, Color.green); 
	line36 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line36.set_draw_line(obj3, 12, 13, obj6, -12, -13, Color.green); 
	line46 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line46.set_draw_line(obj4, 0, 14, obj6, 2, -13, Color.green); 
	line56 = new JotsaAnimationObject(start_x,start_y,1,1,this); 
	line56.set_draw_line(obj5, 25, 0, obj6, -25, 0, Color.green); 
	stobj12 = new JotsaAnimationObject(start_x - 45,start_y + 25,1,1,this); 
	stobj12.set_draw_string(str6,12,Color.black); 
	stobj13 = new JotsaAnimationObject(start_x ,start_y + 45,1,1,this); 
	stobj13.set_draw_string(str1,12,Color.black); 
	stobj14 = new JotsaAnimationObject(start_x + 45,start_y + 25,1,1,this); 
	stobj14.set_draw_string(str5,12,Color.black); 
	stobj23 = new JotsaAnimationObject(start_x - 45,start_y + 65,1,1,this); 
	stobj23.set_draw_string(str5,12,Color.black); 
	stobj43 = new JotsaAnimationObject(start_x + 40,start_y + 65,1,1,this); 
	stobj43.set_draw_string(str5,12,Color.black); 
	stobj25 = new JotsaAnimationObject(start_x - 85,start_y + 120,1,1,this); 
	stobj25.set_draw_string(str3,12,Color.black); 
	stobj35 = new JotsaAnimationObject(start_x - 40,start_y + 135,1,1,this); 
	stobj35.set_draw_string(str6,12,Color.black); 
	stobj36 = new JotsaAnimationObject(start_x + 40,start_y + 135,1,1,this); 
	stobj36.set_draw_string(str4,12,Color.black); 
	stobj46 = new JotsaAnimationObject(start_x + 85,start_y + 120,1,1,this); 
	stobj46.set_draw_string(str2,12,Color.black); 
	stobj56 = new JotsaAnimationObject(start_x,start_y + 190,1,1,this); 
	stobj56.set_draw_string(str6,12,Color.black); 
	JotsaInsertObject(introbj); 
	JotsaInsertObject(obj1); 
	JotsaInsertObject(obj2); 
	JotsaInsertObject(line12); 
	JotsaInsertObject(obj3); 
	JotsaInsertObject(line13); 
	JotsaInsertObject(obj4); 
	JotsaInsertObject(line14); 
	JotsaInsertObject(obj5); 
	JotsaInsertObject(line23); 
	JotsaInsertObject(line43); 
	JotsaInsertObject(line25); 
	JotsaInsertObject(line35); 
	JotsaInsertObject(obj6); 
	JotsaInsertObject(line36); 
	JotsaInsertObject(line46); 
	JotsaInsertObject(line56); 
	JotsaInsertObject(stobj12); 
	JotsaInsertObject(stobj14); 
	JotsaInsertObject(stobj13); 
	JotsaInsertObject(stobj23); 
	JotsaInsertObject(stobj43); 
	JotsaInsertObject(stobj25); 
	JotsaInsertObject(stobj35); 
	JotsaInsertObject(stobj36); 
	JotsaInsertObject(stobj46); 
	JotsaInsertObject(stobj56); 
	} 


	/*public void Construct_Tree(){ 
	System.out.println("ORIGINAL TREE DATA"); 
 	System.out.println("Node1"+"   "+"Node2"+"   "+"Weight");
	Kruskal.print_original_data(); 
	System.out.println("SORTED DATA"); 
 	System.out.println("Node1"+"   "+"Node2"+"   "+"Weight");
	Kruskal.print_sorted_data(); 
	}*/   



	/**
	*Returns a boolean action regarding a button selection or choice
	*item located at the bottom of the screen of the applet.
	*@return	The desired action 
	*
	*
	*/ 
	public boolean action(Event e, Object arg){ 
	if("Introduction".equals(arg)){ 
	   JotsaRemoveAllObjects();   
	   intro(); 
	   return true; 
	} 	
	else if("Original Tree".equals(arg)){ 
	   JotsaRemoveAllObjects(); 
	   if(Kruskal.getZero() == TRUE)
	     Ootree = new ZeroOriginal_Tree(this);  
	   else{ 
	   otree = new Original_Tree(this);  
	   } 
	   /*Construct_Tree();*/  
	   return true; 	
	} 
	else if("Minimized Tree".equals(arg)){ 
	   JotsaRemoveAllObjects(); 
	   if(Kruskal.getZero() == TRUE)
	     motree = new ZeroMinimized_Tree(this); 
	   else{ 
	   mtree = new Minimized_Tree(this);  
	   }  
	   return true; 	
	} 
	else if(e.target instanceof Choice){  
	
	   String choice = (String)arg; 
	   if(choice.equals("ABOUT THIS APPLET")){ 
	   String str; 
	   str = new String("Hello.  This applet reads in an integer input file. The applet\n"); 
	   str += "assumes that the first integer represents the number of nodes\n";        
	   str += "or vertices of a graph.  The second integer represents the\n"; 
	   str += "number of edges in the graph.  The remaining data should be\n";  
	   str += "organized as follows: Vertex Vertex WeightedEdge followed\n";                 
	   str += "by Vertex Vertex WeightedEdge.... e.t.c.\n";  
	   str += "The applet can construct a graph that has between 4 and 12\n";  
	   str += "vertices and from this graph construct a minimum spanning tree.\n";              
	   str += "Click on button Tree to see example graph constructed, then\n"; 
	   str += "click on button Minimized Tree to see resulting minimized\n"; 
	   str += "spanning tree.\n";  
	   str += "EXAMPLE FILE INPUT:\n"; 
	   str += "12\n";  
	   str += "30\n";  
	   str += "1 2 10\n";  
	   str += "3 8 6\n";  
	   str += "9 4 2\n";  
	   str += "0 3 2\n";  
	   popup_count++; 
	   GraphFrame f  = new GraphFrame("ABOUT THIS APPLET",str);     
	   clip[0] = getAudioClip(getDocumentBase(),"About_Applet.au"); 
	   clip[0].play(); 
	   return true; 
	
	   } 
	   if(choice.equals("GRAPH TRAVERSALS")){ 
	   String str1; 
	   str1 = new String("In a number of graph problems, we need to visit the vertices\n"); 
	   str1 += "of a graph systematically.  \n"; 
	   str1 += "Depth-first search and breadth-first search, are two\n"; 
	   str1 += "important techniques for doing this.  Both techniques can be used\n"; 
	   str1 += "to determine efficiently all vertices that are connected to a\n"; 
	   str1 += "given vertex.\n"; 
	   str1 += "Depth-First Search:\n"; 
	   str1 += "It should be noted that each tree in a forest is one connected\n"; 
	   str1 += "component of a graph, so if a graph is connected, it has only\n"; 
	   str1 += "one tree in it's depth-first spanning forest.  During a depth-first\n"; 
	   str1 += "traversal of a directed graph, certain undirected edges, when\n"; 
	   str1 += "traversed, lead to unvisited vertices, these back edges form a\n"; 
	   str1 += "depth-first spanning forest for the given undirected graph. For\n";   
	   str1 += "a directed graph the arcs can be grouped into tree, back, forward\n"; 
	   str1 += "and cross arcs.\n";   
	   str1 += "Breadth-First Search:\n"; 
	   str1 += "This approach systematically searches as broadly as possible  for\n"; 
	   str1 += "all vertices adjacent to v from v. As with Depth-First Search, a\n"; 
	   str1 += "a spanning tree can be built.  In this case an edge (x,y) a tree\n";
	   str1 += "edge is vertex y is first visited from vertex x in the inner\n"; 
	   str1 += "loop of the search procedure in the Breadth-First Search algorithm.\n";     
	   GraphFrame f  = new GraphFrame("GRAPH TRAVERSALS",str1);     
	   clip[1] = getAudioClip(getDocumentBase(),"Graph.au"); 
	   clip[1].play(); 
	   return true; 
	   }  
	   if(choice.equals("SOUND ON")){ 
		for (int c = 0; c<numclip; c++)
		{ 
			if(clip[c] != null)
			{ 
			  clip[c].play(); 
			}  
		}  

	   return true; 

	   } 
	   if(choice.equals("SOUND OFF")){ 
		for (int c = 0; c<numclip; c++)
		{ 
			if(clip[c] != null)
			{ 
			  clip[c].stop(); 
			}  
		}  
	   return true; 

	   }  
	   if(choice.equals("KRUSKAL THE MAN")){ 
		JotsaAnimationObject exobj;  
		JotsaAnimationObject exobj1; 
		JotsaAnimationObject exobj2; 
		exobj1 = new JotsaAnimationObject(10,50,1,1,this); 
		exobj2 = new JotsaAnimationObject(10,400,1,1,this); 
		exobj = new JotsaAnimationObject(150,150,1,1,this); 
		external_image = getImage(getDocumentBase(),"kruskal.gif");  
		String exstr ="THIS COULD BE KRUSKAL.......";   
		String exstr1 = "NOT!!!  HIS PICTURE WAS NOT AVAILABLE.";   
		exobj1.set_draw_strings(exstr,"Courier",Font.BOLD,30,Color.black,Color.black,1,1); 	
		exobj2.set_draw_strings(exstr1,"Courier",Font.BOLD,30,Color.black,Color.black,1,1); 	
		exobj.set_image(external_image);   
		JotsaRemoveAllObjects(); 
		JotsaInsertObject(exobj); 
		JotsaInsertObject(exobj1); 
		JotsaInsertObject(exobj2); 
	return true; 
	   }  
	}  
	
	return super.action(e,arg); 
	}  

}     
 
