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 slope3c extends Applet{

      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[]; double other[]; double slopeline[];
int np=50;
      URL markersURL;
      Markers markers;
      Panel      panel;
     Label title;
	 int i; int j; int lx; int ly, ax, jx, a, b,x;
	 double m1,m3, a1, b1, slope;
       

      public void init() {
        double data[] = new double[2*np];
double slopeline[] =new  double[6];
/*
**      Get the passed parameters
*/
       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(255,230,200));


        graph2 = new Graph2D();
       graph2.gridcolor=Color.black;
 graph2.drawzero =false;
	
        graph2.drawgrid =true;
        graph2.borderLeft =  0;
        graph2.setDataBackground(Color.yellow);

/*
**      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]=-2.5; data[1]=-8; data[2]=2.5; data[3]=8;

        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-25)*1.0/10;
            data[j+1] =3*data[j]*data[j]-2*data[j]-4;

        }

        data2 = graph2.loadDataSet(data, np);
data2.linestyle = 1;
        data2.linecolor   =  Color.magenta;
        data2.marker      = 0;
        data2.markercolor = new Color(100,100,255);
/*
**	third data set
*/

 for(i=j=0; i<np; i++,j+=2) {
            data[j] = (i-25)*1.0/10.0;
            data[j+1] =(data[j]-1)*(data[j]-2)*(data[j]+2) ;
        }
 data3 = graph1.loadDataSet(data,np);
 	data3.linestyle =1;
        data3.linecolor   =  Color.black;
        data3.marker      = 0;
        data3.markercolor = Color.black;

     a1=1.0; b1=(a1-1)*(a1-2)*(a1+2);

slopeline[0] =-2.5; slopeline[1]=-8;
slopeline[2] =-2.5; slopeline[3]=-8;
slopeline[4] =-2.5; slopeline[5]=-8;
        data4 = graph1.loadDataSet(slopeline, 3);
        data4.linecolor   =  Color.blue;
        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("x ");
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("x");
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("y=(x-1)(x-2)(x+2)");
        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("slope of y");
        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 boolean mouseDrag(Event evt, int x, int y)
		{
			a=x; b=y; 
			mousie(x);
			return true;
		}
public boolean mouseDown(Event e2, int x, int y)
		{
			a=x; b=y; 
			mousie(x);
			return true;
		}

void mousie(int x){
		a =x; a1=xaxis1.getDouble(x);
		b1=(a1-1)*(a1-2)*(a1+2);
		b=yaxis1.getInteger(b1);
		slope = 3*a1*a1-2*a1-4;
Graphics g;
		
        double slopeline[]= new double[6];
	slopeline[0] = -2.5;
	slopeline[1] = slope*(-2.5-a1)+b1;
slopeline[2]= a1; slopeline[3] =b1; 
slopeline[4] =2.5; slopeline[5] =slope*(2.5-a1)+b1;
data4.delete(0,3);
		try{    data4.append(slopeline, 3);}
		     
                   catch (Exception e) {
                        System.out.println("Error appending Data!");
                   }
       xaxis1.setManualRange(true);
		data4.xaxis.minimum=-2.5; data4.xaxis.maximum=2.5;
		yaxis1.setManualRange(true);
		        data4.yaxis.minimum=-8.0; data4.yaxis.maximum=8.0;  
		  
            
	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.red);
osg.fillOval(a-5,b-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); 
osg.setColor(Color.blue);
ax=xaxis2.getInteger(a1);
jx=yaxis2.getInteger(slope);
osg.fillOval(ax-5,jx-5,10,10);

g.drawImage(osi,0,0,graph2);}
	

public void update(Graphics screen){
	paint(screen);
	}
}

