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 dropdistance5f extends Applet implements  ActionListener{

      Graph2D graph1;
      Graph2D graph2;
Image osi=null;
	Graphics osg=null;
	int iwidth = 0;
	int iheight=0;
      DataSet data1;
      DataSet data2;
Button drop;

      Axis    xaxis1;
      Axis    xaxis2;
      Axis    yaxis1;
      Axis    yaxis2;
      double data[];

      URL markersURL;
      Markers markers;
      Panel      panel;
      Label title;
 TextField strobe;
 int i;
        int j; int lx; int ax, jx, np,nc, kx; 
	 double m1,m3,m4, ly;
       

      public void init() {
	np=2;
        double data[] = new double[2*np];
/*
**      Get the passed parameters
*/
      drop = new Button("drop");
		add("South",drop);drop.reshape( 550,380,50,20);
		drop.addActionListener(this);
Label slopelabel=new Label("strobe time, seconds"); 
		add("South",slopelabel); slopelabel.reshape(10,375,150,20);
strobe = new TextField("  ", 20);
		add("South", strobe);
		strobe.reshape(380,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 =true;
        graph1.borderRight = 0;
        graph1.setDataBackground(new Color(255,230,200));


        graph2 = new Graph2D();
        graph2.drawzero = false;
        graph2.drawgrid =true;
        graph2.borderLeft =  0;
        graph2.setDataBackground(new Color(130,230,130));
/*
**      Build the title
*/
        title = new Label(st, Label.CENTER);
        title.setFont(new Font("TimesRoman",Font.PLAIN,20));




 
        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]=2; data[3]=-2000;

        data1 = graph1.loadDataSet(data, 2);
        data1.linecolor   =  Color.red;
        data1.linestyle = 0;
        data1.marker    = 0;
        data1.markerscale = 1.0;
     
       
/*
**      Calculate the Second data Set.
*/

       data[0] = 0; data[1] = 0; data[2] =12;data[3]=-2000;

        data2 = graph2.loadDataSet(data, np);
        data2.linecolor   =  Color.magenta;
 	 data2.linestyle = 0;
        data2.marker      = 0;
        data2.markercolor = new Color(100,100,255);

/*
**      Attach data sets to the Xaxes
*/
        xaxis1 = graph1.createAxis(Axis.BOTTOM);
        xaxis1.attachDataSet(data1);
	 xaxis1.setTitleText(" strobe view ");
	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("time ( seconds)");
	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.setTitleText("distance in feet");
        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("distance in feet");
        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(90);
      }

public void actionPerformed(ActionEvent evt)
	{ Object src=evt.getSource();
ly = new Double(strobe.getText()).doubleValue();
		m1= ly; m3=m1;
if (m3<0.1) m3=0.1;
if (m3>2.0) m3=2.0;
		strobe.setText(String.valueOf(m3));
		np = (int) (15.0/m3);
		Graphics g;
		double other[]= new double[2*np];
	 	for(i=j=0; i<np; i++,j+=2) {
          	 		other[j] =1 ;
          	 		other[j+1] = -16*m3*m3*i*i;  
			if (other[j+1]<-2000) break;
			nc=i;
			pulse(other);
       			 }
     		
	}
	void pause(int time){
		try{Thread.sleep(time);
		}catch(InterruptedException e) { }
}

void pulse(double other[]){
		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);
 		graph1.update(osg);
		for(i=j=0; i<nc; i++,j+=2) {  
        			 ax = xaxis1.getInteger(1);
			jx= yaxis1.getInteger(other[j+1]);
			
			osg.setColor( Color.black);
			osg.drawOval(ax-5,jx-5,10,10);
 	 		
			
			 }
g.drawImage(osi,0,0,graph1);
       	 		 osg.setColor(Color.red);
			osg.fillOval(ax-5,jx-5,10,10);
	 		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);
 		for(i= j=0; i<nc; i++,j+=2) {  
        			
			kx =xaxis2.getInteger(m3*i);
			lx=yaxis2.getInteger(-16*m3*m3*i*i);
			osg.setColor( Color.black);
			
			osg.drawOval(kx-5,lx-5,10,10);
 	 		
			 }
       	 		g.drawImage(osi,0,0,graph2); 
		osg.setColor(Color.blue);
		osg.fillOval(kx-5,lx-5,10,10);
	 	g.drawImage(osi,0,0,graph2);
pause((int) (m3*1000));
		}


public void update(Graphics screen){
	paint(screen);
	}
}

