import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.MediaTracker;
import java.awt.Panel;
import java.awt.Scrollbar;
import java.awt.TextField;
import java.awt.Toolkit;
import java.net.URL;




class ScrollbarLiar extends Scrollbar {
	// Just an event filtering fakery.
	
	public ScrollbarLiar(int a, int b, int c,int d, int e){
			super(a,b,c,d,e);
	}
	
	public boolean handleEvent(Event e){
 		boolean ig=super.handleEvent(e);
		return(false);
	}
}

class DLCanvas extends Canvas{
	Image i;  
	int iwidth;
	int iheight;
	
	public DLCanvas(Image ImageName){
		i=  ImageName;
	}
	
	public Dimension preferredSize(){
		iwidth = i.getWidth(this);
		iheight = i.getHeight(this);
		Dimension d=new Dimension(iwidth,iheight);
		 
		return(d);
	}
	
	public void paint(Graphics g){
		iwidth = i.getWidth(this);
		iheight = i.getHeight(this);
		Dimension d=this.getSize();
		g.drawImage(i,(d.width-iwidth)/2, (d.height-iheight)/2,this);
	}
		
}	

class ColorBox extends Canvas {
	Color ColorMe;
	public ColorBox(Color c){
		ColorMe=c;
	}
	
	public void paint(Graphics g){
		g.setColor(ColorMe);
		Dimension d=this.size();
		g.fillRect(0,0,d.width,d.height);
	}
}
	
		

class DLScroll extends Panel{
	Label right;
	ScrollbarLiar s;
	DLCanvas i;
	Panel sub;
	
	public DLScroll(int start, int min, int max, Image ImageName){
		i=new DLCanvas(ImageName);
		sub=new Panel();
		
		s=new ScrollbarLiar(Scrollbar.HORIZONTAL,start,0,min,max);
		right=new Label(""+start);
		this.setLayout(new BorderLayout());
		this.add("Center",i);
		sub.setLayout(new FlowLayout());
		sub.add(s);
		sub.add(right);
		this.add("South",sub);
	}
	 
	public void resetLabel(){
		right.setText(""+s.getValue());
	}
	
	public int getValue(){
		return(s.getValue());
	}
	
	public boolean handleEvent(Event e){
 		boolean ig=super.handleEvent(e);
 		right.setText(""+s.getValue());
		return(true);
	}
}

class ScrollLabel extends Panel{
	Label right;
	ScrollbarLiar s;
	Dimension sz;
	 	
	public ScrollLabel(int start, int min, int max){
	 
		s=new ScrollbarLiar(Scrollbar.HORIZONTAL,start,0,min,max);
		right=new Label(""+start);
		 
		
		
		this.add(s);
		this.add(right); 
		sz=this.getSize();
		s.setLocation(0,sz.height);
		right.setLocation(sz.width-40,0);
		s.setSize(sz.width-40,sz.height);
		right.setSize(40,sz.height);
		Font labelFont = new Font("Helvetica", Font.BOLD, 10);
		right.setFont(labelFont);
	}
	
	/*public void layout(){
		sz=this.getSize();
		s.setLocation(0,sz.height);
		right.setLocation(sz.width-40,0);
		s.setSize(sz.width-40,sz.height);
		right.setSize(40,sz.height);
		
	}
	*/
	 
	public void resetLabel(){
		right.setText(""+s.getValue());
	}
	
	public int getValue(){
		return(s.getValue());
	}
	
	public boolean handleEvent(Event e){
 		boolean ig=super.handleEvent(e);
 		right.setText(""+s.getValue());
		return(true);
	}
}

class DLLabel extends Panel {
	Label right;
	DLCanvas i;
	
	public DLLabel(int start, Image ImageName){
		i=new DLCanvas(ImageName);
		right=new Label(""+start);
		this.setLayout(new BorderLayout( ));
		this.add("Center",i);
		this.add("South",right);
		Font labelFont = new Font("Helvetica", Font.ITALIC, 11);
		right.setFont(labelFont);
	}
	
	public void changeData(int s){
		right.setText(""+s);
	}
	
}

class CenteredTextField extends Panel {
	TextField TF;
	
