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 trig functions The data is calculated locally by
**    the applet
**
*************************************************************************/
public class trig6a extends Applet{

      Graph2D graph1;
      Graph2D graph2;
Graph2D graph3;
Image osi=null;
Image theta;
	Graphics osg=null;
	int iwidth = 0;
	int iheight=0; int yvalue=0;
      DataSet data1;
 DataSet data2;
DataSet data3;
     Axis    xaxis1;
      Axis    xaxis2;
      Axis    xaxis3;
      Axis    yaxis1;
      Axis    yaxis2;
      Axis    yaxis3;

      double data[];  int np=2;
double rightsin[]; double rightcos[];
   URL markersURL;
      Markers markers;
      Panel      panel;
Panel panel1;
      Label title;

	 int i; int j; int lx; int ly, ax, jx, jy,a, b,cx,cy,dy,ey,c1,cL, d1, dL;
	 double  a1, b1,DEX,REX;
       TextField xtext,ytext,sintext,costext, angletext,radianstext, faketext;

      public void init() {
              double data[] = new double[2*np];
double rightsin[]=new double[2*361];
double rightcos[]=new double[2*361];
theta=getImage(getDocumentBase(),"theta.jpg");

		xtext=new TextField("x: ", 20);
		add("South",xtext);
		xtext.reshape(100,350, 60,20);
		ytext=new TextField("y: ", 20);
		add("South",ytext);
		ytext.reshape(250,350, 60,20);
		sintext=new TextField("sin: ", 20);
		add("South",sintext);
		sintext.reshape(550,350, 80,20);
		costext=new TextField("cos: ", 20);
		add("South",costext);
		costext.reshape(400,350, 80,20);
		angletext=new TextField("degrees: ", 40);
		add("South",angletext);
		angletext.reshape(100,330, 100,20);
		radianstext=new TextField("radians: ", 40);
		add("South",radianstext);
		radianstext.reshape(250,330, 100, 20);
		faketext=new TextField(" ", 1);
		add("South",faketext);
	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.gridcolor=Color.black;
        graph1.borderRight = 0;
        graph1.setDataBackground(new Color(170,170,250));

 graph2 = new Graph2D();
        graph2.drawzero =true;
graph2.zerocolor=Color.black;
        graph2.drawgrid = true;
        graph2.borderLeft =  0;
        graph2.setDataBackground(new Color(190,190,250));

 graph3 = new Graph2D();
        graph3.drawzero =true;
graph3.zerocolor=Color.black;
        graph3.drawgrid = true;
        graph3.borderLeft =  0;
        graph3.setDataBackground(new Color(190,190,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);
        graph2.setMarkers(markers);

        panel = new Panel();
        panel.setLayout( new GridLayout(2,1,0,5) );
        panel.add(graph2);
        panel.add(graph3);

panel1 = new Panel();
        panel1.setLayout( new GridLayout(1,2,5,0) );
        panel1.add(graph1);
        panel1.add(panel);


        this.setLayout( new BorderLayout() );
        this.add("North", title);
        this.add("Center", panel1);



/*
**      Calculate the first data Set.
*/

           data[0]=-1.1; data[1]=-1.02; data[2]=1.1; data[3]=1.02;

        data1 = graph1.loadDataSet(data, np);
        data1.linecolor   =  Color.red;
        data1.linestyle = 0;

        for(i=j=0; i<361; i++,j+=2) {
            rightsin[j] = Math.PI*i/180;
rightcos[j] = Math.PI*i/180;
            rightsin[j+1] =Math.sin(Math.PI*i/180);
rightcos[j+1] =Math.cos(Math.PI*i/180);
        }
      data2 = graph2.loadDataSet(rightsin, 360);
        data2.linecolor   =  Color.blue;
        data2.marker      = 0;
        data2.markercolor = new Color(100,100,255);
       
 data3 = graph3.loadDataSet(rightcos, 360);
        data3.linecolor   =  Color.magenta;
        data3.marker      = 0;
        data3.markercolor = new Color(100,100,255);
       
/*
**      Attach data sets to the Xaxes
*/
        xaxis1 = graph1.createAxis(Axis.BOTTOM);
        xaxis1.attachDataSet(data1);
        xaxis1.setTitleText(" ");
	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("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));

 xaxis3 = graph3.createAxis(Axis.BOTTOM);
   
 xaxis3.attachDataSet(data3);
        xaxis3.setTitleText("angle in radians");
xaxis3.setTitleColor( new Color(0,0,255) );
        xaxis3.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        xaxis3.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("y");
        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);

yaxis2 = graph2.createAxis(Axis.RIGHT);
        yaxis2.attachDataSet(data2);

        yaxis2.setTitleText(" sine");

        yaxis2.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        yaxis2.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis2.setTitleColor(Color.blue );
        yaxis2.setTitleRotation(90);

yaxis3 = graph3.createAxis(Axis.RIGHT);

       yaxis3.attachDataSet(data3);
        yaxis3.setTitleText("cosine");

        yaxis3.setTitleFont(new Font("TimesRoman",Font.PLAIN,15));
        yaxis3.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        yaxis3.setTitleColor(Color.magenta );
        yaxis3.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){
		cx=xaxis1.getInteger(0.0); cy=yaxis1.getInteger(0.0); c1=xaxis1.getInteger(1.0); cL=c1-cx;	
			d1=yaxis1.getInteger(-1.0); dL=d1-cy;	
dy=yaxis2.getInteger(0.0);
ey=yaxis3.getInteger(0.0);
 a1=xaxis1.getDouble(a);
	if (a1>1.0){ a1=1.0; a=c1;}
	else  if (a1<-1.0) { a1=-1.0; a=cx-cL;}
		
		b1=Math.sqrt(1-a1*a1);
		if (b>cy) b1=-b1;
		b=yaxis1.getInteger(b1);
if (a1>0)
if (b1>0)
{DEX=180*Math.asin(b1)/Math.PI;
REX = Math.asin(b1);}
else 
{DEX=360+180*Math.asin(b1)/Math.PI;
REX =2* Math.PI+Math.asin(b1);}
else 
{DEX=180-180*Math.asin(b1)/Math.PI;
REX =Math.PI - Math.asin(b1);}

		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);
	osg.setColor(Color.black);
	osg.drawOval(cx-cL,cy-dL,2* cL,2* dL);
	osg.drawLine(cx,cy, a,b);
osg.drawArc(cx-40,cy-40, 80,80,0, (int) DEX);
	g.drawImage(theta, cx+20,cy-20, this);

	osg.setColor(Color.red);
	osg.fillOval(a-5,b-5,10,10);
	osg.setColor(Color.magenta);
osg.drawLine(cx, cy, a,cy);
	osg.setColor(Color.blue);
osg.drawLine(a,cy,a,b);

    	 g.drawImage(osi,0,0,graph1);
g.drawImage(theta, cx+20,cy-20, this);
xtext.setText("x="+ a1);ytext.setText("y="+ b1); 
sintext.setText("sin="+b1); costext.setText("cos="+ a1); 
angletext.setText("degrees="+DEX);radianstext.setText("radians="+REX);

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(REX);
jx = yaxis3.getInteger(Math.sin(REX));
osg.setColor(Color.blue);
osg.fillOval(ax-5,jx-5,10,10);
osg.drawLine(ax, dy, ax, jx);


                   g.drawImage(osi,0,0,graph2);

g = graph3.getGraphics();
                   if( osi == null || iwidth != graph3.size().width
                                   || iheight != graph3.size().height  ) {
                       iwidth = graph3.size().width;
                       iheight = graph3.size().height;
                       osi = graph3.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);
           graph3.update(osg);

          ax = xaxis3.getInteger(REX);
jy = yaxis3.getInteger(Math.cos(REX));
osg.setColor(Color.magenta);
osg.fillOval(ax-5,jy-5,10,10);
osg.drawLine(ax, ey, ax, jy);


                   g.drawImage(osi,0,0,graph3);
}


	

public void update(Graphics screen){
	paint(screen);
	}
}

