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   * Looped frames animation.
33   *
34   * @author  Michael Heuer
35   * @version $Revision$ $Date$
36   */
37  public final class LoopedFramesAnimation
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 looped 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 LoopedFramesAnimation(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      @Override
76      public boolean advance()
77      {
78          index++;
79          if (index == frames.size())
80          {
81              index = 0;
82          }
83          return true;
84      }
85  
86      @Override
87      public Image getCurrentFrame()
88      {
89          return frames.get(index);
90      }
91  }