Search code examples
javaimageobjectimageicon

Why use ImageIcon rather than Image?


In what instance would I want to use ImageIcon to represent a picture file rather than an Image object? I've been searching and I've seen people say that you would use an ImageIcon object when dealing with images that will be part of the GUI, but I still don't understand the implications of this. In other words, what is the actual difference between the two object types and what situations are they each suited for?


Solution

  • Image is an object that represents a bitmap: an array of pixels of different colors.

    Icon is an object that can draw a rectangular piece of graphics. Since it is an interface (and a simple one too), you can imagine many different types of icons: drawing predefined vector graphics, generating images on the fly etc. Therefore it is a useful abstraction and is used by Swing components (buttons, labels).

    ImageIcon is an object that IS an Icon, but HAS-A Image. That is - it draws graphics based on a specific image.

    When you say "why should I be using an ImageIcon instead of Image" you miss the point: in fact you are using an Image either way.