package code.kalter.longflight; import java.io.IOException; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; /** * Реализация анимации: покадровая смена кадра из массива картинок за * определённое время. Можно запустить как конечную анимацию, так и бесконечную * * @author KalterFive */ public class Animation { public static Animation createAnimation(String path, int width, int deltaTime) throws IOException { final Loader loader = Loader.getInstance(); final Image image = loader.getImage(path); return createAnimation(image, width, deltaTime); } public static Animation createAnimation(String path, int width, int height, int deltaTime) throws IOException { final Loader loader = Loader.getInstance(); final Image image = loader.getImage(path); return createAnimation(image, width, height, deltaTime); } public static Animation createAnimation(Image source, int width, int deltaTime) { final int height = source.getHeight(); return createAnimation(source, width, height, deltaTime); } public static Animation createAnimation(Image source, int width, int height, int deltaTime) { final Image[] still = new Image[source.getWidth() / width]; for (int i = 0; i < still.length; i++) { still[i] = Image.createImage(source, i * width, 0, width, height, 0); } return createAnimation(still, width, height, deltaTime); } public static Animation createAnimation(Image[] still, int deltaTime) { int width = still[0].getWidth(); int height = still[0].getHeight(); for (int i = 1; i < still.length; i++) { if (still[i].getWidth() > width) { width = still[i].getWidth(); } if (still[i].getHeight() > height) { height = still[i].getHeight(); } } return createAnimation(still, width, height, deltaTime); } public static Animation createAnimation(Image[] still, int width, int height, int deltaTime) { return new Animation(still, width, height, deltaTime); } private final Image[] still; // кадры private final int width; private final int height; //======================= private long lastTime; // необходимо для засекания времени private int deltaTime; // скорость смены кадров //======================= private int position; private boolean animationLoop; private Animation(Image[] still, int width, int height, int deltaTime) { this.still = still; this.width = width; this.height = height; this.deltaTime = deltaTime; lastTime = System.currentTimeMillis(); position = 0; animationLoop = true; } public Image getStill(int index) { if (index < 0) { throw new IllegalArgumentException("index < 0"); } if (index >= still.length) { throw new ArrayIndexOutOfBoundsException("i >= length"); } return still[index]; } public int getWidth() { return width; } public int getHeight() { return height; } public int getDeltaTime() { return deltaTime; } public void setDeltaTime(int deltaTime) { if (deltaTime < 0) { throw new IllegalArgumentException("delta time < 0"); } this.deltaTime = deltaTime; } public int getPosition() { return position; } // использование нежелательно, но безопасно public void setPosition(int position) { if (position < 0) { throw new IllegalArgumentException("index < 0"); } if (position >= still.length) { throw new ArrayIndexOutOfBoundsException("i >= length"); } this.position = position; } public void start() { animationLoop = true; position = 0; } public void stop() { animationLoop = false; } public boolean getState() { return animationLoop; } public void nextToInfinity() { final long time = System.currentTimeMillis() - lastTime; if (time > deltaTime) { lastTime = System.currentTimeMillis(); if (++position >= still.length) { start(); } } } public void next() { final long time = System.currentTimeMillis() - lastTime; if (getState() && time > deltaTime) { lastTime = System.currentTimeMillis(); if (++position >= still.length) { stop(); } } } public void paint(Graphics graphics, int x, int y) { if (animationLoop) { graphics.drawImage(still[position], x, y, 0); } } }