Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: ImageIcon vs. Image difference

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

like image 246
ps-aux Avatar asked Jun 25 '12 19:06

ps-aux


People also ask

What is ImageIcon in Java?

public class ImageIcon extends Object implements Icon, Serializable, Accessible. An implementation of the Icon interface that paints Icons from Images. Images that are created from a URL, filename or byte array are preloaded using MediaTracker to monitor the loaded state of the image.

What is an image Icon?

Icon is small fixed size picture, typically used to decorate components. ImageIcon is an implementation of the Icon interface that paints icons from images. Images can be created from a URL, filename, or byte array. paintIcon(Component c, Graphics g, int x, int y)

How to add image Icon in JFrame in Java?

To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon);


2 Answers

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.

like image 139
tenorsax Avatar answered Sep 19 '22 15:09

tenorsax


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.

like image 27
Nicolas Avatar answered Sep 17 '22 15:09

Nicolas