By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

How to override ImageIcon paint method to display gif Image?

P: 19
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:

Expand|Select|Wrap|Line Numbers
  1. public class Infantry extends UnitIF{
  3. ImageIcon icon;
  5. public void paintIcon(Component c, Graphics g, int x, int y) {
  7.      icon = new ImageIcon("BlueInfantry.gif");
  8. g.drawImage(icon.getImage(), (c.getWidth()/4), 0, c.getWidth(), c.getHeight(), c);
  10.     } 
  11. }
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?
Jan 20 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.