View Javadoc

1   /*
2   
3       dsh-piccolo-sprite  Piccolo2D sprite nodes and supporting classes.
4       Copyright (c) 2006-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.piccolo.sprite;
25  
26  import java.awt.Image;
27  
28  import java.util.List;
29  import java.util.ArrayList;
30  
31  /**
32   * Multiple frames animation.
33   *
34   * @author  Michael Heuer
35   * @version $Revision$ $Date$
36   */
37  public final class MultipleFramesAnimation
38      implements Animation
39  {
40      /** Index to current frame. */
41      private int index;
42  
43      /** List of frames. */
44      private List<Image> frames;
45  
46  
47      /**
48       * Create a new multiple frames animation with the specified list of frames.
49       *
50       * <p>
51       * The specified list of frames must contain at least one frame.
52       * The frames in <code>frames</code> are copied defensively into this class.
53       * </p>
54       *
55       * @param frames list of frames, must not be null and must
56       *    contain at least one frame
57       *
58       * @throws IllegalArgumentException if <code>frames.size() &lt; 1</code>
59       */
60      public MultipleFramesAnimation(final List<Image> frames)
61      {
62          if (frames == null)
63          {
64              throw new IllegalArgumentException("frames must not be null");
65          }
66          if (frames.size() < 1)
67          {
68              throw new IllegalArgumentException("frames must contain at least one frame");
69          }
70          index = 0;
71          this.frames = new ArrayList<Image>(frames);
72      }
73  
74  
75      /**
76       * Reset this multiple frames animation.
77       */
78      public void reset()
79      {
80          index = 0;
81      }
82  
83      @Override
84      public boolean advance()
85      {
86          index = Math.min(index + 1, frames.size() - 1);
87          return true;
88      }
89  
90      @Override
91      public Image getCurrentFrame()
92      {
93          return frames.get(index);
94      }
95  }