Display an Image in Java

Rupam Yadav Jan 30, 2023 Sep 06, 2021
  1. Display an Image in Java Using JLabel.setIcon()
  2. Display an Image in Java Using ImageJ Library
Display an Image in Java

We can show an image using several ways in Java. Below we will see how to display an image in Java using two methods.

Display an Image in Java Using JLabel.setIcon()

In the first example, we use JLabel class of the Swing library. JLabel extends JComponent, and we can attach this component to a JFrame. To read the image file, we use the File class and pass the path of the image. Next we convert the image to a BufferedImage object using ImageIO.read(). Now we create an icon to be shown in the JLabel.

To show the label icon, we need a JFrame object with a FlowLayout and a size of 500 x 500. The size can be adjusted according to our needs. Now we create a JLabel object and set its icon using JLabel.setIcon() function. Then we add the jLabel component to jFrame and set the visibility of the frame as true.

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class DisplayImage {
    public static void main(String[] args) throws IOException {

        File file = new File("C:\\Users\\User1\\Pictures\\Camera Roll\\java.png");
        BufferedImage bufferedImage = ImageIO.read(file);

        ImageIcon imageIcon = new ImageIcon(bufferedImage);
        JFrame jFrame = new JFrame();

        jFrame.setLayout(new FlowLayout());
        
        jFrame.setSize(500, 500);
        JLabel jLabel = new JLabel();

        jLabel.setIcon(imageIcon);
        jFrame.add(jLabel);
        jFrame.setVisible(true);

        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Output:

display an image in java

Display an Image in Java Using ImageJ Library

In this program, we use a powerful image processing library called ImageJ. To use it, we first import the maven repository and the dependency into pom.xml.

<repositories>
    <repository>
        <id>scijava.public</id>
        <url>https://maven.scijava.org/content/groups/public</url>
    </repository>
</repositories>
<dependency>
    <groupId>net.imagej</groupId>
    <artifactId>ij</artifactId>
    <version>1.53j</version>
</dependency>

Our goal is to show an image, and ImageJ makes it simple for us. Following is the code where we first call a static function openImage() from the class IJ and pass the path of the image in it as an argument. Notice that we write only the image name with its extensions because our image is in the same directory.

IJ.openImage() returns an ImagePlus object imagePlus. Now we call the show() method using the imagePlus object. We can see that the output shows an image in a frame.

import ij.IJ;
import ij.ImagePlus;

public class DisplayImage {
    public static void main(String[] args) {
        ImagePlus imagePlus = IJ.openImage("mountains.jpeg");
        imagePlus.show();

    }
}

Output:

display an image in java

Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java JLabel

Related Article - Java Swing