View Javadoc

1   package com.explosion.utilities.Graphics;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  /***
24   * @author Stephen Cowx
25   * @version 1.0
26   */
27  
28  import java.awt.Graphics;
29  import java.awt.Image;
30  import java.awt.Rectangle;
31  
32  public class AnimatedImage
33  {
34  
35      int xpos = 0;
36  
37      int ypos = 0;
38  
39      int size = 10;
40  
41      Image[] images;
42  
43      int numImages = 10;
44  
45      int currentImageIndex = 0;
46  
47      int imageHeight = 22;
48  
49      int imageWidth = 22;
50  
51      int screenWidth = 10;
52  
53      int screenHeight = 10;
54  
55      boolean finished = false;
56  
57      boolean moving = false;
58  
59      boolean loop = false;
60  
61      public AnimatedImage(int xpos, int ypos, Image[] images, int imageWidth, int imageHeight, int screenWidth, int screenHeight, boolean moving, boolean loop)
62      {
63          this.xpos = xpos;
64          this.ypos = ypos;
65          this.images = images;
66          this.imageWidth = imageWidth;
67          this.imageHeight = imageHeight;
68          this.numImages = images.length;
69          this.screenWidth = screenWidth;
70          this.screenHeight = screenHeight;
71          this.moving = moving;
72          this.loop = loop;
73          this.currentImageIndex = 0;
74      }
75  
76      public void draw(Graphics g)
77      {
78          if (!finished) g.drawImage(images[currentImageIndex], xpos, ypos, null);
79      }
80  
81      public void cycle()
82      {
83          if (loop)
84          {
85              if (currentImageIndex < numImages - 1)
86                  currentImageIndex++;
87              else
88                  currentImageIndex = 0;
89          } else
90          {
91              if (currentImageIndex < numImages - 1)
92                  currentImageIndex++;
93              else
94                  finished = true;
95          }
96      }
97  
98      public void move(int x, int y)
99      {
100         if (xpos + x > 0 && xpos + x < screenWidth)
101         {
102             xpos += x;
103         }
104 
105         if (ypos + y > 0 && ypos + y < screenHeight)
106         {
107             ypos += y;
108         }
109     }
110 
111     public int getXPos()
112     {
113         return xpos;
114     }
115 
116     public int getYPos()
117     {
118         return ypos;
119     }
120 
121     public Rectangle getBounds()
122     {
123         return new Rectangle(xpos, ypos, imageWidth, imageHeight);
124     }
125 
126     public boolean isFinished()
127     {
128         return finished;
129     }
130 
131     public boolean isMoving()
132     {
133         return moving;
134     }
135 }