import java.awt.*;
import java.applet.*;
import java.net.URL;
import graph.*;
import java.awt.event.*;
import java.util.*;

/*************************************************************************
**
**    Applet example1a
**                                              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 bobble2t extends Applet implements AdjustmentListener{

      Graph2D graph1;
      Graph2D graph2;
Image osi=null;
	Graphics osg=null;
	int iwidth = 0;
	int iheight=0;
      DataSet data1;
      DataSet data2;
DataSet data3;
DataSet data4;
      Axis    xaxis1;
      Axis    xaxis2;
      Axis    yaxis1;
      Axis    yaxis2;
      double data[];
      int np = 30;
      URL markersURL;
      Markers markers;
      Panel      panel;
      Label title;
Scrollbar slope; TextField slopetext;
 int i;
        int j; int lx; int ly, ax, jx;
	 double m1,m3;
       

      public void init() {
        double data[] = new double[2*np];
/*
**      Get the passed parameters
*/
        slope = new Scrollbar(Scrollbar.HORIZONTAL,70,1,60,120);
		add("South",slope); slope.reshape(60,380,150,10);
		slope.addAdjustmentListener(this);
Label slopelabel=new Label("slope,a"); 
		add("South",slopelabel); slopelabel.reshape(10,375,40,20);
slopetext = new TextField(" ", 20);
		add("South", slopetext);
		slopetext.reshape(300,380,50,25);
String st = getParameter("TITLE");
        String mfile    = getParameter("MARKERS");
/*
**      Create the Graph instance and modify the default behaviour
*/
        graph1 = new Graph2D();
        graph1.drawzero = false;
        graph1.drawgrid = false;
        graph1.borderRight = 0;
        graph1.setDataBackground(new Color(255,230,200));


        graph2 = new Graph2D();
        graph2.drawzero = false;
        graph2.drawgrid = false;
        graph2.borderLeft =  0;
        graph2.setDataBackground(new Color(230,230,230));
/*
**      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; data[1]=0; data[2]=3; data[3]=30;

        data1 = graph1.loadDataSet(data,np);
        data1.linecolor   =  Color.red;
        data1.linestyle = 0;
        data1.marker    = 0;
        data1.markerscale = 1.0;
     
       
/*
**      Calculate the Second data Set.
*/

        for(i=j=0; i<np; i++,j+=2) {
            data[j] = i*2.0/10 +6;
            data[j+1] =13.86*data[j]*data[j] - 253.36*data[j] + 1160.3;

        }

        data2 = graph2.loadDataSet(data, np);
        data2.linecolor   =  Color.magenta;
        data2.marker      = 0;
        data2.markercolor = new Color(100,100,255);
/*
**	third data set
*/

data[0]=0.6; data[1]=4.3;
data[2]=1.0; data[3]=9.1;
data[4]=1.5; data[5]=13;
data[6] =2.0; data[7]=19;
data[8]=2.5; data[9]=23;
 data3 = graph1.loadDataSet(data, 5);
	  data3.linestyle = 0;
        data3.linecolor   =  new Color(0,255,0);
        data3.marker      = 3;
        data3.markercolor = new Color(100,100,255);
       m3 =7.0;
  for(i=j=0; i<np; i++,j+=2) {
            data[j] = i*1.0/10 ;
            data[j+1] =m3*data[j];

        }

        data4 = graph1.loadDataSet(data, np);
        data4.linecolor   =  Color.black;
        data4.marker      = 0;
        data4.markercolor = new Color(100,100,255);

/*
**      Attach data sets to the Xaxes
*/
        xaxis1 = graph1.createAxis(Axis.BOTTOM);
        xaxis1.attachDataSet(data1);
	xaxis1.attachDataSet(data3);
xaxis1.attachDataSet(data4);
        xaxis1.setTitleText("Doubling time µ ");
xaxis1.setTitleColor( new Color(0,0,255) );
        xaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        xaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));

        xaxis2 = graph2.createAxis(Axis.BOTTOM);
        xaxis2.attachDataSet(data2);
        xaxis2.setTitleText("a=slope");
xaxis2.setTitleColor( new Color(0,0,255) );
        xaxis2.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        xaxis2.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
/*
**      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(" r nucleotides/min/cell  x10^5");
        yaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        yaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis1.setTitleColor( new Color(0,0,255) );
        yaxis1.setTitleRotation(90);
/*
**      Attach the second data set to the Right Axis
*/
        yaxis2 = graph2.createAxis(Axis.RIGHT);
        yaxis2.attachDataSet(data2);
        yaxis2.setTitleText("J(a)");
        yaxis2.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        yaxis2.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis2.setTitleColor(new Color(100,100,255) );
        yaxis2.setTitleRotation(0);
      }

public void adjustmentValueChanged(AdjustmentEvent ade)
	{
		ly = slope.getValue();
		slope.setValue(ly);
		m1= ly; m3=m1/10;
Graphics g;
		double other[]= new double[2*np];
	 for(i=j=0; i<np; i++,j+=2) {
           other[j] = i*1.0/10 ;
           other[j+1] =m3*other[j];  
        }
        
		data4.delete(0,np);
		try{    data4.append(other,np);}
		     
                   catch (Exception e) {
                        System.out.println("Error appending Data!");
                   }
        
       xaxis1.setManualRange(true);
		data4.xaxis.minimum=0.00; data4.xaxis.maximum=3.0;
		yaxis1.setManualRange(true);
		        data4.yaxis.minimum=0.0; data4.yaxis.maximum=30;  
		  
             slopetext.setText(" "+m3);
	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);
           graph1.update(osg);
     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);

          ax = xaxis2.getInteger(m3);
jx= yaxis2.getInteger(13.86*m3*m3 - 253.36*m3 + 1160.3);
          

osg.setColor(Color.red);

osg.fillOval(ax-5,jx-5,10,10);


        


                   g.drawImage(osi,0,0,graph2);
	}

public void update(Graphics screen){
	paint(screen);
	}
}

