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 applet demonstrates derivatives of  trig functions The data is calculated locally by
**    the applet
**
*************************************************************************/
public class difftrig2j extends Applet{

      	Graph2D graph1;
	Graph2D graph2;
	Image osi=null;
	Image theta;
	Graphics osg=null;
	int iwidth = 0;
	int iheight=0; int yvalue=0;
      	DataSet data1;
 	DataSet data2;
	DataSet data3;
	DataSet data4;
	DataSet data5;
	DataSet data6;

      Axis    xaxis1;
      Axis    xaxis2;


      Axis    yaxis1;
      Axis    yaxis2;


      double data[]; double reset[];double reset2[]; int np=2;
double rightsin[]; double rightcos[]; double lowersin[];
   URL markersURL;
      Markers markers;
      Panel      panel;
      Label title;

	 int i; int j; int lx; int ly, ax, jx, jy,a,b, c1,c2, d1, dL;
	 double  a1, b1,b2;

      public void init() {
              	double data[] = new double[2*np];
	double rightsin[]=new double[2*361];
	double rightcos[]=new double[2*361];
	double lowersin[]=new double[2*361];
	theta=getImage(getDocumentBase(),"theta.jpg");
	String st = getParameter("TITLE");
        String mfile    = getParameter("MARKERS");	
/*	
**      Create the Graph instance and modify the default behaviour
*/
      
 graph1 = new Graph2D();
        graph1.drawzero =true;
graph1.zerocolor=Color.black;
        graph1.drawgrid = true;
graph1.gridcolor=Color.blue;
  
        graph1.setDataBackground(new Color(220,220,250));

 graph2 = new Graph2D();
        graph2.drawzero =true;
graph2.zerocolor=Color.black;
        graph2.drawgrid = true;
graph2.gridcolor=Color.blue;

        graph2.setDataBackground(new Color(220,220,250));
       
       
/*
**      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);

        panel = new Panel();
        panel.setLayout( new GridLayout(2,1,0,5) );
        panel.add(graph1);
        panel.add(graph2);

        this.setLayout( new BorderLayout() );
        this.add("North", title);
        this.add("Center", panel);



/*
**      Calculate the first data Set.
*/
        for(i=j=0; i<361; i++,j+=2) {
            rightsin[j] = Math.PI*i/180;
	rightcos[j] =Math.PI*i/180;
	lowersin[j] =Math.PI*i/180;
            	rightsin[j+1] =Math.sin(Math.PI*i/180);
	rightcos[j+1] =Math.cos(Math.PI*i/180);
	lowersin[j+1]= -Math.sin(Math.PI*i/180);
        }
     	 data1 = graph1.loadDataSet(rightsin, 360);
        	data1.linecolor   =  Color.blue;
        	data1.marker      = 0;
        	data1.markercolor = new Color(100,100,255);
       
 	data2 = graph1.loadDataSet(rightcos, 360);
       	 data2.linecolor   =  Color.magenta;
        	data2.marker      = 0;
        	data2.markercolor = new Color(100,100,255);

 	data3 = graph2.loadDataSet(rightcos, 360);
        	data3.linecolor   =  Color.magenta;
        	data3.marker      = 0;
        	data3.markercolor = new Color(100,100,255);

 	data4 = graph2.loadDataSet(lowersin, 360);
        	data4.linecolor   =  new Color(100,0,200);
       	 data4.marker      = 0;
        	data4.markercolor = new Color(100,100,255);

 data[0]=0; data[1]=-0.5; data[2]=0; data[3]=+0.5;

        data5 = graph1.loadDataSet(data, np);
        data5.linecolor   =  Color.black;
        data5.linestyle = 1;

  data6 = graph2.loadDataSet(data, np);
        data6.linecolor   =   new Color(100,0,200);
        data6.linestyle = 1;


/*
**      Attach data sets to the Xaxes
*/
      

 	xaxis1 = graph1.createAxis(Axis.BOTTOM);
        	 xaxis1.attachDataSet(data1);      
	 xaxis1.attachDataSet(data2);
	 xaxis1.attachDataSet(data5);
       	 xaxis1.setTitleText("angle in radians");
	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(data3);
 xaxis2.attachDataSet(data4);
	 xaxis2.attachDataSet(data6);
        xaxis2.setTitleText("angle in radians");
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(data2);
	 yaxis1.attachDataSet(data5);

        yaxis1.setTitleText(" sine");

        yaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        yaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis1.setTitleColor(Color.blue );
        yaxis1.setTitleRotation(90);


yaxis2 = graph2.createAxis(Axis.LEFT);

       yaxis2.attachDataSet(data3);
       yaxis2.attachDataSet(data4);
	yaxis2.attachDataSet(data6);
        yaxis2.setTitleText("cosine");

        yaxis2.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        yaxis2.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis2.setTitleColor(Color.magenta );
        yaxis2.setTitleRotation(90);


      }
public boolean mouseDrag(Event evt, int x, int y)
		{
			a=x; b=y; 
			mousie(a, b);
			return true;
		}
public boolean mouseDown(Event e2, int x, int y)
		{
			a=x; b=y; 
			mousie(a, b);
			return true;
		}

void mousie(int a, int b){
 	a1=xaxis1.getDouble(a);
	b1 =Math.sin(a1);
	b2= Math.cos(a1);
              	double reset[] = new double[2*np];
 	reset[0]=a1-0.4; reset[1]=b1-0.4*b2; 
	reset[2]=a1+0.4; reset[3]=b1+0.4*b2;
	data5.delete(0,2);
		try{    data5.append(reset, 2);}
		     
                   	catch (Exception e) {
                        		System.out.println("Error appending Data!");
                   		}
 xaxis1.setManualRange(true);
		data5.xaxis.minimum=0; data5.xaxis.maximum=6.28;
		yaxis1.setManualRange(true);
		        data5.yaxis.minimum=-1; data5.yaxis.maximum=1;  
              	double reset2[] = new double[2*np];
   	reset2[0]=a1-0.4; reset2[1]=b2+0.4*b1; 
	reset2[2]=a1+0.4; reset2[3]=b2-0.4*b1;
	data6.delete(0,2);
	try{    data6.append(reset2,2);}
		     
                   catch (Exception e) {
                        System.out.println("Error appending Data!");
                 		  }
data6.xaxis.minimum=0; data6.xaxis.maximum=6.28;
		yaxis2.setManualRange(true);
		        data6.yaxis.minimum=-1; data6.yaxis.maximum=1;
    	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);

