Java Swing Tutorial - Java ImageIcon.getImage()








Syntax

ImageIcon.getImage() has the following syntax.

public Image getImage()

Example

In the following code shows how to use ImageIcon.getImage() method.

/*  w  ww .jav  a2 s.  c  o m*/
import java.awt.Image;

import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {

  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    ImageIcon icon = new ImageIcon("yourFile.gif");

    Image normalImage = icon.getImage();
    Image grayImage = GrayFilter.createDisabledImage(normalImage);
    Icon warningIcon = new ImageIcon(grayImage);
    JLabel warningLabel = new JLabel(warningIcon);

    JLabel label3 = new JLabel("Warning", icon, JLabel.CENTER);

    frame.add(warningLabel,"Center");
    frame.add(label3,"South");

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}