I'm kinda stuck. I am creating a Java Memory Game that consists of a 6x6 grid of animated sprites (requirement of my professor). On my trial, I was only able to display one component that has a sprite animation. My other try was changing p1[i] = new Ash() which was able to paint all sprites to all components in the grid but no animation. I would like to ask for some ideas in how to approach this in which all components in the grid has an ANIMATED sprite.
My main class:
public class Main extends Component{
public static void main(String[] args) {
Ash ash = new Ash();
JFrame f = new JFrame("Game sample");
JPanel panel1 = new JPanel(new GridLayout(6,6,6,6));
JPanel[] p1 = new JPanel[36];
for(int i = 0;i < 36;i++){
p1[i] = new JPanel(new BorderLayout());
p1[i].add(ash);
panel1.add(p1[i]);
}
f.add(panel1,BorderLayout.CENTER);
f.setSize(500,500);
f.setVisible(true);
long start, trigger = 0, delay = 1000 / 8;
while(true) {
start = System.currentTimeMillis();
if(start > trigger) {
trigger = start + delay;
ash.repaint();
}
}
}
}
My Ash class:
public class Ash extends JPanel{
BufferedImageLoader loader;
BufferedImage spriteSheet;
BufferedImage sprite, sprite2, sprite3, sprite4;
int step = 0, start = 0;
public Ash() {
loader = new BufferedImageLoader();
spriteSheet = null;
try{
spriteSheet = loader.loadImage("spritesheet.png");
}catch (IOException ex){
}
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
SpriteSheet ss = new SpriteSheet(spriteSheet);
sprite = ss.grabSprite(start + step, 0, 32, 50);
g2d.drawImage(sprite,0,10,null);
if(step == 96) {
step = 0;
} else {
step += 32;
}
}