import java.awt.*;

public class LakeApplet extends AnimatedApplet
{
    int image[], imageWidth, imageHeight;
    
    public void AAinit()
    {
     	// We're going to partially update the applet manually, since the top half will remain static
        setFullUpdate(false);
        setFPS(25);
    	
        // Load image
        try
        {
            Image tmp = loadImage(getParameter("Image"));
            
            imageWidth = tmp.getWidth(this);
            imageHeight = tmp.getHeight(this);
            
            image = grabPixels(tmp);
        }
        catch (Exception e)
        {
            System.out.println(""+e);
            System.exit(-1);
        }
        
        // Resize screen to width of image and 2*height of image
        newSize(imageWidth, imageHeight << 1);
        resize(imageWidth, imageHeight << 1);
        
        // Copy the image into the upper half of the screen
        System.arraycopy(image, 0, pixels, 0, image.length);
        newPixels(0, 0, imageWidth, imageHeight);
        repaint(0, 0, 0, imageWidth, imageHeight);
    }
    
    float a = 0;
    private static final float DISPLACEMENT = 10f;
    private static final float SPEED = 0.001f;
    
    public void loop()
    {
        // Start at the offset just below the static first half of the screen
        int i = image.length;
        
        // Traverse the scanlines upside-down
        for(int y = imageHeight-1; y >= 0; y--)
        {
            // Pick a scanline, using a sinewave...
            int j = (int)(DISPLACEMENT*sin(((float)y/(float)imageHeight)*(Math.PI*2))*cos(a));
            
            // Calculate the offset of that scanline in the pixelarray
            j *= imageWidth;
            j += y*imageWidth;
            
            // Clip the value to avoid OutOfBounds
            j = min(j, image.length-imageWidth);
            
            // Update the cyclic motion
            a += SPEED;
            
            // Draw 1 scanline
            System.arraycopy(image, j, pixels, i, imageWidth);
            i += imageWidth;
            j += imageWidth;
        }
        
        // Repaint the lower half of the screen
        newPixels(0, imageHeight, imageWidth, imageHeight);
        repaint(0, 0, imageHeight, imageWidth, imageHeight);
    }
}
