Java: ImageIcon vs. Image difference
Asked Answered
T

2

14

Could anyone explain to me in noob way what the difference is betweeen ImageIcon and Image classes/objects in Java? Thanks

Ticktock answered 25/6, 2012 at 19:18 Comment(2)
Is this homework? Everything needed to answer this question should be documented in the javadocs.Biographer
No it is not a homework. For this question like for the great majority of my other questions regarding Java, the info can be found in javadocs. But if I could understand everything from javadocs I wouldnt need any books, forums, or google or even internet for that matter.Ticktock
L
12

Their nature and application is different. Image is an abstract superclass of all classes that represent graphical images. ImageIcon is an implementation of Icon interface that uses Image as its source.

Edit: Think of an Image as something that could be rendered and an ImageIcon as something that will be rendered as an Icon when its paintIcon() method is called.

Edit: The links above will take you to the JDK 6 api. These links will take you to the JDK 8 api: Image and ImageIcon.

Landers answered 25/6, 2012 at 19:33 Comment(2)
Ok so ImageIcon object has an underlying Image object and provides an extra layer with methods to manipulate this image? Also, Icon can be implemented in number of ways and not necessarily to have the underlying image (for example painting a label or rectangle). Or am I wrong? I just don't fully understand, if icon is just a small image, why to have two different classes for that.Ticktock
@LeNoob Icon is typically used to decorate components (labels, buttons, etc), i.e. they are painted on something. Image, on the other hand, is not only for displaying, it can be used to draw on it. Take a look at Images tutorial for some examples. How to Use Icons can also be useful.Landers
C
3

You can scale and save Image, but you can't do it with ImageIcon. For creating pictures in your GUI you usually have to use ImageIcon, but if you don't wanna do that, Image should be better.

Conveyor answered 25/6, 2012 at 20:48 Comment(1)
But if ImageIcon has underlying image, how come I cannot save it and scale it?Ticktock

© 2022 - 2024 — McMap. All rights reserved.