/*
	A basic extension of the java.applet.Applet class
 */

import java.awt.*;
import java.applet.*;

public class SpriteApplet extends Applet implements Runnable
{
    private Button transparent_button;
    private Choice sprite_choices;
    private TextField Answer;
    Image crabimg,Bgndimg, pigimg, Animatedimg;
    Sprite sprite, pigsprite,crabsprite;
    PlayField playfield;
    Raster Bgnd;
    Image Output;
    boolean Check_transparency=false;
    int last_x, last_y;
    Thread animate;
	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		//setLayout(null);
		setSize(600,400);
		this.setBackground(Color.white);
		//}}
	    //Create a transparent button and add it to the applet 
	    //Set the button's colors
	    //Code from Java in a Nutshell pg 10-11
	    transparent_button=new Button("Transparent Mode");
	    transparent_button.setForeground(Color.black);
	    transparent_button.setBackground(Color.lightGray);
	    this.add(transparent_button);
	    
	    //Create a field to repond to whether a  
	    //pixel is transparent or not.
	    Answer=new TextField("", 10);
	    this.add(Answer);
	    
	    //Create a list of Sprites to go on Playfield 
	    //Code from Java in a Nutshell pg 10-11
	    sprite_choices= new Choice();
	    sprite_choices.addItem("Pig");
	    sprite_choices.addItem("Crab");
	    sprite_choices.addItem("SomethingElse");
	    sprite_choices.setForeground(Color.black);
	    sprite_choices.setBackground(Color.lightGray);
	    this.add(sprite_choices);
	    
	    //Images used in this project 
	    pigimg= Toolkit.getDefaultToolkit().getImage("d:\\pinkpig.gif");                        
	    crabimg= Toolkit.getDefaultToolkit().getImage("d:\\crab.gif");                      
	    //From Winamp player by Nullsoft
	    Bgndimg= Toolkit.getDefaultToolkit().getImage("d:\\cthugha.gif");                                               
	    //Sprites
	    pigsprite=new Sprite(pigimg);
	    crabsprite=new Sprite(crabimg);
	    //sprite to be displayed
	    sprite=pigsprite;
	    //Background of the playfield
	    Bgnd=new Raster(Bgndimg);
	    playfield=new PlayField(Bgnd,1);
	    playfield.add_sprite(sprite,0);
		renderPlayfield();
	}

	//{{DECLARE_CONTROLS
	//}}
	
    public void start() {
        animate = new Thread(this);
        animate.start();
    }  
    public void stop() {
        animate = null;
    }
             
    public void run() {
        while (true) {
            try {
                animate.sleep(1000000);
                } catch (InterruptedException e) {
                }
                renderPlayfield();
        }
    }
	
	public void renderPlayfield()
	{
	    playfield.Draw();
	    Output=playfield.toImage();
	    repaint();
	}
	public void paint(Graphics g)
	{
      g.drawImage(Output, 0, 50, this);
    }
    
    public void update(Graphics g)
    {
        paint(g);
    }

	public boolean mouseDown(Event e, int x, int y)
	{
		// to do: code goes here.
		if (Check_transparency==true){
		   if  (true==sprite.transparent(x,y-50))
		      Answer.setText("Yes");
		    else
		      Answer.setText("No");
		}
		else
		 sprite.set_location(x, y-50);
	
		//{{CONNECTION
		// Repaint the Applet
		renderPlayfield();
		//}}    
		return true;
	}
	public boolean action (Event event, Object arg)
	{
	    //If the transparent button was clicked, handle it
	    if (event.target==transparent_button){
	        if (Check_transparency==true)
	            Check_transparency=false;
	        else
	            Check_transparency=true;
	        return true;
	    }
	    else if (event.target==sprite_choices){
	        if (arg.equals("Pig")) sprite=pigsprite;
	        else if (arg.equals("Crab")) sprite=crabsprite;
	        else if (arg.equals("SomethingElse")) sprite=pigsprite;
	        playfield.add_sprite(sprite,0);
	        return true;
	    }
	    else return super.action(event, arg);
	}
}