	public CenteredTextField(int size){
			TF=new TextField(size);
			add(TF);
		}
	public void setText(String s){
		TF.setText(s);
	}
	
	public String getText(){
		return(TF.getText());
	}
}

class DLInput extends Panel {
	CenteredTextField right;
	DLCanvas i;
	
	public DLInput(  Image ImageName){
		i=new DLCanvas(ImageName);
		right= new CenteredTextField(5);
		this.setLayout(new BorderLayout( ));
		this.add("South",right);
		this.add("Center",i);
	
	}
	
	public void changeData(int s){
		right.setText(""+s);
	}
	
	public void setText(String s){
		right.setText(s);
	}
	
	public String getText(){
		return(right.getText());
	}
	
}

		


class AIDSSim extends Thread {
	// This class will produce a thread that will scan through
	// a loop and keep updating things. 
	
	Pair[] TheCouples;
	int HowManyCouples;
	AIDS4 PrintOut;
	int HowManyDays=200;
	boolean Relentless=false;
	
	public AIDSSim(int Daze, int Couples, int PromiscuousMen, int PromiscuousWomen, AIDS4 ReportMe, boolean r){
		super(); 
		HowManyDays=Daze;
		HowManyCouples = Couples;
		TheCouples = new Pair[Couples];
		int TotalAllocated = 0;
		int Candidate;
		PrintOut= ReportMe; 
		Relentless = r;
		
		for (int i=0; i<HowManyCouples ; i++){
				TheCouples[i]=new Pair();}
		
		if (PromiscuousMen>Couples-PromiscuousMen) {
		// There are more PromiscuousMen then non-P Men, so
		// work backwards.
			for (int i=0; i<HowManyCouples ; i++){
				TheCouples[i].MalePromiscuous = true;}
			TotalAllocated=0;
			while (TotalAllocated < Couples-PromiscuousMen) {
				Candidate=(int)(Math.random()*HowManyCouples);
				if (TheCouples[Candidate].MalePromiscuous){
					TheCouples[Candidate].MalePromiscuous = false;
					TotalAllocated++;
				}
			}
		} else {
			// Men are initialized as non-promiscuous.
			TotalAllocated=0;
			while (TotalAllocated < PromiscuousMen) {
				Candidate=(int)(Math.random()*HowManyCouples);
				if (!TheCouples[Candidate].MalePromiscuous){
					TheCouples[Candidate].MalePromiscuous = true;
					TotalAllocated++;
				}
			}
		}
		
			int TotalInfected=0;
			// For now, hard code the infected population.
			int k=0;
			while ((k<HowManyCouples) & (TotalInfected<5)) {
				if (TheCouples[k].MalePromiscuous) {
					TheCouples[k].MaleInfected= true;
					TotalInfected++;
					TheCouples[k].MaleDeathDate=Pair.InfectedLifespan;
				}
				k++;
			}
	
		if (PromiscuousWomen>Couples-PromiscuousWomen) {
		// There are more PromiscuousWomen then non-P Men, so
		// work backwards.
			for (int i=0; i<HowManyCouples ; i++){
				TheCouples[i].FemalePromiscuous = true;}
			TotalAllocated=0;
			while (TotalAllocated < Couples-PromiscuousWomen) {
				Candidate=(int)(Math.random()*HowManyCouples);
				if (TheCouples[Candidate].FemalePromiscuous){
					TheCouples[Candidate].FemalePromiscuous = false;
					TotalAllocated++;
				}
			}
		} else {
			// Women are initialized as non-promiscuous.
			TotalAllocated=0;
			while (TotalAllocated < PromiscuousWomen) {
	 			Candidate=(int)(Math.random()*HowManyCouples);
	 			if (!TheCouples[Candidate].FemalePromiscuous){
					TheCouples[Candidate].FemalePromiscuous = true;
					TotalAllocated++;
				}
			}
		}
		 
	}
	
	
	 final void Report(int Day){
		int MI=0;
		int FI=0;
		int MPI=0;
		int FPI=0;
		int MD=0;
		int FD=0;
		
		for (int i=0; i<HowManyCouples; i++){
			if (TheCouples[i].MaleInfected) {
				MI++; 
				if (TheCouples[i].MalePromiscuous) {MPI++;}
			}
			if (TheCouples[i].FemaleInfected) {
				FI++; 
				if (TheCouples[i].FemalePromiscuous) {FPI++;}
			}
			if (!TheCouples[i].MaleAlive){
				MD++;
			}
			if (!TheCouples[i].FemaleAlive){
				FD++;
			}
		}
		PrintOut.addData( Day,MI,FI,MPI,FPI,MD,FD,HowManyCouples);
	}
	
