/*
 * Test class to practice mouse movement by drawing a circle where the cursor is.
 *
 * @author Cyrus Barker
 * @date 2013-11-15
 */

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;

public class HWJ14_BallDrop extends JApplet implements MouseListener, MouseMotionListener,ActionListener,KeyListener,Runnable
{

    BufferedImage oS=new BufferedImage(2000,2000,BufferedImage.TYPE_INT_RGB);
    int frame;
    int delay=20;

    int ballWidth=40;
    int x=120;
    int y=120;
    //velocity
    double vx=0;
    double ovy=5;
    double vy=5;
    //acceleration due to gravity
    double ag=1;

    Color ballColor=Color.black;

    public int findRandom (int start, int end)
    {
        int multiplier = end-start+1;
        int random = (int) (Math.random()*multiplier)+start;
        return random;
    }

    public double findRandomDouble(int start, int end) {
        double multiplier = end-start+1;
        double random = (Math.random()*multiplier)+start;
        return random;
    }

    public Color findRandomColor ()
    {
        int r= (int)(256 * Math.random());
        int g= (int)(256 * Math.random());
        int b= (int)(256 * Math.random());
        Color randomColor=new Color(r,g,b);
        return randomColor;
    }

    public void init()
    {

        addMouseListener(this);
        addMouseMotionListener(this);
        addKeyListener(this);
        setFocusable(true);

        Container screen = getContentPane();
        screen.setBackground(Color.white);
        screen.setLayout (new FlowLayout() );

    }

    public void paint (Graphics g)
    {
        super.paint(g);

    }

    public void reset() {
        //debugOut("reset");
        vy=findRandom(0,8);
        vx=findRandom(-10,10);
        x=findRandom(20,400);
        y=120;
    }

    public void bounce() {
        if (y>=500-ballWidth-vy) {
            vy=-vy;
            ballColor=findRandomColor();

        }
        if (x<=0 || x>=500-ballWidth) {
            vx=-vx;
            ballColor=findRandomColor();
        }
        if (y+ballWidth>500) {
            reset();
        }
    }

    public void move() {
        vy=vy+ag;
        y=(int)(y+vy);
        x=(int)(x+vx);
    }

    //This is the method that will be called if a key is pressed
    //It is neccessary if you are using getKeyCode()
    public void keyPressed(KeyEvent e)
    {
        char theChar=e.getKeyChar();
        int theCode=e.getKeyCode();
    }

    //This is the method that will be called if a key is released
    //It is unneccessary to use this method, but you must include it
    public void keyReleased(KeyEvent e)
    { 
    }

    //This is the method that will be called if a key is typed, however you cant use getKeyCode, so
    // you are better to use keyPressed
    public void keyTyped(KeyEvent e)
    {   
    }

    //when someone presses the mouse button
    public void mousePressed(MouseEvent e)
    {    
        int x=e.getX();
        int y=e.getY();
    }

    //when someone releases the mouse button
    public void mouseReleased(MouseEvent e)
    {    

    }

    // when the mouse enters the applet
    public void mouseEntered(MouseEvent e)
    {    

    }

    //when the mouse leaves the applet
    public void mouseExited(MouseEvent e)
    {    

    }

    //when the mouse button is clicked
    public void mouseClicked(MouseEvent e)
    {
        requestFocus(); //this bring the focus make to the main progra

    }

    //the mouse button is pressed and the mouse makes a significantly large movement
    public void mouseDragged(MouseEvent e)
    {

    }

    //the mouse makes a significantly large movement
    public void mouseMoved(MouseEvent e)
    {
    }

    /*********************************************************************************************/
    /* BELOW IS FOR ANIMATION.  THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY */
    // this is the time of the delay in milliseconds.
    Thread animator;

    //Update (the method below) gets called every frame
    public void update()
    {
        Graphics oSG=oS.getGraphics();
        oSG.setColor(Color.white);
        oSG.fillRect(0,40,2000,2000);
        oSG.setColor(ballColor);
        bounce();
        oSG.fillOval(x,y,ballWidth,ballWidth);
        move();
        Graphics g = getGraphics();
        g.drawImage(oS,0,0,this);

        super.paint(oSG);
    }

    /**
     * This method is called when the applet becomes visible on
     * the screen. Create a thread and start it.
     */
    public void start()
    {
        animator = new Thread(this);
        animator.start();
    }

    /**
     * This method is called by the thread that was created in
     * the start method. It does the main animation.
     */
    public void run()
    {
        // Remember the starting time
        long tm = System.currentTimeMillis();
        while (Thread.currentThread() == animator)
        {

            update();
            try
            {
                tm += delay;
                Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
            }
            catch (InterruptedException e)
            {
                break;
            }
            // Advance the frame
            frame++;
        }
    }

    /**
     * This method is called when the applet is no longer
     * visible. Set the animator variable to null so that the
     * thread will exit before displaying the next frame.
     */
    public void stop()
    {
        animator = null;
    }

    public void actionPerformed(ActionEvent thisEvent)
    {
        Object source = thisEvent.getSource();
    }
}