import java.math.*;
import java.awt.*;

/**
 * Example3Applet
 *
 * This is a template applet for animation.
 * It illustrates how to use a graphics context
 * to draw to the screen.
 *
 * @author Arthur van Hoff
 */

//I have added double buffering to this in order to make it look smoother


public class my_animation extends java.applet.Applet implements Runnable {
    //The object we will use to write with instead of standard screen graphics

    Graphics bufferGraphics;
    Image offscreen;

    int b;
    int frame;
    int delay;
    Thread animator;
    Dimension dim;

    double[] sine_values=new double[50];
    /**
     * Initialize the applet and compute the delay between frames.
     */
    public void init() {
	String str = getParameter("fps");
	int fps = (str != null) ? Integer.parseInt(str) : 10;
	delay = (fps > 0) ? (1000 / fps) : 100;
	
	//This will return the width/height in the object variables dim.width
	//dim.height

	dim=getSize();    	
	offscreen=createImage(dim.width,dim.height);
	bufferGraphics=offscreen.getGraphics();

	}

    /**
     * This method is called when the applet becomes visible on
     * the screen. Create a thread and start it.
     */
    public void start() {
	animator = new Thread(this);
	animator.start();
    }

    /**
     * This method is called by the thread that was created in
     * the start method. It does the main animation.
     */
    public void run() {
	// Remember the starting time
	long tm = System.currentTimeMillis();
	while (Thread.currentThread() == animator) {
	    // Display the next frame of animation.
	    repaint();

	    // Delay depending on how far we are behind.
	    try {
		tm += delay;
		Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
	    } catch (InterruptedException e) {
		break;
	    }

	    // Advance the frame
	    frame++;
	    b=Math.sin(frame);
	}
    }

    /**
     * This method is called when the applet is no longer
     * visible. Set the animator variable to null so that the
     * thread will exit before displaying the next frame.
     */
    public void stop() {
	animator = null;
    }

    /**
     * Paint a frame of animation.
     */
    public void paint(Graphics g) {
	bufferGraphics.clearRect(0,0,dim.width,dim.width);
	bufferGraphics.setColor(Color.red);
	bufferGraphics.drawRect(frame,frame,frame,frame);  
	
	g.drawImage(offscreen,0,0,this);
	g.drawString("hello",10,10);
	}
    
    public void update(Graphics g)
	{
	paint(g);
	}

}


