// --------------------------------------------
// PROGRAM   : SINPLOT.JAVA
// --------------------------------------------
// I wrote this program while playing around
// with math and graphics methods.
// No 3D correction is considered. Thus, the
// curves seem to be distorted in 3D space.
// --------------------------------------------
// AUTHOR    : Andre Maier
// --------------------------------------------

import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

public class sinplot extends Applet implements AdjustmentListener,
					       ItemListener
{
   // Declaration of variables

	double i,j1,j2,cd;
	int x1,x2,y1,y2;
	int a,b,c;
	int old_x1, old_x2, old_y1, old_y2;
	String Function1, Function2;

	Scrollbar slider1, slider2, slider3;
	Label label1, label2, label3, label4;
	Choice curve1;
	Choice curve2;

   // The paint method will calculate and
   // plot the functions.

   public void paint(Graphics g)
	{
		cd = c/300.0;
		old_x1 = 0; old_x2 = 0; old_y1 = 0; old_y2 = 0;
		for(i=0;i<(6.28);i=i+0.001+cd)
		{
			if(Function1=="SINUS")
			{
			   j1 = 50*(Math.sin(i));
			}
			if(Function1=="COSINUS")
			{
			   j1 = 50*(Math.cos(i));
			}
			if(Function1=="ZERO")
			{
			   j1 = 0;
			}
			if(Function1=="TANGENS")
			{
			   j1 = 2*(Math.tan(i));
			   // Maximum value of TAN is limited to 50
			   if(j1>50.0) { j1 = 50.0; }
			   if(j1<-50.0) { j1 = -50.0; }
			}
			if(Function1=="RANDOM")
			{
			   j1 = 50*Math.random();
			}
			if(Function2=="SINUS")
			{
			  j2 = 50*(Math.sin(i));
			}
			if(Function2=="COSINUS")
			{
			   j2 = 50*(Math.cos(i));
			}
			if(Function2=="ZERO")
			{
			   j2 = 0;
			}
			if(Function2=="TANGENS")
			{
			   j2 = 2*(Math.tan(i));
			   // Maximum value of TAN is limited to 50
			   if(j2>50.0) { j2 = 50.0; }
			   if(j2<-50.0) { j2 = -50.0; }
			}
			if(Function2=="RANDOM")
			{
			   j2 = 50*Math.random();
			}

         // Here the calculated values are scaled and
         // rounded to get integer values.

			x1 = (int) Math.round(i*((300+120)/6.28));
			y1 = (int) Math.round(j1);
			x2 = (int) Math.round(i*(500/6.28));
			y2 = (int) Math.round(j2);

         // Now we plot the functions and draw lines
         // connecting each calculated dot.

         g.drawLine(x1+200-a,y1+100+b,x2+a,y2+400-b);
			if(i>0)
			{
            g.drawLine(old_x1+200-a,old_y1+100+b,x1+200-a,y1+100+b);
            g.drawLine(old_x2+a,old_y2+400-b,x2+a,y2+400-b); 
			}
			old_x1 = x1; old_y1 = y1;
			old_x2 = x2; old_y2 = y2;
		}
	}

   // Event Handling

	public void adjustmentValueChanged(AdjustmentEvent e)
	{
		if(e.getAdjustable()==slider1)
		{
			label1.setText("a = " 
				+ String.valueOf(slider1.getValue()));
			a = slider1.getValue();
			repaint();
		}
		if(e.getAdjustable()==slider2)
		{
			label2.setText("b = " 
				+ String.valueOf(slider2.getValue()));
			b = slider2.getValue();
			repaint();
		}
		if(e.getAdjustable()==slider3)
		{
			label3.setText("c = "
				+ String.valueOf(slider3.getValue()));
			c = slider3.getValue();
			repaint();
		}
	}

	public void itemStateChanged(ItemEvent e)
	{
		if(e.getItem()=="1:SINUS")
		{
	  	   Function1 = "SINUS";
		   repaint();	
		}
		if(e.getItem()=="1:COSINUS")
		{
		   Function1 = "COSINUS";
		   repaint();
		}
		if(e.getItem()=="1:ZERO")
		{
		   Function1 = "ZERO";
		   repaint();
		}
		if(e.getItem()=="1:TANGENS")
		{
		   Function1 = "TANGENS";
		   repaint();
		}
		if(e.getItem()=="1:RANDOM")
		{
		   Function1 = "RANDOM";
		   repaint();
		}
		if(e.getItem()=="2:SINUS")
		{
		   Function2 = "SINUS";
		   repaint();
		}
		if(e.getItem()=="2:COSINUS")
		{
		   Function2 = "COSINUS";
		   repaint();
		}
		if(e.getItem()=="2:ZERO")
		{
		   Function2 = "ZERO";
		   repaint();
		}
		if(e.getItem()=="2:TANGENS")
		{
		   Function2 = "TANGENS";
		   repaint();
		}
		if(e.getItem()=="2:RANDOM")
		{
		   Function2 = "RANDOM";
		   repaint();
		}
	}

   // This is the point where our program starts

	public void init()
	{
      // Default values
		a = 190; b = 20; c = 30;
		Function1 = "SINUS";
		Function2 = "SINUS";

      // Some values for the Applet
		this.setBackground(new Color(0,0,0));
		this.setForeground(new Color(0,255,255));
		this.setLayout(null);

		// *** SCROLLBARS ***
		slider1 = new Scrollbar(Scrollbar.HORIZONTAL);
		slider2 = new Scrollbar(Scrollbar.HORIZONTAL);
		slider3 = new Scrollbar(Scrollbar.HORIZONTAL);
		slider1.setValues(0,10,0,200);
		slider2.setValues(0,10,0,200);
		slider3.setValues(0,10,0,200);
		slider1.setBounds(new Rectangle(0,0,200,15));
		slider2.setBounds(new Rectangle(0,15,200,15));
		slider3.setBounds(new Rectangle(0,30,200,15));
		slider1.setBackground(new Color(150,150,150));
		slider2.setBackground(new Color(150,150,150));
		slider3.setBackground(new Color(150,150,150));
		slider1.addAdjustmentListener(this);
		slider2.addAdjustmentListener(this);
		slider3.addAdjustmentListener(this);
		slider1.setValue(a);
		slider2.setValue(b);
		slider3.setValue(c);
		this.add(slider1);
		this.add(slider2);
		this.add(slider3);
		
		// *** LABELS ***
		label1 = new Label("");
		label2 = new Label("");
		label3 = new Label("");
		label4 = new Label("SINEPLOT (c)2000 by Andre Maier");
		label1.setBounds(new Rectangle(220,0,50,15));
		label2.setBounds(new Rectangle(220,15,50,15));
		label3.setBounds(new Rectangle(220,30,50,15));
		label4.setBounds(new Rectangle(410,460,300,15));
		label1.setForeground(new Color(255,255,0));
		label2.setForeground(new Color(255,255,0));
		label3.setForeground(new Color(255,255,0));
		label4.setForeground(new Color(255,255,255));
		this.add(label1);
		this.add(label2);
		this.add(label3);
		this.add(label4);
		label1.setText("a = " + a);
		label2.setText("b = " + b);
		label3.setText("c = " + c);

		// *** CHOICES ***
		curve1 = new Choice();
		curve2 = new Choice();
		curve1.add("1:SINUS");
		curve1.add("1:COSINUS");
		curve1.add("1:ZERO");
		curve1.add("1:TANGENS");
		curve1.add("1:RANDOM");
		curve2.add("2:SINUS");
		curve2.add("2:COSINUS");
		curve2.add("2:ZERO");
		curve2.add("2:TANGENS");
		curve2.add("2:RANDOM");
		curve1.setBounds(300,0,100,15);
		curve2.setBounds(400,0,100,15);
		curve1.setBackground(new Color(150,150,150));
		curve2.setBackground(new Color(150,150,150));
		curve1.setForeground(Color.black);
		curve2.setForeground(Color.black);
		curve1.addItemListener(this);
		curve2.addItemListener(this);
		this.add(curve1);
		this.add(curve2);
		
	} 

	public void destroy()
	{
	}

	public String getAppletInfo()
	{
		return "This Applet was written by Andre Maier";
	}
}
