Loading Images from Resource Files in Java
An icon in Java GUI is represented by an object of type Icon, and Icon is an interface rather than a class.
Why Pictures Need Wrapping
Buttons, labels, and menus in a Java graphical interface often display small pictures alongside their text. Those pictures are not simply dropped onto a component. Java expects the picture to be wrapped in a particular kind of object first, and the GUI then knows how to display that object. This article follows that wrapping step from start to finish, so you can see which piece is responsible for what.
What do you think happens?
If a button should show a picture, can you hand the raw picture straight to the button, or does something have to happen first?
Reveal answer
Wrap the picture in an object that Java knows how to display first
Java expects the picture to be wrapped in a particular kind of object before the GUI displays it. The wrapping object is an icon.
Icon and ImageIcon: Two Different Roles
The name Icon refers to a type that describes what an icon is in a Java GUI. The name ImageIcon refers to a class that provides an actual icon built from a picture. Keeping these two roles separate is the key to reading icon code. The type Icon is the general description; ImageIcon is one concrete way of satisfying that description.
An icon in Java GUI is represented by an object of type Icon, and Icon is an interface rather than a class. An interface describes what something must be able to do, without providing the full working version itself. The class ImageIcon implements the Icon interface, which means it is one of the concrete things that fills the Icon role.
Because ImageIcon implements the Icon interface, an ImageIcon object can be stored in a variable whose declared type is Icon. This is the usual reason a programmer writes the more general type on the left of a declaration. The variable promises only that it holds something that behaves as an icon, and the specific class behind it can be chosen separately.
Tracing an Image Into an Icon
Work through the process step by step. First, you have an Image object, which holds a picture. Second, you pass that image to the constructor of ImageIcon by writing new ImageIcon(image). Third, Java produces an ImageIcon object whose picture is the image you supplied. Finally, you can store that result in a variable of type Icon, because ImageIcon implements the interface.
Wrapping a Picture as an Icon
You already have an Image object named image that holds a picture. Make an icon from it and store that icon in a variable of type Icon.
- 1Start with the Image
The variable image already refers to an Image object that holds the picture.
- 2Call the ImageIcon constructor
Write new
ImageIcon(image). This creates an ImageIcon whose picture is the image you supplied. - 3Store the result as an Icon
Declare a variable of type Icon and assign the new ImageIcon to it, because ImageIcon implements the Icon interface.
Icon icon = new ImageIcon(image); leaves the variable icon holding an icon whose picture is the original image.
- Icon is the declared type on the left: the variable promises only that it holds an icon.
- new
ImageIcon(image)is the creation step: it builds an ImageIcon from the Image object. - The = stores the new ImageIcon in the Icon variable.
Reading the Declaration Line
A single line such as Icon icon = new ImageIcon(image) does two jobs at once. The part on the left sets the type of the variable. The part on the right creates the object. The equals sign connects them by storing the newly created object in the variable. The two parts are independent choices: the left side names the general role, and the right side names the concrete class that fills it.
Common Icon Mistakes
The mistakes below are built from the rules in this article. Each compares a correct line with a faulty one, so you can see where the reasoning breaks down.
Common mistake Creating an object from the interface name
What it looks likeIcon icon = new Icon(image);Why it failsIcon is an interface that describes what an icon is, not a class that can be constructed. The constructor belongs to a class that implements Icon, such as ImageIcon.
Do this insteadIcon icon = new ImageIcon(image);Common mistake Treating the constructor as the place where the image is loaded
What it looks likeThinking new ImageIcon(image) reads the picture from a file by itself
Why it failsThe constructor handles only the wrapping step. It takes an Image object that already exists and makes an ImageIcon whose picture is that image.
Do this insteadObtain the Image first, then pass that Image to new ImageIcon(image).
Common mistake Skipping the wrapping step
What it looks likeIcon icon = image;Why it failsJava expects a picture to be wrapped in an icon object before the GUI displays it. An Image object on its own is not the icon that the GUI is set up to use in this way.
Do this insteadIcon icon = new ImageIcon(image);Common mistake Declaring the variable with a type unrelated to Icon and then expecting it to behave as an icon
| Line | Status | Reason |
|---|---|---|
| Icon icon = new ImageIcon(image); | Correct | ImageIcon implements Icon, so the variable holds a valid icon |
| ImageIcon icon = new ImageIcon(image); | Correct | The variable type is the concrete class itself, which also works |
| Icon icon = new Icon(image); | Incorrect | Icon is an interface and cannot be constructed directly |
| Icon icon = image; | Incorrect | The Image is not wrapped in an ImageIcon before being used as an icon |
Correct and incorrect icon declarations compared
Practice: Declaring an Icon From an Image
You have an Image object named logo. Write one Java line that creates an ImageIcon from logo and stores it in a variable named logoIcon whose declared type is Icon. Then explain in one sentence which part of your line sets the type and which part creates the object.
Hints
- The declared type goes on the left of the equals sign and must be Icon.
- The creation step uses the constructor call new ImageIcon with the Image as its argument.
- Check that you did not try to construct Icon itself, since it is an interface.
Key Takeaways
- An icon in Java GUI is represented by the Icon type, which is an interface rather than a class.
- ImageIcon is a class that implements Icon and builds an icon from an Image object.
- The constructor call new
ImageIcon(image)wraps an existing Image as an ImageIcon, and the result can be stored in a variable of type Icon. - Wrapping an image as an icon is a separate job from loading the image, which may come from a resource file and is covered later in the chapter.
- Icon is the general role on the left of a declaration; ImageIcon is the concrete class that fills it on the right.