Java ImageIcon load from path

Description

Java ImageIcon load from path

import javax.swing.ImageIcon;

public class Main {
  /** Returns an ImageIcon, or null if the path was invalid. */
  public static ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Main.class.getResource(path);
    if (imgURL != null) {
      return new ImageIcon(imgURL);
    } else {/*from w ww.  jav  a  2  s  . c o  m*/
      System.err.println("Couldn't find file: " + path);
      return null;
    }
  }
}



PreviousNext

Related