import java.applet.Applet;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import java.net.URL;
import java.net.MalformedURLException;

public class DemoApplet
extends      Applet
implements   MouseListener,
			 Runnable
{
	public  Playfield      playfield;
	private Image          frame_image;
	private AnimatedSprite body_animation;
	private Thread         update_thread;
	
    /**
     *  This constructor creates an Raster initialized
     *  with the contents of an image.
     */
    public DemoApplet()
    {
    }


    /**
     *  init
     */
    public void
	init()
    {
		Image background  = null;
		Image upper_torso = null;
		
		try {
			background = 
				getImage( getCodeBase(), "../images/background.gif" );
			upper_torso = 
				getImage( getCodeBase(), "../images/lower_torso.gif" );
			System.out.println( "Got the image..." );
		} catch ( Exception e ) {
			System.out.println( "Got exception in loading background..." );
			e.printStackTrace( System.out );
		}
		
		System.out.println( "Initialized the play_frame" );
		
		playfield = new Playfield( background, this );
		
		AnimationRule torso_animation_rule = new AnimationRule();
		
		torso_animation_rule.appendClip( new Rectangle ( 0, 0, 24, 29 ), 
										 200l);
		torso_animation_rule.appendClip( new Rectangle ( 24, 0, 24, 29 ),
										 200l );
		torso_animation_rule.appendClip( new Rectangle ( 49, 0, 24, 29 ),
										 200l );
		torso_animation_rule.appendClip( new Rectangle ( 72, 0, 17, 29 ),
										 200l );
		torso_animation_rule.appendClip( new Rectangle ( 88, 0, 24, 29 ),
										 200l );
		torso_animation_rule.appendClip( new Rectangle ( 112, 0, 24, 29 ),
										 200l );
		torso_animation_rule.appendClip( new Rectangle ( 137, 0, 24, 29 ),
										 200l );
		torso_animation_rule.appendClip( new Rectangle ( 160, 0, 24, 29 ),
										 200l );
		
		body_animation = 
			new AnimatedSprite( upper_torso, torso_animation_rule );
		
		playfield.addRaster( body_animation );
		
		addMouseListener( this );

		update_thread = new Thread( this, "UpdateThread" );
		update_thread.start();
	}
	
	
    /**
     *  destroy
     */
    public void
	destroy()
    {
	}
	
	
    /**
     *  stop
     */
    public void
	stop()
    {
	}
	
	
    /**
     *  start
     */
    public void
	start()
    {
    }


    /**
     *  Draw the sprite on the input raster.
     */
    public void
	update( Graphics g )
    {
    }


    /**
     *  Draw the sprite on the input raster.
     */
    public void
	paint( Graphics g )
    {
    }


    /**
     *  Draw the sprite on the input raster.
     */
    public void
	paintAll( Graphics g )
    {
		if ( frame_image != null ) {
			System.out.println( "Painting the image " );
			g.drawImage( frame_image, 0, 0, this );
			System.out.println( "Painted the image" );
		}
    }
	
	
    /**
    *
    * Handle mouse events
    *
    **/
    public void
    mouseReleased( MouseEvent e )
    {
	}
	

    
    /**
    *
    * Handle mouse events
    *
    **/
    public void
    mouseClicked( MouseEvent e ) 
    {
		System.out.println( "Preparing the playfield" );
		playfield.draw();
		System.out.println( "Getting the image from the playfield" );
		frame_image = playfield.toImage( this );
		getGraphics().drawImage( frame_image, 0, 0, this );
		body_animation.startAnimation();
		playfield.startAnimation();
    }
    

    /**
    *
    * Handle mouse events
    *
    **/
    public void
    mouseEntered( MouseEvent e ) 
    {
    }
    

    /**
    *
    * Handle mouse events
    *
    **/
    public void
    mouseExited( MouseEvent e ) 
    {
    }
    

    /**
    *
    * Handle mouse events
    *
    **/
    public void
    mousePressed( MouseEvent e ) 
    {
    }


    /**
     *  Run
     */
    public void
	run()
    {
		while ( true ) {
			try {
				update_thread.sleep( 1000l );
			} catch ( Exception e ) {
				System.out.println( "Update Thread exception in sleeping" );
				e.printStackTrace( System.out );
			}
		}
	}
}
