2

I'm looking for a way to find the pixel dimensions of a .gif file in Java.

Will I have to convert one of the images from the gif to a BufferedImage and get it from there or is there a simpler way?

Using ImageIO doesn't work because I need to use ImageIcons to draw a gif using Swing. Here's how I'm drawing it.

attackImage = new ImageIcon("assets/Attacks/EnergyAttack.gif").getImage();
g.drawImage(attackImage, getX() + 20, getY(), null);
Ferex
  • 61
  • 4
  • 1
    Possible duplicate of [How to get image height and width using java?](https://stackoverflow.com/questions/672916/how-to-get-image-height-and-width-using-java) – MTCoster Aug 16 '19 at 16:15
  • @MTCoster no because this specifically relates to gifs, not jpegs/pngs – Ferex Aug 16 '19 at 16:23

2 Answers2

1

I quote you:

I need to use ImageIcons to draw a gif using Swing.

Have you tried using ImageIcon.getIconHeight and ImageIcon.getIconWidth? Additionally, if you have a fixed-size view, you might consider using ImageIcon.getImage followed by Image.getScaledInstance.

Avi
  • 2,611
  • 1
  • 14
  • 26
1

Since you already have an ImageIcon instance, you can simply use its getIconHeight() and getIconWidth() methods to get these values:

ImageIcon attackImage = new ImageIcon("assets/Attacks/EnergyAttack.gif");
int attackImageHeight = attackImage.getIconHeight();
int attackImageWidth = attackImage.getIconWidth();
g.drawImage(attackImage.getImage(), getX() + 20, getY(), null);
MTCoster
  • 5,868
  • 3
  • 28
  • 49