	c1=xaxis1.getInteger(a1);
	jx = yaxis1.getInteger(b1);
osg.setColor(Color.black);
int mx[]={xaxis1.getInteger(reset[0]), xaxis1.getInteger(reset[2]), xaxis1.getInteger(reset[2]),xaxis1.getInteger(reset[0]), xaxis1.getInteger(reset[0]) };
int my[]={yaxis1.getInteger(reset[1]),yaxis1.getInteger(reset[3]), 2+yaxis1.getInteger(reset[3]), 2+yaxis1.getInteger(reset[1]),yaxis1.getInteger(reset[1]) };
Polygon polyx=new Polygon(mx,my, 5);
osg.fillPolygon(polyx);
	osg.setColor(Color.blue);
	osg.fillOval(c1-5,jx-5,10,10);
	jy=  yaxis1.getInteger(b2);
	osg.setColor(Color.magenta);
	osg.fillOval(c1-5,jy-5,10,10);
osg.drawString(" d(sin(x))/dx = cos(x)", xaxis1.getInteger(0),yaxis1.getInteger(1.1));


                   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);


	c2=xaxis2.getInteger(a1);
	jx = yaxis2.getInteger(b2);
osg.setColor(Color.black);
int lx[]={xaxis2.getInteger(reset2[0]), xaxis2.getInteger(reset2[2]), xaxis2.getInteger(reset2[2]),xaxis2.getInteger(reset2[0]), xaxis2.getInteger(reset2[0]) };
int ly[]={yaxis2.getInteger(reset2[1]),yaxis2.getInteger(reset2[3]), 2+yaxis2.getInteger(reset2[3]), 2+yaxis2.getInteger(reset2[1]),yaxis2.getInteger(reset2[1]) };
Polygon poly=new Polygon(lx,ly, 5);
osg.fillPolygon(poly);
	osg.setColor(Color.magenta);
	osg.fillOval(c1-5,jy-5,10,10);
	jy=  yaxis2.getInteger(-b1);
	osg.setColor(new Color(100,0,200));
	osg.fillOval(c2-5,jy-5,10,10);
osg.drawString(" d(cos(x))/dx = -sin(x)", xaxis2.getInteger(4.5),yaxis2.getInteger(1.1));


                   g.drawImage(osi,0,0,graph2);
}


	

public void update(Graphics screen){
	paint(screen);
	}
}

