Computer Science Canada

Help with sprite animation

Author:  Harblkipz [ Tue May 13, 2008 8:45 am ]
Post subject:  Help with sprite animation

Hey, so I have a set of images, and I have loaded them into an array of images in my program. I then have a JFrame created, and my plan is to display the images in a repeating cycle as an ImageIcon of JLabel. When I load one image on its own, it displays correctly in the frame, but when I try to cycle through the array in a For loop, the images are not being cycled in the window... No idea why. Any help is appreciated.

My Main class:
code:

import javax.swing.*;
public class Game{
   public static void main(String[] args){
       boolean loop = true;
       Koopa koopa = new Koopa();
       JFrame frame = new JFrame("Mario Game");
       JLabel turtle = new JLabel(new ImageIcon(koopa.trtl(1)));
       frame.add(turtle);
       frame.setSize(200,200);
       frame.validate();
       frame.setVisible(true);
       frame.setLayout(null);
       while(loop==true){
           for(int i = 0;i < 5; i++){
               turtle = new JLabel(new ImageIcon(koopa.trtl(i)));
               frame.validate();
           }
       }
   }
}


And here is the class I use to load the turtle images:
code:

import java.awt.*;
public class Koopa{
public Image trtl(int G) {
    Image[] trtl = new Image[5];
    for(int i= 0 ;i<5;i++){
        trtl[i] = Toolkit.getDefaultToolkit().getImage("G:\\Game shit\\sprite\\Turtle2\\"+i+".JPG");
    }
    return trtl[G];
}
}

[/b]

Author:  jernst [ Tue May 13, 2008 10:09 am ]
Post subject:  Re: Help with sprite animation

In your loop you have created a new Jlabel but it is not a part of the frame anywhere. So either you need to add it to the frame (which im not sure will do what you want) or you could change the contents of the Jlabel that is already in the frame.


: