package shootOut;

 

/*

 * This source is taken/modified from an earlier project: AnimatedSprite.java

 * This shows a sprite sheet output

 * @author  JasonButka

 * The objective of this class is to get the sprite sheet to

 * be animated and have a background image

 *

 */

 

import java.awt.*;

 

import java.applet.*;

 

 

import java.net.*;

 

 

public class Game2 extends Applet  implements Runnable {

  

                static int SCREENWIDTH = 550;

    static int SCREENHEIGHT = 500;

   

    Thread animationLoop;

   

   

    //double buffer objects

    Image backbuffer;

  

    //sprite variables

    Image sprite;

   

    int spriteX = 100, spriteY = 100;

   

    //animation variables

    int currentFrame;

    int totalFrames = 8;

    int animationDirection = 1;

    int frameCount = 0;

    int frameDelay = 1;

  

    int keyFrame;   // the beginning of the current loop

  

    String info;  // help clarify Animation numbers

   

    Image dbImage; // double buffering technique

               

    Graphics dbg; //  double buffering technique

 

    // find & load image

                private int fx;

                private int fy;

   

  

               

                /**

                 * This method checks for key down events and

                 * returns a boolean (true or false)if the given key

                 * is down (true) or is not down (false)

                 */

                public boolean keyDown (Event e, int key)

{

   

   

// user presses right cursor key

    if (key == Event.RIGHT)

    {

        keyFrame = 0;

        currentFrame++;

        gameUpdate(0);

             

    }

 

     

    // DON'T FORGET to return a boolean!

        return true;

    }

   

   

               

                /**

                 * This method is used to locate

                 * a given file name (picture in our case)

                 *

                 */

    private URL getURL(String filename)

    {

        URL url = null;

       

        try

        {

            url = this.getClass().getResource(filename);

        }

        catch (Exception e)

        {

                   // do nothing

        }

        return url;

    }

 

    /**

     * The init method sets up the Applet variables

     * at their start point for running

     */

   

    public void init()

    {

        this.setSize(SCREENWIDTH, SCREENHEIGHT);

         Toolkit tk = Toolkit.getDefaultToolkit();

 

      

        backbuffer= tk.getImage(getURL("HockeyRink.png"));

        //load the animation strip

        sprite = tk.getImage(getURL("shot.gif"));

 

     

    }

 

    /**

     * This method is used to start the thread

     */

    public void start()

    {

        animationLoop = new Thread(this);

        animationLoop.start();

    }

 

    /**

     * can be used to kill/stop the thread

     */

    public void stop()

    {

        animationLoop = null;

    }

 

    /**

     * Runs thread (what happens after it is started)

     */

    public void run()

    {

       

        Thread t = Thread.currentThread();

        while (t == animationLoop) {

            try {

                Thread.sleep(5);

            }

            catch (InterruptedException e) {

                e.printStackTrace();

            }

          

            repaint();

           

        }

    }

     

 

   

    public void gameUpdate(int x)

    {

        //see if it's time to animate

       

        frameCount++;

        // so the animation does not have to run to find the desired set

        if (currentFrame < keyFrame-1)

        {

            currentFrame = keyFrame;

        }

       

       

       

            if (currentFrame > totalFrames-1 ) //keyFrame +4

            {

                currentFrame = keyFrame;

            }

           

            else if (currentFrame < 1)

            {

                currentFrame = keyFrame;

                       

            }

 

       

 

 }

   

   

  

   

     

     

 

    public void update(Graphics g)

    {

       

          if (dbImage == null)

                                 {

                                                 // this size of the background

                                       dbImage = createImage (SCREENWIDTH, SCREENHEIGHT);

                                       dbg = dbImage.getGraphics ();

 

                                 }

 

                                 // clear screen in background

                                 dbg.setColor (Color.white);

                                 dbg.fillRect (0, 0, SCREENWIDTH, SCREENHEIGHT);

 

                                 // draw elements in background

                                 dbg.setColor (getForeground());

                                 paint (dbg);

 

                                 // draw image on the screen

                                g.drawImage (dbImage, 0, 0, this);

        

        

        

       

      

      

    }

 

 

   

    public void paint(Graphics g)

    {

        //draw the back buffer to the screen

        g.drawImage(backbuffer, 0, 0, this);

         Graphics2D g2d=(Graphics2D) g;

          //draw the current frame of animation

        drawFrame(sprite, g2d, spriteX, spriteY, 8, currentFrame, 20, 20);

       

       

        //I WANT TO PRINT INFO ON THE SCREEN

        //THIS HELPS ME PROBLEM SOLVE (to see variables)

        g2d.setColor(Color.black);

        g2d.drawString("Position: " + spriteX + "," + spriteY, 5, 150);

      

        g2d.drawString("Animation Frame: " + currentFrame, 5, 240);

        //Draw the appletInfo() X and Y

        g.drawString(info, 5, 285);

     

    

     

      

       

    }

 

   

    /**

     * Draws a single frame of information and

     * prints out HOW the math/algorithm advances

     * through the sprite (appletInfo method)

     * @param source

     * @param dest

     * @param x

     * @param y

     * @param cols

     * @param frame

     * @param width

     * @param height

     */

    public void drawFrame(Image source, Graphics2D dest, int x, int y, int cols, int frame,int width, int height)

    {

         fx = 0;

         fy=0;

        fx = (frame % cols) * width;

        fy = (frame / cols) * height;

        dest.drawImage(source, x, y, x+width, y+height,fx, fy, fx+width, fy+height, this);

        appletInfo(x, y, x+width, y+height,fx, fy, fx+width, fy+height);

       

        /**

         *     img - the specified image to be drawn. This method does nothing if img is null.

                                         dx1 - the x coordinate of the first corner of the destination rectangle.

                                         dy1 - the y coordinate of the first corner of the destination rectangle.

                                         dx2 - the x coordinate of the second corner of the destination rectangle.

                                         dy2 - the y coordinate of the second corner of the destination rectangle.

                                         sx1 - the x coordinate of the first corner of the source rectangle.

                                         sy1 - the y coordinate of the first corner of the source rectangle.

                                         sx2 - the x coordinate of the second corner of the source rectangle.

                                         sy2 - the y coordinate of the second corner of the source rectangle.

                                         observer - object to be notified as more of the image is scaled and converted.

         */

       

    

    }

                

    

// Just used to print info about the drawFrame method (help to understand it a bit more).

    public String appletInfo(int a, int b, int c, int d, int e, int f, int g, int h)

    {

         info = "X "+a+" Y "+b+" C "+c+" D "+d+" Current column X "+e+" Current column Y "+f+" Right side "+g+" bottom side "+h;

 

               

         return info;

 

    }

 

 

 

}