Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I draw an animated gif file using Graphics(2D)?

s3aApr 27 2011 — edited May 5 2011
I have attempted the following:
img = ImageIO.read(new File("/tmp/CampFire.gif"));
int x = (frame.getWidth() - buttonsPanel.getWidth() - img.getWidth(this))/2;
int y = frame.getHeight() - img.getHeight(this) - BORDER_HEIGHT; // BORDER_HEIGHT is a constant I made
g.drawImage(img, x , y, null);
but this does not animate the gif file. So I googled and found this:
Icon icon = new ImageIcon("/tmp/CampFire.gif");
JLabel label = new JLabel(icon);
JFrame frame = new JFrame();
frame.add(label);
which does animate the gif file but, to my knowledge, the coordinate system depends on a layout manager which is very difficult to implement in a "complex" simulation program. What I want is to easily tell Java where to put the gif file at the exact coordinate just like in my first code example (except with animation). Can someone please help me solve my problem?

Any help would be greatly appreciated!
Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2011
Added on Apr 27 2011
4 comments
3,457 views