DEADSOFTWARE

License project under GNU GPL 3.
[LongFlight.git] / src / code / kalter / longflight / Animation.java
1 package code.kalter.longflight;
3 import java.io.IOException;
4 import javax.microedition.lcdui.Graphics;
5 import javax.microedition.lcdui.Image;
7 /**
8 * Реализация анимации: покадровая смена кадра из массива картинок за
9 * определённое время. Можно запустить как конечную анимацию, так и бесконечную
10 *
11 * @author KalterFive
12 */
13 public class Animation {
15 public static Animation createAnimation(String path, int width,
16 int deltaTime) throws IOException {
17 final Loader loader = Loader.getInstance();
18 final Image image = loader.getImage(path);
19 return createAnimation(image, width, deltaTime);
20 }
22 public static Animation createAnimation(String path, int width, int height,
23 int deltaTime) throws IOException {
24 final Loader loader = Loader.getInstance();
25 final Image image = loader.getImage(path);
26 return createAnimation(image, width, height, deltaTime);
27 }
29 public static Animation createAnimation(Image source, int width,
30 int deltaTime) {
31 final int height = source.getHeight();
32 return createAnimation(source, width, height, deltaTime);
33 }
35 public static Animation createAnimation(Image source, int width, int height,
36 int deltaTime) {
37 final Image[] still = new Image[source.getWidth() / width];
38 for (int i = 0; i < still.length; i++) {
39 still[i] = Image.createImage(source, i * width, 0,
40 width, height, 0);
41 }
42 return createAnimation(still, width, height, deltaTime);
43 }
45 public static Animation createAnimation(Image[] still, int deltaTime) {
46 int width = still[0].getWidth();
47 int height = still[0].getHeight();
48 for (int i = 1; i < still.length; i++) {
49 if (still[i].getWidth() > width) {
50 width = still[i].getWidth();
51 }
52 if (still[i].getHeight() > height) {
53 height = still[i].getHeight();
54 }
55 }
56 return createAnimation(still, width, height, deltaTime);
57 }
59 public static Animation createAnimation(Image[] still,
60 int width, int height, int deltaTime) {
61 return new Animation(still, width, height, deltaTime);
62 }
64 private final Image[] still; // кадры
65 private final int width;
66 private final int height;
67 //=======================
68 private long lastTime; // необходимо для засекания времени
69 private int deltaTime; // скорость смены кадров
70 //=======================
71 private int position;
72 private boolean animationLoop;
74 private Animation(Image[] still, int width, int height, int deltaTime) {
75 this.still = still;
76 this.width = width;
77 this.height = height;
78 this.deltaTime = deltaTime;
79 lastTime = System.currentTimeMillis();
80 position = 0;
81 animationLoop = true;
82 }
84 public Image getStill(int index) {
85 if (index < 0) {
86 throw new IllegalArgumentException("index < 0");
87 }
88 if (index >= still.length) {
89 throw new ArrayIndexOutOfBoundsException("i >= length");
90 }
91 return still[index];
92 }
94 public int getWidth() {
95 return width;
96 }
98 public int getHeight() {
99 return height;
102 public int getDeltaTime() {
103 return deltaTime;
106 public void setDeltaTime(int deltaTime) {
107 if (deltaTime < 0) {
108 throw new IllegalArgumentException("delta time < 0");
110 this.deltaTime = deltaTime;
113 public int getPosition() {
114 return position;
117 // использование нежелательно, но безопасно
118 public void setPosition(int position) {
119 if (position < 0) {
120 throw new IllegalArgumentException("index < 0");
122 if (position >= still.length) {
123 throw new ArrayIndexOutOfBoundsException("i >= length");
125 this.position = position;
128 public void start() {
129 animationLoop = true;
130 position = 0;
133 public void stop() {
134 animationLoop = false;
137 public boolean getState() {
138 return animationLoop;
141 public void nextToInfinity() {
142 final long time = System.currentTimeMillis() - lastTime;
143 if (time > deltaTime) {
144 lastTime = System.currentTimeMillis();
145 if (++position >= still.length) {
146 start();
151 public void next() {
152 final long time = System.currentTimeMillis() - lastTime;
153 if (getState() && time > deltaTime) {
154 lastTime = System.currentTimeMillis();
155 if (++position >= still.length) {
156 stop();
161 public void paint(Graphics graphics, int x, int y) {
162 if (animationLoop) {
163 graphics.drawImage(still[position], x, y, 0);