Advertisement
tsinclai2002

Simple Animation (Adapted from Java SE 6 Game Programming)

Feb 28th, 2014
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. /*****************************************************
  2. * Beginning Java Game Programming, 3rd Edition
  3. * by Jonathan S. Harbour
  4. * AnimationTest program
  5. *****************************************************/
  6. import java.awt.*;
  7. import javax.swing.*;
  8. import java.util.*;
  9. import java.awt.image.*;
  10. import java.net.*;
  11.  
  12. public class AnimationTest extends JFrame implements Runnable {
  13.     static int ScreenWidth = 640;
  14.     static int ScreenHeight = 480;
  15.     Thread gameloop;
  16.     Random rand = new Random();
  17.  
  18.     //double buffer objects
  19.     BufferedImage backbuffer;
  20.     Graphics2D g2d;
  21.  
  22.     //sprite variables
  23.     Image image;
  24.     Point pos = new Point(300,200);
  25.  
  26.     //animation variables
  27.     int currentFrame = 0;
  28.     int totalFrames = 30;
  29.     int animationDirection = 1;
  30.     int frameCount = 0;
  31.     int frameDelay = 10;
  32.    
  33.     public static void main(String[] args) {
  34.         new AnimationTest();
  35.     }
  36.    
  37.     public AnimationTest() {
  38.         super("Animation Test");
  39.         setSize(ScreenWidth,ScreenHeight);
  40.         setVisible(true);
  41.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.  
  43.         //create the back buffer for smooth graphics
  44.         backbuffer = new BufferedImage(ScreenWidth, ScreenHeight,
  45.             BufferedImage.TYPE_INT_RGB);
  46.         g2d = backbuffer.createGraphics();
  47.  
  48.         //load the ball animation strip
  49.         Toolkit tk = Toolkit.getDefaultToolkit();
  50.         image = tk.getImage(getURL("hello2world_strip_sm.png"));
  51.  
  52.         gameloop = new Thread(this);
  53.         gameloop.start();
  54.     }
  55.  
  56.     private URL getURL(String filename) {
  57.         URL url = null;
  58.         try {
  59.             url = this.getClass().getResource(filename);
  60.         }
  61.         catch (Exception e) {}
  62.         return url;
  63.     }
  64.  
  65.     public void run() {
  66.         Thread t = Thread.currentThread();
  67.         while (t == gameloop) {
  68.             try {
  69.                 Thread.sleep(5);
  70.             }
  71.             catch (InterruptedException e) {
  72.                 e.printStackTrace();
  73.             }
  74.             gameUpdate();
  75.         }
  76.     }
  77.  
  78.     public void gameUpdate() {
  79.         //clear the background
  80.         g2d.setColor(Color.BLACK);
  81.         g2d.fill( new Rectangle(0, 0, ScreenWidth-1, ScreenHeight-1) );
  82.  
  83.         //draw the current frame of animation
  84.         drawFrame(image, g2d, pos.x, pos.y, 6, currentFrame, 128, 128);
  85.  
  86.         g2d.setColor(Color.WHITE);
  87.         g2d.drawString("Position: " + pos.x + "," + pos.y, 10, 50);
  88.         g2d.drawString("Animation: " + currentFrame, 10, 70);
  89.  
  90.         //see if it's time to animate
  91.         frameCount++;
  92.         if (frameCount > frameDelay) {
  93.             frameCount=0;
  94.             //update the animation frame
  95.             currentFrame += animationDirection;
  96.             if (currentFrame > totalFrames - 1) {
  97.                 currentFrame = 0;
  98.                 pos.x = rand.nextInt(ScreenWidth-128);
  99.                 pos.y = rand.nextInt(ScreenHeight-128);
  100.             }
  101.             else if (currentFrame < 0) {
  102.                 currentFrame = totalFrames - 1;
  103.             }
  104.         }
  105.  
  106.         repaint();
  107.     }
  108.  
  109.     public void paint(Graphics g) {
  110.         //draw the back buffer to the screen
  111.         g.drawImage(backbuffer, 0, 0, this);
  112.     }
  113.  
  114.     //draw a single frame of animation
  115.     public void drawFrame(Image source, Graphics2D dest,
  116.                           int x, int y, int cols, int frame,
  117.                           int width, int height)
  118.     {
  119.         int fx = (frame % cols) * width;
  120.         int fy = (frame / cols) * height;
  121.         dest.drawImage(source, x, y, x+width, y+height,
  122.                        fx, fy, fx+width, fy+height, this);
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement