// Flip two pictures
// by Dobrica Pavlinusic (dpavlin@foi.hr)
// use this, but give me the credits

import java.awt.*;

public class Flip extends java.applet.Applet implements Runnable {
    Image pic[];
    int timeout = 200;
    Thread scroller;
    String file1, file2;
    double a, step;
    int y, picnr = 0;

    public void init() {
	String at = getParameter("img1");
	file1 = (at != null) ? at : "wwwbeldata.gif";
	at = getParameter("img2");
	file2 = (at != null) ? at : "wwwbeldata2.gif";
	at = getParameter("speed");
	timeout = 1000 / ((at == null) ? 10 : Integer.valueOf(at).intValue());
	at = getParameter("step");
	step = ((at == null) ? 0.01 : Double.valueOf(at).doubleValue());

	pic = new Image[2];
	pic[0] = getImage(getDocumentBase(), file1);
	pic[1] = getImage(getDocumentBase(), file2);

	a = 0;
    }

    public void start() {
	if (scroller == null) {
	    scroller = new Thread(this);
	    scroller.start();
	}
    }
    public void stop() {
	if (scroller != null) {
	    scroller.stop();
	    scroller = null;
	}
    }

    public void run() {
	while (true) {
	try {Thread.currentThread().sleep(timeout);} catch (InterruptedException e){}
	    flip();
	}
    }

    synchronized void flip() {
	y = (int) ((size().height / 2) * Math.sin(a));
	a += step;
	if (a > Math.PI) {
		a = 0;
		picnr = picnr ^ 1;
	}
	repaint();
    }

    public void update(Graphics g) {

	int t;

	t = (size().height / 2) - y;

	g.setColor(Color.lightGray);
	g.fillRect(1, 1, size().width, t);
	g.fillRect(1, size().height-t, size().width, t);
	g.drawImage(pic[picnr], 1, t, size().width, y * 2, this);
	paint(g);
    }

}
