Concepts / Loading Images from Resource Files in Java

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.

  • Programming

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?

  • Hand the raw picture straight to the button

  • Wrap the picture in an object that Java knows how to display first

Reveal answer
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.

Iconinterface describing aniconImageIconclass built from a pictureOther icon classesother possibleimplementationsimplementsimplements
How are Icon, ImageIcon, and other possible icon classes related, and why does the code refer to them through the Icon type?

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.

Image objectholds a picturenew ImageIcon(image)constructor callImageIcon objectpicture is the suppliedimageIcon variableholds an iconpassed to constructorproducesstored in
What happens to the Image object when it is passed to the ImageIcon constructor, and what object comes out the other side?
Worked example

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.

  1. 1
    Start with the Image

    The variable image already refers to an Image object that holds the picture.

  2. 2
    Call the ImageIcon constructor

    Write new ImageIcon(image). This creates an ImageIcon whose picture is the image you supplied.

  3. 3
    Store the result as an Icon

    Declare a variable of type Icon and assign the new ImageIcon to it, because ImageIcon implements the Icon interface.

Result

Icon icon = new ImageIcon(image); leaves the variable icon holding an icon whose picture is the original image.

java
Notes on the code
  • 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.

Icondeclared typeiconvariable name=stores the resultnew ImageIcon(image)creates the object
In the line Icon icon = new ImageIcon(image), which part sets the variable's type, which part creates the object, and how do they connect?

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 like
    Icon icon = new Icon(image);
    Why it fails

    Icon 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 instead
    Icon icon = new ImageIcon(image);
  • Common mistake

    Treating the constructor as the place where the image is loaded

    What it looks like

    Thinking new ImageIcon(image) reads the picture from a file by itself

    Why it fails

    The 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 instead

    Obtain the Image first, then pass that Image to new ImageIcon(image).

  • Common mistake

    Skipping the wrapping step

    What it looks like
    Icon icon = image;
    Why it fails

    Java 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 instead
    Icon icon = new ImageIcon(image);
  • Common mistake

    Declaring the variable with a type unrelated to Icon and then expecting it to behave as an icon

LineStatusReason
Icon icon = new ImageIcon(image);CorrectImageIcon implements Icon, so the variable holds a valid icon
ImageIcon icon = new ImageIcon(image);CorrectThe variable type is the concrete class itself, which also works
Icon icon = new Icon(image);IncorrectIcon is an interface and cannot be constructed directly
Icon icon = image;IncorrectThe 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

EASY

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.