import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;
import java.util.*;
import graph.*;
/*************************************************************************
**
**    Applet linear1a
**                                              Version 1.0   January 1996
**
**************************************************************************
**    Copyright (C) 1996 Leigh Brookshaw
**
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation; either version 2 of the License, or
**    (at your option) any later version.
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**************************************************************************
**
**    This is a simple applet that demonstrates how to use the basic features
**    of the Plotting Class library. The data is calculated locally by
**    the applet
**
*************************************************************************/

public class logaids5e extends Applet implements AdjustmentListener{
	LoadData dynamic;
      	Graph2D graph1; 
	Graph2D graph2;
	Image osi=null;

	Graphics osg=null;
	int iwidth = 0;
	int iheight=0; int yvalue=0;
     	DataSet data1;
	DataSet data2;
	DataSet data3;
	DataSet data4;
	DataSet data5;
      	Axis    xaxis1;
	Axis xaxis2;
      	Axis    yaxis1;
	Axis yaxis2;
      	double data[];double errorsum;
	double spots[];
      	int np = 12;
int p=12;
      	URL markersURL;
      	Markers markers;
      	Panel      panel;
     	 Label title;
	
	Scrollbar slope, intercept;
	TextField equation, errortext;
	int i,j; double m1,m2,m3,m4; 
	int lx =100; int ly = 100; double az=200; double bz=85;
	int mx=100; int my=100;
	
