import java.awt.*;
import java.awt.event.*;

class Canvas_graph extends java.awt.Canvas implements AdjustmentListener
{
  Dimension size;
  double omega;
  double amplitude;
  double phase;
 	
  public Dimension getPreferredSize()
	{
	  return getMinimumSize();
	}

	public Dimension getMinimumSize()
	{
	  return size;
	}
  

	public Canvas_graph(int initialWidth, int initialHeight, double freq, double ampl, int ph, int n)
	{
	  size = new Dimension(initialWidth, initialHeight);
	  omega = freq;
	  amplitude = (ampl)/20;
	  phase = ph/90*java.lang.Math.PI;
		switch(n)
		{
		  case 1:	Vibration.f1.addAdjustmentListener(this);
		  					Vibration.a1.addAdjustmentListener(this);
		  					break;
		  case 2:	Vibration.f2.addAdjustmentListener(this);
								Vibration.a2.addAdjustmentListener(this);
								Vibration.p2.addAdjustmentListener(this);
		  					break;
		}
	  
	}	
	
	double f(double x)
  {
   	return (-amplitude*Math.sin(x*omega-phase)+ 1) * size.height/2;
  }

  public void paint(Graphics g)
  {
  	g.drawRect(0,0,size.width,size.height-1);
    g.drawLine(0,size.height/2,size.width,size.height/2);
    
//    System.out.println("omega=" + this.omega);
    for (int x = 0 ; x < size.width ; x++)
    {
      g.drawLine(x, (int)f(x), x + 1, (int)f(x + 1));
//      System.out.println("f(x)=" + f(x));
    }
  }
  
  public void adjustmentValueChanged(AdjustmentEvent evt)
  {
   // System.out.println("Value:" + evt.getValue());
  	Component c =(Component) evt.getSource();
  	char name = c.getName().charAt(1);
		//System.out.println("name:" + name);
 
		switch(name)
		{
			case 'A':
				this.amplitude = evt.getValue();
				this.amplitude = 1-(this.amplitude/20);
				break;
			case 'F':
				this.omega = evt.getValue();
				this.omega /=600;
				break;
			case 'P':
				this.phase = (double)evt.getValue()/90*java.lang.Math.PI;
				//System.out.println("phase:" + phase);
				break;
		}
    this.repaint();
    Vibration.t1.repaint();
   }
}