
import java.awt.*;

public class Line extends java.applet.Applet implements Runnable {
    Thread timer;
    double f;
    int off;
    
    public void start() {
	timer = new Thread(this);
	timer.start();
	f = 1.0;
    }
    public void stop() {
	if (timer != null) {
	    timer.stop();
	    timer = null;
	}
    }
    public void run() {
	while (timer != null) {
	    if (f > 20.0) {
		f = 1.0;
	    } else {
		f *= 1.02;
	    }
	    off += 2;
	    repaint();
	    try {
			Thread.sleep(100);
		}
		catch (InterruptedException e)
		{
		}
	}
    }

    int func(int x, int h) {
	return h/2 + (int)((h * Math.sin((x+off)*f)) / 2);
    }

    Image offscreen;
    Dimension offscreensize;
    Graphics offgraphics;

    public void update(Graphics g) {
	Dimension d = size();
	if ((offscreen == null) || (d.width != offscreensize.width) || (d.height != offscreensize.height)) {
	    offscreen = createImage(d.width, d.height);
	    offscreensize = d;
	    offgraphics = offscreen.getGraphics();
	    offgraphics.setFont(getFont());
	}
	offgraphics.setColor(Color.lightGray);
	offgraphics.fillRect(0, 0, d.width, d.height);
	offgraphics.setColor(Color.gray);

	int v1 = func(0, d.height), v2;
	int step = 2;

	for (int i = step ; i < d.width ; i += step) {
	    v2 = func(i, d.height);
	    offgraphics.drawLine(i - step, v1, i, v2);
	    v1 = v2;
	}
	g.drawImage(offscreen, 0, 0, null);
    }
}
