Hello. I'm using the following code to construct an animated image, slicing one big image to parts:
Code:
package core.main.graphics;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import core.main.Player;
public class AnimatedImage implements Drawable{
private Image sourceImg;
private Image img;
private Image animated[];
private Image tcolored[][];
private int length, count, width, height;
private int animNum, colorNum;
public AnimatedImage(Image img, Image teamcolor, int width, int height){
try{
animNum = 0;
colorNum = 0;
int clength = Player.playerColors.length + 1;
sourceImg = img;
length = (int) (img.getWidth() / width);
count = (int) (img.getHeight() / height);
animated = new Image[count * length + count];
tcolored = new Image[clength][animated.length];
this.width = width;
this.height = height;
for(int i = 0; i < count; i++)
for(int j = 0; j < length; j++){
Image bi = new Image(width, height);
Graphics g = bi.getGraphics();
g.drawImage(img, -j * width, -i * height, null);
g.destroy();
animated[length * i + j] = bi;
for(int k = 0; k < clength; k++){
bi = new Image(width, height);
g = bi.getGraphics();
g.drawImage(teamcolor, -j * width, -i * height, null);
if (k > 0){
Color rec = Player.playerColors[k - 1];
for(int l = 0; l < width; l++)
for(int n = 0; n < height; n++){
Color cur = new Color(bi.getColor(l, n));
java.awt.Color ncur = new java.awt.Color(cur.r, cur.g, cur.b, cur.a);
if (ncur.getRGB() < -0xFFFFFF)
continue;
int rc = Math.min(255, cur.getRed() + rec.getRed()),
gc = Math.min(255, cur.getGreen() + rec.getGreen()),
bc = Math.min(255, cur.getBlue() + rec.getBlue());
Color nc = new Color(rc, gc, bc);
g.setColor(nc);
g.drawRect(l, n, 0, 0);
}
}
g.destroy();
tcolored[k][length * i + j] = bi;
}
}
this.img = animated[0];
} catch(Exception e){
e.printStackTrace();
}
}
public void draw(Graphics g, double x, double y){
if (img == null){
Color cdef = g.getColor();
g.setColor(Color.red);
g.fillRect((int) x, (int) y, 4, 4);
g.setColor(cdef);
} else {
int dx = (int) (x - img.getWidth() / 2.),
dy = (int) (y - img.getHeight() / 2.);
g.drawImage(img, dx, dy, null);
g.drawImage(tcolored[colorNum][animNum], dx, dy, null);
}
}
public void setAnimation(int num, int step){
animNum = num * length + step;
img = animated[animNum];
}
public void setTeamColor(int clr){
colorNum = clr + 1;
}
public int getLength(){
return length;
}
}
And it takes over 20 seconds to construct one animated animated image from 320x96 png image, where one piece is 32x32. In pure java this operation is instant. So I can't even test my application normally because of extremely long loading time. What should I do?