Carregar uma imagem de sprites em java
Quero perguntar por que estou recebendo erro ao carregar qualquer imagem de sprite no objeto
Aqui está como eu recebo a imagem.
import java.awt.image.BufferedImage;
import java.io.IOException;
public class SpriteSheet {
public BufferedImage sprite;
public BufferedImage[] sprites;
int width;
int height;
int rows;
int columns;
public SpriteSheet(int width, int height, int rows, int columns, BufferedImage ss) throws IOException {
this.width = width;
this.height = height;
this.rows = rows;
this.columns = columns;
this.sprite = ss;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
sprites[(i * columns) + j] = ss.getSubimage(i * width, j * height, width, height);
}
}
}
}
aqui está como eu estou implementando
public BufferedImage[] init(){
BufferedImageLoader loader = new BufferedImageLoader();
BufferedImage spriteSheet = null;
SpriteSheet ss = null;
try {
spriteSheet = loader.loadImage("planet.png");
ss = new SpriteSheet(72,72,4,5,spriteSheet);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ss.sprites;
}
Também quero perguntar sobre minha maneira de usar a matriz de sprites. Eu quero usar no timer alterando a imagem desenhada pelo evento action, alterando a imagem atual do sprite
tmr = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for(Rock r:rocks){
r.move();
r.changeSprite();
}
repaint();
}
});
com o método
public void changeSprite(){
if(ds==12)
ds=0;
ds++;
currentSprite = sprite[ds];
}
Cada objeto de rocha possui um conjunto de sprites cheios de imagem em buffer recebida da imagem de sprite carregada e uma imagem atual que é desenhada. o cronômetro mudará a imagem atual e a redesenhará no objeto para que todo o sprite seja desenhado, mas não parece funcionar. Portanto, é meu loadingSpriteImage que tem o problema ou minha maneira de desenhá-lo está causando o problema?