Issue
I have recently designed an icon in blender for my application. I have exported this as a square 600 by 600 png file. How do I put this in a Java Swing or JavaFX application, and do I need to convert it to a different format? I also need this to be packaged neatly into a .JAR file. The icon needs to display right on all JRE supported platforms. Thank You
Solution
Java's ImageIO
class is a handy tool for reading and writing images. It supports JPEG, PNG, BMP, WBMP, and GIF formats. You can also use some third party dependencies like TwelveMonkeys to add support for more formats. This is how I would implement it in a Swing application:
JFrame frame = new JFrame("My Window");
// Setup frame...
try (InputStream in = this.getClass().getResourceAsStream("/myPackage/mySubPackage/myIcon.png")) {
BufferedImage image = ImageIO.read(in);
frame.setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
frame.setVisible(true);
Answered By - Cardinal System