	public final void run(){ 
		int Other;
		
		for (int Day=0; Day<=HowManyDays;Day++){
	 		for (int i=0; i<HowManyCouples; i++){
				TheCouples[i].InterCoupleFluidExchange(Day);
			}
			for (int i=0; i<HowManyCouples;i++){
				if (Relentless) {
					Other=(int)(Math.random()*HowManyCouples);
					while (!TheCouples[Other].FemalePromiscuous) {
						Other=(int)(Math.random()*HowManyCouples);
					}
					TheCouples[i].MaleIntramuralFluidExchange(TheCouples[Other],Day);
				} else {
					for (int j=0; j<10;j++){
						Other=(int)(Math.random()*HowManyCouples);
 						TheCouples[i].MaleIntramuralFluidExchange(TheCouples[Other],Day);
 					}
 				}
 			  
			}
			for (int i=0; i<HowManyCouples; i++){
				TheCouples[i].TestDeath(Day);
			}
			Report(Day);
			try{
 					Thread.sleep(250);
 			} catch (InterruptedException e){
 			}
		}
	}		
}




public class AIDS4 extends Applet implements Runnable  {
	TextField InCouples;
	TextField InLifespan;
	TextField InDays;
	TextField MaleToFemale, FemaleToMale;
	Image Strip1,Strip2, Strip3, Strip4,Strip5;
	
	ScrollLabel PM, PW, MPF, FPF;
	Button StartSimulation,StopSimulation;
	Button Relayout, CheckImages;
	MediaTracker TrackMe;
	
	
		
	AIDSSim RunMe=null;
	
	Checkbox Relentless, Limited;
			// 
			
	HistoryGraph MenInf,FemInf;
	Label Day,MI,FI,MPI,FPI,MD,FD;
	Label LastData,SecLastData;
		// Used for the Key.
	String NextDataLabel="";
	String SecNextDataLabel="";
		String ThirdNextDataLabel="";
	ColorBox ThisColorBox,LastColorBox,SecLastColorBox;
	

	Image   ImDay,ImMI,ImFI,ImMPI,ImFPI,ImMD,ImFD;
	Panel GraphPanel, LabelPanel;
	
	TextWindow DefinitionWindow;
	
	static final int Strip1X=5;
	final int Strip1Y=5;
	final int Strip1Height=145;
	final int Strip1Width=400;
	 int Strip2X=5;
	int Strip2Y=Strip1Y+Strip1Height+29;
	int Strip2Height=65;
	int Strip2Width=400;
	int Strip3X=5;
	int Strip3Y=Strip2Y+Strip2Height+65;
	int Strip3Height=75;
	int Strip3Width=400;
	
	int Strip4X=110;
	int Strip4Y=Strip3Y+Strip3Height+20;
	
	int Strip5X=0;
	int Strip5Y=Strip4Y;
	
	int TextFieldLength=40;
	int TextFieldHeight=24;
	int TextLabelLength=30;
	int TextLabelHeight=20;
	
	int TextFieldMargin=65;
	int ScrollFieldLength=105; // was 105
	int ScrollFieldHeight=32; // was 26
	int ScrollFieldMargin=20;
	boolean NeedsCleanup=true;
	
	public Image myGetImage(String s){
		//URL u=this.getClass().getResource(s);
		URL u=Thread.currentThread().getContextClassLoader().getResource(s);
		
		System.out.println("URL="+u);
	 	return  Toolkit.getDefaultToolkit().getImage(u);
	}
	 
