Advertisement
Guest User

Animation

a guest
Apr 14th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5.  
  6. public class Animation {
  7.  
  8.     private int frameCount;                 // Counts ticks for change
  9.     private int frameDelay;                 // frame delay 1-12 (You will have to play around with this)
  10.     private int currentFrame;               // animations current frame
  11.     private int animationDirection;         // animation direction (i.e counting forward or backward)
  12.     private int totalFrames;                // total amount of frames for your animation
  13.     private boolean loop;                   // loop this?
  14.  
  15.     private boolean stopped;                // has animations stopped
  16.  
  17.     private List<Frame> frames = new ArrayList<Frame>();    // Arraylist of frames
  18.  
  19.     public Animation(BufferedImage[] frames, int frameDelay, boolean shouldLoop) {
  20.         this.frameDelay = frameDelay;
  21.         this.stopped = true;
  22.         this.loop = shouldLoop;
  23.  
  24.         for (int i = 0; i < frames.length; i++) {
  25.             addFrame(frames[i], frameDelay);
  26.         }
  27.  
  28.         this.frameCount = 0;
  29.         this.frameDelay = frameDelay;
  30.         this.currentFrame = 0;
  31.         this.animationDirection = 1;
  32.         this.totalFrames = this.frames.size();
  33.  
  34.     }
  35.  
  36.     public void start() {
  37.         if (!stopped) {
  38.             return;
  39.         }
  40.  
  41.         if (frames.size() == 0) {
  42.             return;
  43.         }
  44.  
  45.         stopped = false;
  46.     }
  47.  
  48.     public void stop() {
  49.         if (frames.size() == 0) {
  50.             return;
  51.         }
  52.  
  53.         stopped = true;
  54.     }
  55.  
  56.     public void restart() {
  57.         if (frames.size() == 0) {
  58.             return;
  59.         }
  60.  
  61.         stopped = false;
  62.         currentFrame = 0;
  63.     }
  64.  
  65.     public void reset() {
  66.         this.stopped = true;
  67.         this.frameCount = 0;
  68.         this.currentFrame = 0;
  69.     }
  70.  
  71.     private void addFrame(BufferedImage frame, int duration) {
  72.         if (duration <= 0) {
  73.             System.err.println("Invalid duration: " + duration);
  74.             throw new RuntimeException("Invalid duration: " + duration);
  75.         }
  76.  
  77.         frames.add(new Frame(frame, duration));
  78.         currentFrame = 0;
  79.     }
  80.  
  81.     public BufferedImage getSprite() {
  82.         return frames.get(currentFrame).getFrame();
  83.     }
  84.  
  85.     public void update() {
  86.         if (!stopped) {
  87.             frameCount++;
  88.  
  89.             if (frameCount > frameDelay) {
  90.                 frameCount = 0;
  91.                 currentFrame += animationDirection;
  92.  
  93.                 if (currentFrame > totalFrames - 1) {
  94.                     currentFrame = 0;
  95.                     if(!loop){
  96.                        stopped = true;
  97.                     }
  98.                 }
  99.                 else if (currentFrame < 0) {
  100.                     currentFrame = totalFrames - 1;
  101.                 }
  102.             }
  103.         }
  104.  
  105.     }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement