import java.applet.*;
import java.awt.*;
import java.awt.image.*;

public class main extends Applet implements Runnable {

	final int 			W	 			= 320,
						H 				= 240;
	Graphics			backBuffer		= null;
	Image				img				= null,
						offscreen		= null,
						mis_img			= null;
	MediaTracker		mt				= null;
	Thread 				th				= null;
	MemoryImageSource	mis				= null;
	int 				currentIndex	= 0,
						pixels[]		= new int[W*H],
						img_pixels[]	= new int[W*H];
	double 				angle 			= 0.0;
	int 				angle_start 	= 0;
	
	public void init()
	{
		this.setBackground(Color.WHITE);
		this.setSize(W,H);
		
		mt = new MediaTracker(this);	
		
		img = this.getImage(this.getCodeBase(),"image.jpg");		
		mt.addImage(img,0);
		
		offscreen = this.createImage(W,H);
		backBuffer = offscreen.getGraphics();
		
		try {
			mt.waitForAll();
		} catch (InterruptedException ie) {
			System.err.println("MediaTracker : Interrupted!");
			return;
		}
		
		PixelGrabber pg = new PixelGrabber(img, 0, 0, W, H, img_pixels, 0, W);
		try {
		    pg.grabPixels();
		} catch (InterruptedException e) {
		    System.err.println("PixelGrabber : Interrupted!");
		    return;
		}

		//pixels = img_pixels;
		mis = new MemoryImageSource(W, H, pixels, 0, W);
		mis.setAnimated(true);
	    mis_img = createImage(mis);		    				
	}	
	
	public void start()
	{
		th = new Thread(this);
		th.start();
	}
	
	public void run()
	{
		try {
			mt.waitForAll();
		} catch (InterruptedException ie) {
			System.err.println("MediaTracker : Interrupted!");
			return;
		}
				
	    while (th != null)
	    {	
	    	if (angle_start < 359) angle_start += 2;
	    	else angle_start = 0;
	    	
	    	angle = angle_start;
	    	
			for (int x=0; x<W; x++)
			{								
				if (angle < 359.0) angle += 1.0;									
				else angle = 0.0;
				
				double curve = Math.sin(angle*Math.PI/180)*50;
				
				for (int y=0; y<H; y++)
				{
					int tp = (int)(curve+y)*W+(int)(x+curve);
					if (tp < W*H && tp > -1)
						pixels[y*W+x] = img_pixels[tp];							
				}
			}
	    	
			repaint();
			
			try {
				th.sleep(10);
			} catch (InterruptedException e) {}
	    }
	}
	
	public void stop()
	{
		th.stop();
		th = null;
	}
	
	public void paint (Graphics g)
	{	
		mis.newPixels(0,0,W,H);
		backBuffer.drawImage(mis_img, 0,0, this);
		g.drawImage(offscreen, 0,0, this);
	}
	
	public void update (Graphics g)
	{
		paint(g);
	}
}