	public  void init(){
		int Daze;
		System.out.println("Starting layout in AIDS4");
		
		TrackMe = new MediaTracker(this);
		
		this.setBackground(Color.white);
		
		/*		
	 	Strip1 = getImage(getCodeBase(),"Strip1.GIF");
	 	Strip2 = getImage(getCodeBase(),"Strip2.GIF");
	 	Strip3 = getImage(getCodeBase(),"Strip3.GIF");
		Strip4 = getImage(getCodeBase(),"Strip4.GIF");
		Strip5 = getImage(getCodeBase(),"Strip5.GIF");
*/
		Strip1 = myGetImage("Strip1.GIF");
	 	Strip2 = myGetImage("Strip2.GIF");
	 	Strip3 = myGetImage("Strip3.GIF");
		Strip4 = myGetImage("Strip4.GIF");
		Strip5 = myGetImage("Strip5.GIF");
		
		 
		
		
		TrackMe.addImage(Strip1,0);
		TrackMe.addImage(Strip2,0);
		TrackMe.addImage(Strip3,0);
		TrackMe.addImage(Strip4,0);
		TrackMe.addImage(Strip5,0);

		
	 	InDays = new TextField(5);
	 	InDays.setText("200");
		this.add(InDays);
		
	  	InCouples = new TextField(5);
		InCouples.setText("400");
		this.add(InCouples);
		 
	 	MaleToFemale = new TextField(5);
		MaleToFemale.setText("20");
		this.add(MaleToFemale);
	
	 	 FemaleToMale = new TextField(5);
		FemaleToMale.setText("10");
		this.add(FemaleToMale);
		
	 	InLifespan = new TextField(5);
		InLifespan.setText("150");
		this.add(InLifespan);
		
		 
	 	
	 	PM= new ScrollLabel(100,0,100);
		this.add(PM);
	  	PW= new ScrollLabel(5,0,100);
		this.add(PW);
	 	MPF= new ScrollLabel(100,0,100);
		this.add(MPF);
	  	FPF= new ScrollLabel(100,0,100);
		this.add(FPF);
	 	
		 
	 	CheckboxGroup cg= new CheckboxGroup();
		
		Relentless=new Checkbox("Relentless Male", cg,false);
		Limited = new Checkbox("Man Tries 10 Times", cg,true);
		Font labelFont = new Font("Helvetica", Font.ITALIC, 11);
		Relentless.setFont(labelFont);
		Limited.setFont(labelFont);
		
		
		this.add(Limited);
		this.add(Relentless); 
		 
		
		StartSimulation = new Button("Start Simulation");
		StartSimulation.setFont(labelFont);
		this.add(StartSimulation);
		
 		
		StopSimulation = new Button("Stop Simulation");
		StopSimulation.setFont(labelFont);
		this.add(StopSimulation);
		 
		 
		Daze=Integer.parseInt(InDays.getText());
		 this.add(MenInf=new HistoryGraph(0,Daze,0,100,Daze+1));
		this.add(FemInf=new HistoryGraph(0,Daze,0,100,Daze+1));
		
		
		this.add(Day=new Label("0"));
		this.add(MI=new Label("0"));
		this.add(FI=new Label("0"));
		this.add(MPI=new Label("0"));
		this.add(FPI=new Label("0"));
		this.add(MD=new Label("0"));
		this.add(FD=new Label("0"));
		
		this.add(LastData=new Label(""));
		this.add(SecLastData=new Label(""));
		
		this.add(ThisColorBox=new ColorBox(new Color(241,96,67)));
		this.add(LastColorBox=new ColorBox(new Color(241,166,112)));
		this.add(SecLastColorBox=new ColorBox(new Color(74,102,156))); 
		// Note, these colors must match those defined in HistoryGraph. I know they should
		// be centrally coded, but this is the price you pay for OOP.
		  
		DefinitionWindow=new TextWindow();
	  
	}
	
