I am making a game in which it has a JFrame that contains a gridlayout full of Jlabels. I want to make a separate image class that can be added to the JLabels. So far, I have been trying to override the paintIcon method to draw a picture icon to the label. I have been trying, unsuccessfully, to draw an animated GIF. The code is as follows:
UnitIF is an abstract class that extends ImageIcon and Implements Icon; The Infantry class is required to redifine the paint method.
The code works for a non-animated gif file I have, however when trying to use it with an animated gif file, the result is a blank JLabel with no image. Any ideas on how to fix this? or is there a better way to maake a separate image class?
Code:
public class Infantry extends UnitIF{
ImageIcon icon;
public void paintIcon(Component c, Graphics g, int x, int y) {
icon = new ImageIcon("BlueInfantry.gif");
g.drawImage(icon.getImage(), (c.getWidth()/4), 0, c.getWidth(), c.getHeight(), c);
}
}
The code works for a non-animated gif file I have, however when trying to use it with an animated gif file, the result is a blank JLabel with no image. Any ideas on how to fix this? or is there a better way to maake a separate image class?