How to Create Image Icons in Java

Sheeraz Gul Feb 02, 2024
  1. Use Swing.ImageIcon to Paint Image Icons in Java
  2. Use Swing.ImageIcon to Create Image Icon Buttons in Java
How to Create Image Icons in Java

The Swing library in Java has the class ImageIcon, which can be used to create image icons. This tutorial will demonstrate how to make different image icons in Java.

Use Swing.ImageIcon to Paint Image Icons in Java

Painting the image icon means creating an image icon from the given image.

Example:

package delftstack;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

class DrawingPanel extends JPanel {
  private ImageIcon img_icon;

  public DrawingPanel() {
    load_Image();
    init_Panel();
  }

  private void load_Image() {
    img_icon = new ImageIcon("delftstack.jpg");
  }

  private void init_Panel() {
    int img_width = img_icon.getIconWidth();
    int img_height = img_icon.getIconHeight();
    setPreferredSize(new Dimension(img_width, img_height));
  }

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    img_icon.paintIcon(this, g, 0, 0);
  }
}

public class Image_Icon extends JFrame {
  public Image_Icon() {
    init_User_Inter();
  }

  private void init_User_Inter() {
    DrawingPanel drawing1 = new DrawingPanel();

    createLayout(drawing1);

    setTitle("Image Icon");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  private void createLayout(JComponent... argument) {
    Container demo_pane = getContentPane();
    GroupLayout demo_gl = new GroupLayout(demo_pane);
    demo_pane.setLayout(demo_gl);

    demo_gl.setHorizontalGroup(demo_gl.createSequentialGroup().addComponent(argument[0]));

    demo_gl.setVerticalGroup(demo_gl.createParallelGroup().addComponent(argument[0]));

    pack();
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame demo_img = new Image_Icon();
      demo_img.setVisible(true);
    });
  }
}

The code above tries to load the delftstack.jpg image and paint it as an icon.

Output:

Paint Icon

Use Swing.ImageIcon to Create Image Icon Buttons in Java

Image icons are mainly used on buttons; let’s try to create buttons with image icons.

Example:

package delftstack;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Custom_Image_Icon extends JFrame {
  public Custom_Image_Icon() {
    init_User_Interface();
  }

  private void init_User_Interface() {
    ImageIcon Facebook_Icon = new ImageIcon("facebook.png");
    ImageIcon Twitter_Icon = new ImageIcon("twitter.png");
    ImageIcon Email_Icon = new ImageIcon("email.png");

    JButton Facebook_Btn = new JButton(Facebook_Icon);
    JButton Twitter_Btn = new JButton(Twitter_Icon);
    JButton Email_Btn = new JButton(Email_Icon);

    createLayout(Facebook_Btn, Twitter_Btn, Email_Btn);

    setTitle("Image Icons on Buttons");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
  }

  private void createLayout(JComponent... argument) {
    Container Btn_Pane = getContentPane();
    GroupLayout demo_graphic = new GroupLayout(Btn_Pane);
    Btn_Pane.setLayout(demo_graphic);

    demo_graphic.setAutoCreateContainerGaps(true);
    demo_graphic.setAutoCreateGaps(true);

    demo_graphic.setHorizontalGroup(demo_graphic.createSequentialGroup()
                                        .addComponent(argument[0])
                                        .addComponent(argument[1])
                                        .addComponent(argument[2]));

    demo_graphic.setVerticalGroup(demo_graphic.createParallelGroup()
                                      .addComponent(argument[0])
                                      .addComponent(argument[1])
                                      .addComponent(argument[2]));

    demo_graphic.linkSize(argument[0], argument[1], argument[2]);
    pack();
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      Custom_Image_Icon Btn_Icons = new Custom_Image_Icon();
      Btn_Icons.setVisible(true);
    });
  }
}

The code above creates three buttons with Facebook, Twitter, and email icons.

Output:

Button Icons

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Image