import ifi.math.function.DiscreteFunction;
import java.applet.Applet;
import java.awt.*;


/*
 * Applet which tests the PlotWindow class.
 */
public class PlotWindowApplet extends Applet{
  // Data members
  // Sampled functions
  int no_samples = 201;
  DiscreteFunction sin = new DiscreteFunction(no_samples);
  DiscreteFunction cos = new DiscreteFunction(no_samples);

  // Fixed interval to sample in.
  double start = 0.0;
  double stop  = 6.28;

  // GUI elements
  PlotWindow plotwin = new PlotWindow(2);

  /*
   * The init method builds the user interface. It is also responsible
   * for initializing the sampled functions.
   */
  public void init(){
    // Calculate function samples.
    double x, delta = (stop - start) / (no_samples - 1);

    for(int i = 0; i < no_samples; i++){
      x = start + i * delta;

      cos.setX(i, x);
      cos.setY(i, Math.cos(x));

      sin.setX(i, x);
      sin.setY(i, Math.sin(x));
    }

    // Add the functions to the PlotWindow

    plotwin.setTitle("Periodic functions");
    plotwin.setXLabel("X");

    plotwin.setActivePlotArea(0);
    plotwin.setYLabel("Sinus(x)");
    plotwin.addFunction(sin);

    plotwin.setActivePlotArea(1);
    plotwin.setYLabel("Cosinus(x)");
    plotwin.addFunction(cos);

    // Build graphical user interface
    // BorderLayout is a good choice of LayoutManager for this
    // applet. We use the CENTER panel to hold the PlotWindow.

    setLayout(new BorderLayout());
    add(plotwin, BorderLayout.CENTER);

  }

  /*
   * Painting of this applet is handled by PlotWindow.
   */
  public void paint(Graphics g){
    plotwin.repaint();
  }
}
