Created
January 17, 2022 13:31
-
-
Save ecspresso/fd0372e7e1affd03c651b6f2b139d1ad to your computer and use it in GitHub Desktop.
Create ImageIcons from a given path.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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