	 public  void layout() {
	
		
		if (InDays!=null) {
		
		this.resize(525,640);
   		if (InDays!=null)	
		InDays.setBounds(14,Strip1Y+Strip1Height+5,TextFieldLength,TextFieldHeight);
	 
   		if (InCouples!=null) InCouples.setBounds(14+TextFieldLength+TextFieldMargin,Strip1Y+Strip1Height+5,TextFieldLength,TextFieldHeight);
	 	 
   		if (MaleToFemale!=null) MaleToFemale.setBounds(14+2*TextFieldLength+2*TextFieldMargin,Strip1Y+Strip1Height+5,TextFieldLength,TextFieldHeight);
	
   		if (FemaleToMale!=null) FemaleToMale.setBounds(14+3*TextFieldLength+3*TextFieldMargin,Strip1Y+Strip1Height+5,TextFieldLength,TextFieldHeight);
	 
   		if (InLifespan!=null) InLifespan.setBounds(14+4*TextFieldLength+4*TextFieldMargin,Strip1Y+Strip1Height+5,TextFieldLength,TextFieldHeight);
	
   		if (PW!=null) PM.setBounds(10 ,Strip2Y+Strip2Height+5,ScrollFieldLength,ScrollFieldHeight);

   		if (PW!=null) PW.setBounds(10+ScrollFieldLength+ScrollFieldMargin,Strip2Y+Strip2Height+5,ScrollFieldLength,ScrollFieldHeight);

   		if (MPF!=null)  MPF.setBounds(10+2*ScrollFieldLength+2*ScrollFieldMargin,Strip2Y+Strip2Height+5,ScrollFieldLength,ScrollFieldHeight);

   		if (FPF!=null) FPF.setBounds(10+3*ScrollFieldLength+3*ScrollFieldMargin,Strip2Y+Strip2Height+5,ScrollFieldLength,ScrollFieldHeight);
	 	 
	  	 	
   		if (Relentless!=null) Relentless.setBounds(10,Strip2Y+Strip2Height+37,100,25);
   		if (Limited!=null) Limited.setBounds(120,Strip2Y+Strip2Height+37,120,25);
   		if (StartSimulation!=null) StartSimulation.setBounds(260,Strip2Y+Strip2Height+37,90,20);
   		if (MenInf!=null) StopSimulation.setBounds(360,Strip2Y+Strip2Height+37,90,20);
	  	
   		if (MenInf!=null) MenInf.setBounds(Strip4X+50,Strip4Y+50,140,100);  
   		if (FemInf!=null) FemInf.setBounds(Strip4X+250,Strip4Y+50,140,100);
	 
	 
   		if (Day!=null) Day.setBounds(35,Strip3Y+Strip3Height,TextLabelLength,TextLabelHeight);
   		if (MPI!=null) MPI.setBounds(135,Strip3Y+Strip3Height,TextLabelLength,TextLabelHeight);
   		if (FPI!=null) FPI.setBounds(355,Strip3Y+Strip3Height,TextLabelLength,TextLabelHeight);
   		if (MD!=null)  MD.setBounds(275,Strip3Y+Strip3Height,TextLabelLength,TextLabelHeight);
   		if (FD!=null)  FD.setBounds(465,Strip3Y+Strip3Height,TextLabelLength,TextLabelHeight);
   		if (MI!=null) MI.setBounds(Strip4X+110,Strip4Y+35,TextLabelLength,TextLabelHeight);
   		if (FI!=null) FI.setBounds(Strip4X+330,Strip4Y+35,TextLabelLength,TextLabelHeight);

   		if (LastData!=null) LastData.setBounds(Strip5X+15,Strip5Y+66,90,35);
   		if (SecLastData!=null) SecLastData.setBounds(Strip5X+15,Strip5Y+130,90,35);

   		if (ThisColorBox!=null) ThisColorBox.setBounds(Strip5X+2,Strip5Y+30,15,4);
   		if (LastColorBox!=null) LastColorBox.setBounds(Strip5X+2,Strip5Y+56,15,4);
   		if (SecLastColorBox!=null) SecLastColorBox.setBounds(Strip5X+2,Strip5Y+112,15,4);

	 }
	 }
	
 
	
	public void run(){
		
		
		while (true) {
		}
	}
	
	public void paint(Graphics g){
		if (TrackMe.isErrorAny()){
				g.drawString("Image Loading Error. Use Image Free Version.", Strip1X,Strip1Y);
				 Object errors[] = TrackMe.getErrorsAny();
       			g.drawString("Num Errors:"+errors.length, Strip1X,Strip1Y+20);
			
		} else if (TrackMe.checkAll(true)) {
			g.drawImage(Strip1,Strip1X,Strip1Y,this);
 			g.drawImage(Strip2,Strip2X,Strip2Y,this);
			g.drawImage(Strip3,Strip3X,Strip3Y,this);
			 g.drawImage(Strip4,Strip4X,Strip4Y,this);
			 g.drawImage(Strip5,Strip5X,Strip5Y,this);


		} else {
			repaint(200);
		}
	}
		
