Skip to content

Instantly share code, notes, and snippets.

@ecspresso
Created January 17, 2022 13:31
Show Gist options
  • Save ecspresso/fd0372e7e1affd03c651b6f2b139d1ad to your computer and use it in GitHub Desktop.
Save ecspresso/fd0372e7e1affd03c651b6f2b139d1ad to your computer and use it in GitHub Desktop.
Create ImageIcons from a given path.
import javax.swing.ImageIcon;
public class ImageHandler {
/**
* Returns an ImageIcon, or null if the path was invalid.
* https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html
* @param path Path to image file.
* @return A new ImageIcon
*/
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = ImageHandler.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment