How to override ImageIcon paint method to display gif Image?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeNoob117
    New Member
    • Jan 2013
    • 19

    How to override ImageIcon paint method to display gif Image?

    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:

    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);
    		
    	} 
    }
    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?
    Last edited by Rabbit; Jan 21 '13, 12:11 AM. Reason: Please use code tags when posting code.
Working...