/*
 * PlotAnim.java
 * Code: Jan Humble
 */

import java.applet.*;
import java.awt.*;
import PlotPackage.*;

public class PlotAnim extends Applet implements Runnable{

    int dY, dX;
    int plotfunction;    // chosen plot
    String color_str;    // chosen color
    Color color;        
    
    Plot4 plot;
    Thread drawThread;
    int time = 0;

    Image offscreen;     // background graphics buffer

    public void init(){
	// Get the drawing area of the applet 
	dY=getSize().height; dX=getSize().width;
	
	// plotfunction = Integer.parseInt(getParameter("plotfunction"), 10);
	
	color_str = getParameter("color");
	
       

	if (color_str.equals("yellow"))
	    color = Color.yellow;
	else if (color_str.equals("green"))
	    color = Color.green;
       	else if (color_str.equals("black"))
	    color = Color.black;
	// Default color
	else 
	    color = Color.blue;
       
	plot = new Plot4(dX, dY, color);
  
    }
    
  
    
    public void start() { 
	if (drawThread == null){ 
	    drawThread = new Thread(this); 
	    drawThread.start(); 
	} 
    } 

   
    public void stop() { 
	drawThread = null; 
    } 
   

    public void run() { 
	
	while (drawThread != null) { 

	  
	    
	    // Use sleep to pause between movements 
	    try { 
		
		Thread.sleep((int)(100)); 
		
	    } catch (InterruptedException e) {}
	    
	    plot.move(time); 
	    repaint(); 
	    time++;
	} 
    } 
    

   
    public void update (Graphics g){
	
	// Update using double buffering

	  Dimension d = this.getSize();

	    if (offscreen == null)
		offscreen = this.createImage(d.width, d.height);
      		
	    // Draw to background buffer
	    g = offscreen.getGraphics();
	    paint(g);
	    
	    // draw from background buffer to screen buffer
	    g = this.getGraphics();
	    g.drawImage(offscreen, 0, 0, this);
	    
      
    }
    

    public void paint(Graphics g) {

	// Clear screen
	g.setColor(Color.white);
	g.fillRect(0, 0, dX, dY);

	// Draw plot and plot info
	plot.draw(g);
	plot.frameInfo(time, g);
	
    }
}