      public void init() {
        

        double data[] = new double[2*(np+3)];
	        setLayout( new BorderLayout() );		
		
		
		equation=new TextField("best fit: ", 20);
		add("South", equation);
		equation.reshape(20,25,300,30);
		errortext =new TextField("sum of squares: ",40);
		add("South", errortext);
		errortext.reshape (35, 60,200,30);
		intercept = new Scrollbar(Scrollbar.HORIZONTAL, 250,1,100,500);
		add("South", intercept);intercept.reshape(395,380,200,10);
		intercept.addAdjustmentListener(this);
		slope = new Scrollbar(Scrollbar.HORIZONTAL, 200,1,100,500);
		add("South",slope); slope.reshape(50,380,150,10);
		slope.addAdjustmentListener(this);
		Label interceptlabel=new Label("intercept");

		add("South", interceptlabel); interceptlabel.reshape(330,380,50,20);

		Label slopelabel=new Label("slope"); 

		add("South",slopelabel); 

	
/*	
**      Get the passed parameters
*/
        String st = getParameter("TITLE");
        String mfile    = getParameter("MARKERS");
	String points =getParameter("POINTS");
/*
**      Create the Graph instance and modify the default behaviour
*/
        graph1 = new Graph2D();
	dynamic = new LoadData();
        graph1.drawzero = false;
        graph1.drawgrid = true;
        graph1.borderRight = 0;
        graph1.setDataBackground(new Color(248,110,178));
	
 graph2 = new Graph2D();
        graph2.drawzero = false;
        graph2.drawgrid =true;
        graph2.borderLeft =  0;
        graph2.setDataBackground(new Color(131,175,236));
        
/*
**      Build the title
*/
        title = new Label(st, Label.CENTER);
        title.setFont(new Font("TimesRoman",Font.PLAIN,20));



/*
**      Load a file containing Marker definitions
*/
        try {
           markersURL = new URL(getDocumentBase(),mfile);
           markers = new Markers(markersURL);
	 } 
	   catch(Exception e) 
	{
           System.out.println("Failed to create Marker URL!");
        }
        
 
        graph1.setMarkers(markers);
        graph2.setMarkers(markers);
         panel = new Panel();
        panel.setLayout( new GridLayout(0,2,10,0) );
        panel.add(graph1);
        panel.add(graph2);

        this.setLayout( new BorderLayout() );
        this.add("North", title);
        this.add("Center", panel);

/*

**      Calculate the first data Set.

*/

            data[0]=0.0; data[1]=2.8; data[2]=0; data[3]=15;

        data1 = graph1.loadDataSet(data,np);
	data1.linecolor   =  Color.red;
        data1.linestyle = 0;
        data1.marker    = 0;
        data1.markerscale = 1.0;


/*
**      Start a new thread and load the data
*/
        try {
        data3 = dynamic.loadDataSet(new URL(getDocumentBase(),points), graph1);
        } catch (Exception e) {
          System.out.println("Failed to load data file!");
        }
/*
**      Specify the data line color
*/
	
	data3.linecolor   =  new Color(0,255,0);
	data3.linestyle=0;
        data3.marker      = 2;
	data3.markerscale = 3;
        data3.markercolor = new Color(0,0,0);
      
   


/*
**	data set 4
*/
	 m1=200;m2=250; m3=m1/100; m4=m2/100;
	 for(i=j=0; i<np+2; i++,j+=2) {
            data[j] =((double) i)/10;
            data[j+1] = (m3*data[j] + m4);
        }
        data4 = graph1.loadDataSet(data,np+2);
          
        data4.linecolor   =  Color.blue;
        data4.linestyle = 1;
        data4.marker    = 0;
        data4.markerscale = 1.0;
        data4.markercolor = new Color(0,0,255);
        
     
       		 for(i=j=0; i<np+3; i++,j+=2) 
			{
           			data[j] =1+(double) i;
           		 	data[j+1] =Math.exp(m4)*(Math.pow(data[j], m3));
        			}

        		data2 = graph2.loadDataSet(data,np+3);
        		data2.linecolor   =  Color.magenta;
        		data2.linestyle = 1;
        		data2.marker    = 0;
        		data2.markerscale = 1.0;
        		data2.markercolor = new Color(0,0,255);

data[0]=0; data[1]=0;

data[2] =1;data[3]=97;
data[4]=2; data[5]=709;
data[6]=3; data[7]=2698;
data[8]=4; data[9]=6928;
data[10]=5; data[11]=15242;
data[12]=6; data[13]=29944;
data[14]=7; data[15]=52902;
data[16]=8;data[17]=83903;
data[18]=9; data[19]=120612;
data[20]=10; data[21]=161711;
data[22]=11; data[23]=206247;
data[24]=12; data[25]=257085;
data5 = graph2.loadDataSet(data,np+1);
        		data5.linecolor   =  Color.magenta;
        		data5.linestyle = 0;
        		data5.marker    = 2;
        		data5.markerscale = 1.0;
        		data5.markercolor = new Color(0,0,255);

/*	
**      Attach data sets to the Xaxes
*/
        xaxis1 = graph1.createAxis(Axis.BOTTOM);
  	xaxis1.attachDataSet(data1); 
	xaxis1.attachDataSet(data3);
	xaxis1.attachDataSet(data4);
        xaxis1.setTitleText("log(years since 1980)");
        xaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
        xaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
	xaxis1.setTitleColor( new Color(0,0,255) );
         xaxis2 = graph2.createAxis(Axis.BOTTOM);
  	xaxis2.attachDataSet(data2); 
	xaxis2.attachDataSet(data5); 
        xaxis2.setTitleText("years since 1980");
        xaxis2.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
        xaxis2.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
	xaxis2.setTitleColor( new Color(0,0,255) );
/*
**      Attach the first data set to the Left Axis
*/
        yaxis1 = graph1.createAxis(Axis.LEFT);
        yaxis1.attachDataSet(data1);
	yaxis1.attachDataSet(data3);
		yaxis1.attachDataSet(data4);
        yaxis1.setTitleText("log ( aids cases)");
        yaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
        yaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis1.setTitleColor( new Color(0,0,255) );
        yaxis1.setTitleRotation(90);
	 yaxis2 = graph2.createAxis(Axis.RIGHT);
        yaxis2.attachDataSet(data2);
	 yaxis2.attachDataSet(data5);
		
        yaxis2.setTitleText(" aids cases");
        yaxis2.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
        yaxis2.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis2.setTitleColor( new Color(0,0,255) );
        yaxis2.setTitleRotation(90);
	
	}

	
public void adjustmentValueChanged(AdjustmentEvent ade)
	{ lx = intercept.getValue();
		intercept.setValue(lx);
		ly = slope.getValue();
		slope.setValue(ly);
		m1=(double) ly; m2=(double) lx ; m3=m1/100; m4=m2/100;
	
double other[]= new double[2*(np+3)];
	 for(i=j=0; i<np+2; i++,j+=2) {
            other[j] = (double) i;
            other[j+1] = (m3*other[j] + m4);
        }
        
		data4.delete(0,np+2);
		try{    data4.append(other,np+2);}
		     
                   catch (Exception e) {
                        System.out.println("Error appending Data!");
                   }
         for(i=j=0; i<np+3; i++,j+=2) 
			{
           			other[j]=1.0 + (double) i;
           		 	other[j+1] =Math.exp(m4)*Math.pow((other[ j]), m3);
        			}

        		data2.delete(0,np+3);
		try{    data2.append(other,np+3);}
		     
                   catch (Exception e) {
                        System.out.println("Error appending Data!");
                   }
       xaxis1.setManualRange(true);
		data4.xaxis.minimum=0.0; data4.xaxis.maximum=2.8;
		yaxis1.setManualRange(true);
		        data4.yaxis.minimum=0.0; data4.yaxis.maximum=14;  
 xaxis2.setManualRange(true);
		data2.xaxis.minimum=0.0; data2.xaxis.maximum=15;
		yaxis2.setManualRange(true);
		        data2.yaxis.minimum=0.0; data2.yaxis.maximum=750000;  
		   equation.setText("lny= rlnx +lnA= "+m3+"lnx+ "+m4);
               equation.setFont(new Font("TimesRoman",Font.PLAIN,24));    
    
Graphics g;			
g = graph1.getGraphics();
                   if( osi == null || iwidth != graph1.size().width
                                   || iheight != graph1.size().height  ) {
                       iwidth = graph1.size().width;
                       iheight = graph1.size().height;
                       osi = graph1.createImage(iwidth,iheight);
                       osg = osi.getGraphics();
	                   }
                   osg.setColor(this.getBackground());
                   osg.fillRect(0,0,iwidth,iheight);
                   osg.setColor(g.getColor());
                   osg.clipRect(0,0,iwidth,iheight);

	osg.setColor(Color.black);
osg.fillRect(0,0, iwidth,iheight);
                   graph1.update(osg);
osg.setColor(Color.black);
double spots[] = new double[2];
int places[] =new int[2*p]; errorsum=0.0;
for(i=j=0; i<p; i++,j+=2) {
spots=data3.getPoint(i);
           places[j] = xaxis1.getInteger(spots[0]);
           places[j+1] = yaxis1.getInteger(spots[1]);
yvalue = yaxis1.getInteger(m3*spots[0]+m4);
errorsum=errorsum+(m3*spots[0]+m4-spots[1])*(m3*spots[0]+m4-spots[1]);

osg.drawLine(places[j],places[j+1],places[j],yvalue);
       }
 g.drawImage(osi,0,0,graph1);
g = graph2.getGraphics();
                   if( osi == null || iwidth != graph2.size().width
                                   || iheight != graph2.size().height  ) {
                       iwidth = graph2.size().width;
                       iheight = graph2.size().height;
                       osi = graph2.createImage(iwidth,iheight);
                       osg = osi.getGraphics();
                   }
                   osg.setColor(this.getBackground());
                   osg.fillRect(0,0,iwidth,iheight);
                   osg.setColor(g.getColor());
                   osg.clipRect(0,0,iwidth,iheight);
           graph2.update(osg);

errortext.setText("sum of sq.=  " + ((float)((int)(errorsum*1000)))/1000); 
errortext.setFont(new Font("TimesRoman",Font.PLAIN,24));
   g.drawImage(osi,0,0,graph2);
	}
                  
			
	
public void destroy(){
	osg.dispose();
				}
public void update(Graphics screen){
	paint(screen);
	}
		
	
 

}

        
		




