/**
 * 
 * You can add code anywhere in this file - except in the areas that are
 * written by the editor. You should not change the relative ordering of
 * the code.
 * 
 * You can remove this comment block or replace it with another.
 * 
 * @see		
 * @version	
 * @author	
 */

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.Thread.*;

public class Pong extends Applet implements Serializable, Runnable, 
											MouseListener, ActionListener
{
    Playfield pongcourt;
    AnimatedSprite ball;
    AnimatedSprite gorilla;
    int gorilla_mov;		//-1 or 1 for left or right, or 0
    int ball_x_mov;			//-1 or 1 for left or right, or 0
    int ball_y_mov;			//-1 or 1 for up or down, or 0
    int WALK_DISTANCE;
    
    Image output;
    Thread animate;
    
	void pongPreInit()
	{
		// You can add code anywhere in this method.
		// This method is called PRIOR to form initialization.
		// NOTE: The form has NOT been initialized yet. Do not modify the form itself in this method.
	}

	void pongPostInit()
	{
		// You can add code anywhere in this method.
		// This method is called AFTER form initialization.
	}

	public void init()
	{
		Button start = new Button("Start");
		add(start);
		
		addMouseListener(this);
		start.addActionListener(this);


		Image iball = getImage(getDocumentBase(), "WrldAni.gif");
		Image gor = getImage(getDocumentBase(), "MonkAni.gif");
		Image court = getImage(getDocumentBase(), "pongCourt.gif");

		MediaTracker mt = new MediaTracker(this);
        mt.addImage(iball,1);
        mt.addImage(gor,2);
        mt.addImage(court,3);
        try {
		  mt.waitForAll();
		}
		catch (InterruptedException e) { }
		
		WALK_DISTANCE = 3;
		Color transColor;
//		transColor = new Color(192,192,192);
		
		ball = new AnimatedSprite(iball, 12);
//		ball.setTransColor(transColor.getRGB());
		ball.setTransColor(0xC0C0C0);
		ball.addState(0, 0, 0, 0);
		ball.addState(0, 1, 0, 0);
		ball.addState(0, 2, 0, 0);
		ball.addState(0, 3, 0, 0);
		ball.addState(0, 4, 0, 0);
		ball.addState(0, 5, 0, 0);
		ball.addState(0, 6, 0, 0);
		ball.addState(0, 7, 0, 0);
		ball.addState(0, 8, 0, 0);
		ball.addState(0, 9, 0, 0);
		ball.addState(0, 10, 0, 0);
		ball.addState(0, 11, 0, 0);
	
		gorilla = new AnimatedSprite(gor, 7);
//		gorilla.setTransColor(transColor.getRGB());
		gorilla.setTransColor(0xC0C0C0);
		gorilla.addState(0, 0, WALK_DISTANCE, 0);
		gorilla.addState(0, 1, WALK_DISTANCE, 0);
		gorilla.addState(0, 2, WALK_DISTANCE, 0);
		gorilla.addState(0, 3, WALK_DISTANCE, 0);
		gorilla.addState(0, 4, WALK_DISTANCE, 0);
		gorilla.addState(0, 3, WALK_DISTANCE, 0);
		gorilla.addState(0, 2, WALK_DISTANCE, 0);
		gorilla.addState(0, 1, WALK_DISTANCE, 0);
		
		pongcourt = new Playfield(court, 2);
		
		pongcourt.addSprite(0, gorilla);
		pongcourt.addSprite(1, ball);
		
		gorilla.setX((pongcourt.getWidth() / 2));
		gorilla.setY(0);
		
    	gorilla_mov = 0;
    	ball_x_mov = 0;
	    ball_y_mov = 0;
		
		renderPlayfield();
		
	}

	public void actionPerformed(ActionEvent e) {
	  if (e.getActionCommand().equals("Start")) {
	    start_buttonMousePressed();
	  }
	  
	}
	
    public void mouseClicked(MouseEvent e) { 
      pongMouseClicked(e.getX());
    }
    
    public void mouseEntered(MouseEvent e) { }
    
    public void mouseExited(MouseEvent e) { }
    
    public void mousePressed(MouseEvent e) { }

    public void mouseReleased(MouseEvent e) { }
   	
	public void start()
	{
		animate = new Thread(this);
        animate.start();

	}

	public void stop()
	{
	}
	
	public void run()
	{
		while (true) {
        	try {
            	animate.sleep(100);
            } catch (InterruptedException e) {}
        	renderPlayfield( );
        }
    }
    
    public void renderPlayfield() {
    	pongcourt.Draw();
        output = pongcourt.toImage();
        if (gorilla_mov != 0) {
            if (gorilla_mov > 0) {
        		gorilla.nextState();
        	} else {
        		gorilla.prevState();
        	}
        }
        ball.nextState();
        ball.setX(ball.getX() + ball_x_mov);
        ball.setY(ball.getY() + ball_y_mov);
        if ((ball.getX() + ball.frameWidth) > (pongcourt.getWidth() - 5)) {
          if (ball_x_mov > 0) {
            ball_x_mov = -1 * ball_x_mov;
          }
        }
        if (ball.getX() < 5) {
          if (ball_x_mov < 0) {
            ball_x_mov = -1 * ball_x_mov;
          }
        }
        if ((ball.getY() + ball.getHeight()) > (pongcourt.getHeight() - 5)) {
          if (ball_y_mov > 0) {
            ball_y_mov = -1 * ball_y_mov;
          }
        }
        if (ball.getY() < (gorilla.getHeight())) {
          if (ball.getX() < (gorilla.getX() + gorilla.frameWidth)) {
            if ((ball.getX() + ball.frameWidth) > gorilla.getX()) {
              if (ball_y_mov < 0) {
                ball_y_mov = -1 * ball_y_mov;
              }
            }
          }
          if (ball_y_mov < 0) {
            ball.setX(0);
            ball.setY(gorilla.getHeight() + 5);
            ball_y_mov = 0;
            ball_x_mov = 0;
          }
        }
        repaint();
    }
              
    public void paint(Graphics g) {
    	g.drawImage(output, 0, 0, this);
    }
    
    public void update(Graphics g) {
    	paint(g);
    }     	

	public void start_buttonMousePressed()
	{
    	ball_x_mov = 6;
    	ball_y_mov = 6;
    	renderPlayfield();
	}

	public void pongMouseClicked(int x)
	{
		if (x < gorilla.getX()) {
			gorilla_mov = -1;
		}
		else {
		  if (x > (gorilla.getX() + gorilla.frameWidth)) {
			  gorilla_mov = 1;
		  }
		  else {
		  	  gorilla_mov = 0;
		  }
		}
	}


	public Pong() throws IOException, ClassNotFoundException, ClassCastException
	{
		super();

		pongPreInit();
		pongPostInit();
	}
}