	public void update(Graphics g){
			g.drawImage(Strip1,Strip1X,Strip1Y,this);
			g.drawImage(Strip2,Strip2X,Strip2Y,this);
			g.drawImage(Strip3,Strip3X,Strip3Y,this);
			g.drawImage(Strip4,Strip4X,Strip4Y,this);
			g.drawImage(Strip5,Strip5X,Strip5Y,this);
	
		}
	
	public void stop(){
		if (RunMe!=null){
			RunMe.stop();
			NeedsCleanup=true;
		}
	}
	
	void LoadPairs(){
		// The pairs need to have their static variables set.
		Pair.MaleToFemaleRate=Integer.parseInt(MaleToFemale.getText())/1000.0;
		Pair.FemaleToMaleRate=Integer.parseInt(FemaleToMale.getText())/1000.0;
		Pair.MalePromiscuityFactor=MPF.getValue()/100.0;
		Pair.FemalePromiscuityFactor=FPF.getValue()/100.0;
		Pair.InfectedLifespan=Integer.parseInt(InLifespan.getText());
	}
	
	 
	public void addData(int rDay,int rMI,int rFI,int rMPI,int rFPI,int rMD,int rFD, int Couples){
		MI.setText(""+(int)(100*rMI/Couples));
		Day.setText(""+rDay);
		FI.setText(""+(int)(100*rFI/Couples));
		MPI.setText(""+rMPI);
		FPI.setText(""+rFPI);
		MD.setText(""+rMD);
		FD.setText(""+rFD);
		
		MenInf.addPoint(rDay,(int)(100*rMI/Couples));
		FemInf.addPoint(rDay,(int)(100*rFI/Couples));
		MenInf.repaint();
		FemInf.repaint();
	}
	
	public void Cleanup(){
		// Get rid of the last round of data.
		MenInf.Cleanup();
		FemInf.Cleanup();
		MI.setText(""+0);
		Day.setText(""+0);
		FI.setText(""+0);
		MPI.setText(""+0);
		FPI.setText(""+0);
		MD.setText(""+0);
		FD.setText(""+0);
	}
	
	 
	String wrapText(int width,String s){
		// This is a cheap wrapping algorithm. A better one would scan backwards.
		String answer="";
		int CurCount=0;
		for (int i=0; i<s.length(); i++){
			CurCount++;
			answer=answer+s.charAt(i);
			if ((s.charAt(i)==' ') & (CurCount>=width)) {
				answer=answer+'\n';
				CurCount=0; 
			}
		 }
		 return(answer);
	}
	
