/* <applet code = annas.class height = 300 width = 300></applet> */

	import java.applet.*;
	import java.awt.*;
	import java.awt.event.*;
	import java.util.*;
	import javax.swing.*;


	public class annas extends JApplet implements
ActionListener,Runnable
	{
		JButton Start    = new JButton("START");
		JButton Stop     = new JButton("STOP");
		Thread arrival;
		int x1=700,y1=43,x2=700,y2=167,x=645,y=97; 
		int atime,stime,stop=0,total_arrivals=0,total_served=0;
  		float next_arrival_time = 0,time_left = 0,min,hai,avg_q_len=0;;  
                float[] threshold=new float[3];
		int[] busy=new int[3];
		float LAMBDA,MU,clock=0;
		Vector queue = new Vector();

	      	class panel extends JPanel
		{
			panel(annas d)
			{
				setLayout(new FlowLayout());
				add(Start);
				add(Stop);
				Start.addActionListener(d);
				Stop.addActionListener(d);
			}
		}	

		public void init()
		{
			
			panel p = new panel(this);
			panel2 p2 = new panel2();
			Container c = getContentPane();
			c.setLayout(new BorderLayout());
			c.add(p2,"Center");
			c.add(p,"North");
			Start.addActionListener((ActionListener)this);
			Stop.addActionListener((ActionListener)this);
			LAMBDA = (new Float (this.getParameter("LAMBDA") )).floatValue(); 
			MU     = (new Float (this.getParameter("MU") )).floatValue(); 
		}

		class panel2 extends JPanel
		{
	     		public void paintComponent( Graphics G )
			{
				G.drawLine(100,90,650,90);
				G.drawLine(100,110,650,110);
				G.drawRect(675,10,60,30);
				G.drawRect(675,175,60,30);
				G.drawString("Counter1",678,25);
				G.drawString("Counter2",678,190);
				int tempx = x;
				for( int i = 0;i < queue.size(); i++ )
				{
					G.fillRect(tempx,y,5,5);
					tempx = tempx - 8;
				}
				
				if(  busy[1] == 1 )
					G.fillRect(x1,y1,5,5);
				
				if(   busy[2] == 1)
			    		G.fillRect(x2,y2,5,5);
				G.drawString("Total arrivals:" + total_arrivals,300,350);
				G.drawString("Total served  :" + total_served,300,400);
				G.drawString("Average q length  :" + avg_q_len,300,450);
			}
		}

	
	public void actionPerformed( ActionEvent e ) 
			{ 
			JButton b = ( JButton ) e.getSource();
			
			if(  b.getActionCommand() == "START" )
			   {
				arrival = new Thread(this,"Arrival"); 			
				showStatus("Start pressed");
				System.out.println("Lambda"+LAMBDA);
				System.out.println("Mu"+MU);
				while(queue.isEmpty() == false )
				queue.removeElementAt(0);
				System.out.println("Start pressed");
				stop = 0;
				clock= 0;
				total_served   = 0;
				total_arrivals = 0;
				threshold[1] = -1;
				threshold[2] = -1;
				 arrival.start();
			}
			else
			    { 
				stop = 1;
				repaint();
				showStatus("Finished");
			     }
		}


	public void run()
			{

 				hai = (float)Math.random();
        			time_left = (float)Math.abs((-1/LAMBDA)*Math.log(1-hai));
 	       		       while( stop == 0) 
 				{
        			min = time_left;
        			for(int i = 1;i < 3;i++)
                			if(threshold[i] < min && threshold[i] > 0)
                        			min = threshold[i];
        			time_left = time_left - min;
        			clock = clock + min;
				for(int i = 1; i < 3; i++)
              				threshold[i] = threshold[i] - min;

        			if(time_left == 0)
                		{
			 		queue.addElement("1"); 
			 		hai = (float)Math.random();
                	 		time_left=(float)Math.abs((-1/LAMBDA)*Math.log(1-hai));
                      	 		total_arrivals++; 
					avg_queue_length();
					if(stop == 0)
						repaint();
					try{ Thread.sleep(1000); }
					catch( Exception e1 ) { System.out.println(e1); } 
                		}

        			for(int i = 1;i < 3;i++) 
                			if( threshold[i] <= 0)  
					{
                  			busy[i] = 0;
					if( threshold[i] == 0)
                  				total_served += 1;
		
			               }
		
        			for(int i = 1; i < 3; i++)
                        		if(busy[i] == 0  &&  queue.isEmpty() == false )
					{
				  	queue.removeElementAt(0);
                                  	busy[i] = 1;
					avg_queue_length();
				  	if(stop == 0 )
						repaint();
					try{ Thread.sleep(1000); }
					catch( Exception e1 ) { System.out.println(e1); } 
 				  	hai = (float)Math.random();
                                  	threshold[i] =(float)Math.abs((-1/MU)*Math.log(1-hai));
					}
 	 			} /* while */

			}
	
		void avg_queue_length()
			{
			 if(clock > 1) 
				 avg_q_len = ((avg_q_len*(clock-1))+queue.size())/clock; 
			 else
				 avg_q_len = queue.size();
			}

}/* end of beginning */

