/*
 SwingTest
 03/29/2004
 Short Description:
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingTest extends JFrame implements ActionListener,
                             			  		 MouseMotionListener, 
                             					 MouseListener,
                             					 KeyListener {
    
    JLabel ballLabel,
    	   faceLabel;
     
    String cursorImages[];
    Image cursorImage;
    Cursor cursor;
    Rectangle cursorR,
    		  faceR;
    		  
    int currentCursor=0;
    ImageIcon faceIcon;
    
    Timer timer;					//make the timer
	int fps = 60;					//how fast does the animation go
    int delay = (fps > 0) ? (1000 / fps) : 100;
    boolean frozen = false;
    
    Rock[] rockArray;
    String imageLocation = "rock.gif";
    Dimension rockSize = new Dimension(20,20);

	Container cp = getContentPane();
    
    //constructor
    public SwingTest() {
    	super("Happy!");
    	setSize(200,200);
    	cp.setBackground(Color.red);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    	addMouseListener(this);
    	addMouseMotionListener(this);
    	addKeyListener(this);
		//I want absolute positioning   	
    	cp.setLayout(null);		
    	
    	//Set up a timer that calls this object's actionPerformed handler.
        timer = new Timer(delay, this);
        timer.setInitialDelay(0);
        timer.setCoalesce(true);
   	
    	//put all the image urls in an array to access them easily
    	cursorImages = new String[5];
    	cursorImages[0] = "ball.gif";
		cursorImages[1] ="middle.gif";
		cursorImages[2] ="left.gif";
		cursorImages[3] ="right.gif";
		cursorImages[4] ="convert.gif";
		
		//this binds the image to the cursor
		cursorImage = Toolkit.getDefaultToolkit().getImage(cursorImages[currentCursor]);
		cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point( 0, 0), "" );
		setCursor(cursor); 

    	
		buildUI();
		
    		
    	show();
    	
    	startAnimation();
    }
    
    private void buildUI(){
    	faceIcon = new ImageIcon("face1.gif");
		
    	faceLabel = new JLabel(faceIcon);
    	faceLabel.setSize(faceIcon.getIconWidth(),faceIcon.getIconHeight());
		faceLabel.addMouseMotionListener(this);
		
		faceR = new Rectangle(faceLabel.getX(), faceLabel.getY(), faceLabel.getWidth(), faceLabel.getHeight());
		System.out.println(faceLabel.getX() +""+faceLabel.getY());
		cursorR = new Rectangle(faceLabel.getWidth(),faceLabel.getHeight() );
		    	
    	cp.add(faceLabel);
    	
    	buildGame();
    }
    
	void setMouseImage(){}
	
	void buildGame(){
		faceLabel.setLocation(10,10);
		rockArray = new Rock[5];
		for(int i = 0; i < 5; i++){
			rockArray[i] = new Rock(imageLocation, rockSize);
			rockArray[i].setBounds(20,20,10, 10+i);
	    	cp.add(rockArray[i]);
		}
	}
	
	void leave(){
		System.exit(0);
	}
    
    ////////////////////////////////////////////////
    //Listeners Code Begin                         /
    ////////////////////////////////////////////////
	
    public void mouseMoved(MouseEvent e) {
    	cursorR.setLocation(e.getX(), e.getY());
    	
    	if(cursorR.intersects(faceR)){
    		faceLabel.setIcon(new ImageIcon("face2.gif"));
    		System.out.println("OW!");
    	} else{
    		faceLabel.setIcon(faceIcon);
    	}
    }
    public void mouseExited(MouseEvent e) {}
    public void mouseClicked(MouseEvent e) {
    	System.out.println("hellop");
    	if(currentCursor < 4){
			currentCursor++;
			cursorImage = Toolkit.getDefaultToolkit().getImage(cursorImages[currentCursor]);
			cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point( 0, 0), "" );
			setCursor(cursor); 
		} else {
			currentCursor = 0;
			cursorImage = Toolkit.getDefaultToolkit().getImage(cursorImages[currentCursor]);
			cursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImage, new Point( 0, 0), "" );
			setCursor(cursor); 
		}		
    }
    public void mouseDragged(MouseEvent e) {
    	if(e.getSource() == faceLabel){
			faceLabel.setLocation(e.getX(), e.getY());
		}
    }	
    public void mouseReleased(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    
    //key events
	public void keyTyped(KeyEvent e) {
		System.out.println("yo");	
	}
    public void keyReleased(KeyEvent e) {
 		System.out.println("yo");	
    }
    public void keyPressed(KeyEvent e)
    {
    	int facex = faceLabel.getX();
    	int facey = faceLabel.getY();
    	
        switch(e.getKeyCode())
        {
            case KeyEvent.VK_UP   : facey-=5;
                                    break;
            case KeyEvent.VK_DOWN : facey+=5;
                                    break;
            case KeyEvent.VK_LEFT : facex-=5;
                                    break;
            case KeyEvent.VK_RIGHT: facex+=5;
                                    break;
        }
    	faceLabel.setLocation(facex, facey);
    	faceR.setLocation(facex, facey);
    }
	
	///////////////////////////////////////////////
	//here is the main game loop                  /
	///////////////////////////////////////////////
    public void actionPerformed(ActionEvent e) {
    	moveRocks();	
    }
    
	void moveRocks(){
		int oldX, oldY, newX, newY;
		
		//get the previous locations to work with
		oldX = rockArray[1].getX();
		oldY = rockArray[1].getY();
		
		//set the speed and direction of the ball
		newY = oldY - 5;
		newX = oldX + 5;
		
		
		//move the ball
		rockArray[1].setLocation(newX, newY);
	}    
    ////////////////////////////////////////////////
    //timer code                                   /
    ////////////////////////////////////////////////
    public void startAnimation() {
        if (frozen) {
        } else {
            //Start animating!
            if (!timer.isRunning()) {
                timer.start();
            }
        }
    }

    //Can be invoked by any thread (since timer is thread-safe).
    public void stopAnimation() {
        //Stop the animating thread.
        if (timer.isRunning()) {
            timer.stop();
        }
    }
    
  public static void main(String s[]) {
	SwingTest panel = new SwingTest();
  }
}

////////////////////////////////////////////////////
////////////////////////////////////////////////////
//Rock class////////////////////////////////////////
////////////////////////////////////////////////////
////////////////////////////////////////////////////
class Rock extends JLabel {
	private String imageLocation;
	private Dimension size;
	public Rectangle bounds;
	private int x, y;
	
	ImageIcon rockIcon;
	
	//////////////////////////////////////
	//Constructors						 /
	//////////////////////////////////////
	public Rock(String imageLocation, Dimension size){
		this.imageLocation = imageLocation;
		this.size = size;
		
		bounds = new Rectangle(size);
		rockIcon = new ImageIcon(imageLocation);
		
		setIcon(rockIcon);
	}
	
	//////////////////////////////////////
	//Public methods					 /
	//////////////////////////////////////
	public void setRockIcon(String imageLocation){
		this.imageLocation = imageLocation;
	}
	
	public void setRockSize(Dimension size){
		this.size = size;
	}
	
	public String toString(){
		return size + " " + rockIcon + " " + " " + imageLocation;
	}

	public void setRockLocation(int x, int y){
		setLocation(x, y);
	}
}
		