import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import graph.*;
/*************************************************************************
**
**    Applet linear1a
**                                              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.
**************************************************************************
**
**    Applet plotcross2a is Copyright (C) 2001 Louise Wilson 
**    It is free software, to help you learn Java
**    
**
*************************************************************************/

public class plotcross2a extends Applet implements ActionListener{
	LoadData dynamic;
      	Graph2D graph1; 
	Image osi=null;
	Graphics osg=null;
	int iwidth = 0;
	int iheight=0;
  	DataSet data3;   	
	DataSet data4;
      	Axis    xaxis1;
      	Axis    yaxis1;
      	double data[];
      	int np = 3;

      	Panel      panel;
 
	Button refresh;
	TextField slope1, intercept1, slope2, intercept2, equation1, equation2, crosspoint;
	int i,j,lax,lay; double m1,m2,b1,b2, ba1, ba2,ma1,ma2,lx,ly,lbx, lby;
	
      public void init() {
       		double data[] = new double[2*np];
	        	setLayout( new BorderLayout() );			
		equation1=new TextField("line:  y=mx + b ", 20);
		add("West", equation1);
		equation1.reshape(1,10,100,25);
		slope1=new TextField(" ", 20);
		add("West", slope1);
		slope1.reshape(30,40,40,25);

		 intercept1=new TextField(" ", 20);
		add("West", intercept1);
		intercept1.reshape(100,40,40,25);

		 equation2=new TextField("line:  y=mx + b ", 20);
		add("West", equation2);
		equation2.reshape(1,100,100,25);
		 slope2=new TextField(" ", 20);
		add("West", slope2);
		slope2.reshape(30,140,40,25);

		 intercept2=new TextField(" ", 20);
		add("West", intercept2);
		intercept2.reshape(100,140,40,25);

crosspoint=new TextField(" ", 75);
		add("West",crosspoint);
		crosspoint.reshape(1,350,75,25);

		Label intercept1label=new Label("b1"); 
		add("West", intercept1label); intercept1label.reshape(80,40,20,10);
		Label intercept2label=new Label("b2"); 
		add("West", intercept2label); intercept2label.reshape(80,140,20,10);
		Label slope1label=new Label(" m1"); 
		add("West",slope1label);slope1label.reshape(1,40,30,10);
		Label slope2label=new Label(" m2"); 
		add("West",slope2label);slope2label.reshape(1,140,30,10);
Label crosspointlabel=new Label(" line intersection:"); 
		add("West",crosspointlabel);crosspointlabel.reshape(1,330,100,15);
     		
refresh = new Button("refresh");
add("West", refresh); refresh.reshape(30, 250,70,20);
refresh.addActionListener(this);
	

		Label graphlabel=new Label("2 lines");
		add("West", graphlabel); graphlabel.reshape(1,380,10,10);

	

      /*
**      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(150,200,230));
	
        add("Center", graph1);

/*
**	data set 3
*/
	 m1=1.00;b1=1.00;
	 for(i=j=0; i<np; i++,j+=2) {
            		data[j] =( i-1)*10;
           		 data[j+1] = (m1*data[j] +b1);
       		 }
        	data3 = graph1.loadDataSet(data,np);       
        	data3.linecolor   =  Color.red;
        	data3.linestyle = 1;
        	data3.marker    = 0;
          
/*
**	data set 4
*/
	 m2=-1.00; b2 = -1.00;
	 for(i=j=0; i<np; i++,j+=2) {
            		data[j] =( i-1)*10;
            		data[j+1] = (m2*data[j] + b2);
        		}
        	data4 = graph1.loadDataSet(data,np);
        	data4.linecolor   =  Color.red;
        	data4.linestyle = 1;
        	data4.marker    = 0;
        
/*	
**      Attach data sets to the Xaxes
*/
        	xaxis1 = graph1.createAxis(Axis.BOTTOM);
	xaxis1.attachDataSet(data3);
	xaxis1.attachDataSet(data4);
       	 xaxis1.setTitleText("x");
       	 xaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
       	 xaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
	xaxis1.setTitleColor( new Color(0,0,255) );
        
/*
**      Attach the first data set to the Left Axis
*/
       	 yaxis1 = graph1.createAxis(Axis.LEFT);
	yaxis1.attachDataSet(data3);
	yaxis1.attachDataSet(data4);
        	yaxis1.setTitleText("y");
        	yaxis1.setTitleFont(new Font("TimesRoman",Font.PLAIN,20));
       	 yaxis1.setLabelFont(new Font("Helvetica",Font.PLAIN,15));
        	yaxis1.setTitleColor( new Color(0,0,255) );
        	yaxis1.setTitleRotation(90);
	
	
	}

	
public void actionPerformed(ActionEvent evt)
	{ Object src = evt.getSource();

	ba1 = new Double( intercept1.getText()).doubleValue();
	 intercept1.setText(String.valueOf(ba1));
equation1.setText("y = "+ma1+"x +" +ba1);
	ma1 = new Double(slope1.getText()).doubleValue();
	slope1.setText(String.valueOf(ma1));
equation1.setText("y = "+ma1+"x +" +ba1);
	ba2 = new Double(intercept2.getText()).doubleValue();
	 intercept2.setText(String.valueOf(ba2));
equation2.setText("y = "+ma2+"x +" +ba2);
	ma2 = new Double(slope2.getText()).doubleValue();
	slope2.setText(String.valueOf(ma2));
	
	equation2.setText("y = "+ma2+"x +" +ba2);
	
Graphics g;
		double other[]= new double[2*np];
		 for(i=j=0; i<np; i++,j+=2) {
            			other[j] =( i-1)*10;
           			 other[j+1] = (ma1*other[j] + ba1);
        			}
        
		data3.delete(0, np);
		try{    data3.append(other, np);}
		     
                   	catch (Exception e) {
                        		System.out.println("Error appending Data!");
                  		 }

		 for(i=j=0; i<np; i++,j+=2) {
           			 other[j] =( i-1)*10;
            			other[j+1] = (ma2*other[j] + ba2);
       			 }
        
		data4.delete(0,np);
		try{    data4.append(other,np);}
		     
                   	catch (Exception e) {
                        		System.out.println("Error appending Data!");
                  		 }
        
     		 xaxis1.setManualRange(true);
		data3.xaxis.minimum=-10.00; data3.xaxis.maximum=10.0;
		data4.xaxis.minimum=-10.00; data4.xaxis.maximum=10.0;
		yaxis1.setManualRange(true);
	        	data3.yaxis.minimum=-100.0; data3.yaxis.maximum=100.0; 
	        	data4.yaxis.minimum=-100.0; data4.yaxis.maximum=100.0;  
if (Math.abs( ma1-ma2)<0.01)
crosspoint.setText("parallel lines"); 
else	
{lx=(ba2-ba1)/(ma1-ma2); lax=(int)(lx*100); lbx = 0.01*lax;
	ly = ma1*lx+ba1;  lay=(int)(ly*100); lby=0.01*lay;
		crosspoint.setText("( "+lbx+" ,  "+lby+" )"); }
			
	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);
			
		}

public void update(Graphics screen){
	paint(screen);
	}
		
	
 

}

        
		