	public boolean mouseDown(Event e, int x, int y){
		// We need to filter out the mouse clicks for the window.
		double Pos;
		int boxWidth=25;
		String s="";
		if (y<Strip2Y) {
			Pos = (x-14)/(TextFieldLength+TextFieldMargin);
			if (Pos<1) {
				s=wrapText(boxWidth,"This is the number of time units the simulation runs. \n Each time unit a couple mates with each other once and then the promiscuous males look for other promiscuous females."); 
			} else if (Pos<2) {
				s=wrapText(boxWidth,"More couples mean a larger test population and more accurate results. The tradeoff is speed. Doubling the population cuts the execution speed in half.");
			} else if (Pos<3) {
				s=wrapText(boxWidth,"If 1000 males with the disease each mate once with a different uninfected female, then this is the number of new infected females that result. You can also think of it as the propability. See the detailed notes for more information about setting this value. This may or may not be different from the female to male transfer rate.");
			} else if (Pos<4) {
				s=wrapText(boxWidth,"If 1000 females with the disease each mate once with a different uninfected male, then this is the number of new infected males that result. You can also think of it as the propability. See the detailed notes for more information about setting this value. This may or may not be set differently than the male to female transfer rate.");

			} else {
				s=wrapText(boxWidth,"You can set the lifespan for infected people in time units. This is a hard boundary. If it is set to 100 units, then a person officially dies 100 units afterwards. Each person lives the same amount of time which is obviously a departure from reality.");
			}
			
			
		} else if (y<Strip3Y){
			Pos = (x-10)/(ScrollFieldLength+ScrollFieldMargin);
			if (Pos<1) {
				s=wrapText(boxWidth,"What percentage of men are promiscuous. That is, what percentage look for extra action each time unit after their initial liason with their wife. Non-promiscuous men never look outside the marriage.");
			} else if (Pos<2) {
				s=wrapText(boxWidth,"What percentage of women are promiscuous. That is, what percentage accept extra coupling each time unit after their initial liason with their husband. Non-promiscuous women never accept propositions from  outside the marriage.");
			} else if (Pos<3) {
				s=wrapText(boxWidth,"This is an extra factor that controls how likely a man is going to actually follow through after meeting a promiscous woman. Setting it to 50 is equivalent to having the man flip a coin.");
			} else if (Pos<4) {
				s=wrapText(boxWidth,"This is an extra factor that controls how likely a woman is going to actually accept a proposition from a promiscous man. Setting it to 50 is equivalent to having the woman flip a coin.");
			} else {
				
			}
		} else if (y<(Strip3Y-20)){
			
			s=wrapText(boxWidth,"A 'Relentless' man will always choose women randomly until he finds a promiscuous one. A 'Ten-timing' man will approach ten women and mate with all who happen to be promiscuous.");
		
		} else if (y<Strip3Y+48) {
				s=wrapText(boxWidth,"These display the statistics as the epidemic progresses. The graphs display the percentage of men and women that are infected while the numbers merely count the number of promiscuous men and women who are infected as well as those that die. The epidemic halts when all of the promiscuous men or women die.");
		} else {
				s=wrapText(boxWidth,"These display the statistics as the epidemic progresses. The graphs display the percentage of men and women that are infected while the numbers merely count the number of promiscuous men and women who are infected as well as those that die. The epidemic halts when all of the promiscuous men or women die.");
		
		
		
		}
	
		
		DefinitionWindow.show();
		DefinitionWindow.setText(s);
		return true;
	}
			
	public boolean action(Event e, Object a){
		int D, C, PromM, PromF;
		boolean r;
		if (e.target instanceof Button){
			if (e.target.equals(StartSimulation)){
				if (RunMe!=null){
					RunMe.stop();
					RunMe=null;
					NeedsCleanup=true;
				}
				if (RunMe==null){
					//System.out.println("Starting the run of the simulator.");
					if (NeedsCleanup) {
						Cleanup();
						NeedsCleanup=false;
					}
					LoadPairs();
					C=Integer.parseInt(InCouples.getText());
					D=Integer.parseInt(InDays.getText());
					MenInf.changeDays(D);
					FemInf.changeDays(D);
					MenInf.changeGraphs();
					FemInf.changeGraphs();
					PromM=C*PM.getValue()/100;
					PromF=C*PW.getValue()/100;
					r=Relentless.getState();
					ThirdNextDataLabel=SecNextDataLabel;
					SecNextDataLabel=NextDataLabel;
					NextDataLabel="MP:"+PM.getValue()+"%  FP:"+PW.getValue()+"% \n MPF:"+MPF.getValue()+" FPF:"+FPF.getValue();
					LastData.setText(SecNextDataLabel);
					SecLastData.setText(ThirdNextDataLabel);
					RunMe=new AIDSSim(D,C,PromM,PromF,this,r);
					RunMe.start(); 
					NeedsCleanup=true;
				}
			} else {
			if (RunMe!=null){
				RunMe.stop();
				RunMe=null;
				NeedsCleanup=true;
			}
		}}
		return(true);
	}
	
	
	
//	 Main entry point when running standalone
	public static void main(String[] args) 
	{
		AIDS4 applet = new AIDS4();
	   // applet.isStandalone = true;
		Frame frame = new Frame();
		frame.setTitle("Applet Frame");
		frame.add( applet, BorderLayout.CENTER );
		applet.init();
		applet.start();
		frame.setSize( 400, 320 );
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation( (d.width - frame.getSize().width) / 2,
			(d.height - frame.getSize().height) / 2);
		frame.setVisible( true );
	}
 
}



