import java.awt.*;

public class Fouriersynthese extends java.applet.Applet
{
    int depth = 1;
        
    public void paint(Graphics g)
    {
        int width = getSize().width;
        int height = getSize().height;
        
        double schrittweite = 0.01;
        double max = 2*5.0;
        int x_scale = (int) (width / max);
        int y_scale = (int) (height / 15);
        int y_middle = height / 2;
                
        for(double j = 0.0; j <= max; j += schrittweite)
        {
            int x1 = (int) (j * x_scale);
            int y1 = (int) (y_middle - y_scale * f(j, depth));
            int x2 = (int) ((j+schrittweite) * x_scale);
            int y2 = (int) (y_middle - y_scale * f(j+schrittweite, depth));
            g.drawLine(x1, y1, x2, y2);
            g.drawString("" + depth, 10, 10);
        }
    }
    
    public double f(double x, int depth)
    {
        double a = 4.0 * 5.0 / Math.PI;
        double omega = 2.0 * Math.PI / 5.0;
        double y = 0.0;
        
        for(int n = 1; n <= depth; n += 2)
        {
            y += a/n*Math.sin(n*omega*x);   
        }
        
        return y;
    }
    
    public boolean mouseDown(Event evt, int x, int y)
    {
        if (evt.modifiers == Event.CTRL_MASK)
        {
            depth -= 5;
            if (depth < 1) depth = 1;
            if (evt.clickCount == 2) depth = 1;
        }
        else
        {
            depth += 5;
        }
        
        repaint();
        
        return true;
    }
}
