package ptolemy.plot;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;

import javax.swing.JPanel;
import java.util.*;
import java.net.URL;

import ptolemy.plot.*;


public class PlotPanel extends JPanel {
    private static boolean DEBUG = true;
    private Plot plot;
   
    public void init() {
        plot = new Plot();
	add(plot);
    }

    public void update(Vector list) {

	Integer Inum = (Integer)list.elementAt(0);
	int num = Inum.intValue();
	System.err.println("num="+num);

        plot.setTitle("Fourier Series Approximation to a Square Wave");
        plot.setXRange(0, 400);
        plot.setMarksStyle("none");
        plot.addLegend(0, "ideal");
        plot.addLegend(1, "1 sinusoid");
        for (int j = 2; j <= 4; j++) {
            plot.addLegend(j, j + " sinusoids");
        }

        boolean first = true;
        plot.addPoint(0, 0.0, 0.0, false);
        for (int i = 0; i <= 400; i++) {
            double approximation = 0.0;
            for (int j = 1; j <= 4; j++) {
                double sig = 4.0*Math.sin(i*2.0*Math.PI*(2*j-1)/400.0)/
                    (Math.PI*(2*j-1));
                approximation += sig;
                plot.addPoint(j, (double)i, approximation, !first);
            }
            first = false;
            if (i <= 200) {
                plot.addPoint(0, (double)i, 1.0, true);
            }
            if (i >= 200) {
                plot.addPoint(0, (double)i, -1.0, true);
            }
        }
        plot.addPoint(0, 400.0, 0.0, true);
    }
}

