Set window icon in lwjgl 3.1

I’ve been trying to set a png icon for my application (both taskbar and title bar) using the following example:

GLFWImage images[2];
images[0] = load_icon("my_icon.png");
images[1] = load_icon("my_icon_small.png");
glfwSetWindowIcon(window, 2, images);

The problem here is load_icon does not exist. What does that method look like? How do I create a GLFWImage object from a .png resource? Googling won’t show any viable solutions and nobody seems to know how to do it. I would really appreciate some help. Thank you!

The icon format is a raw byte image which you’ll need to load using an image loader library.

It looks like you can use the excellent std_image library via lwgl 3 to load your image, and then create the GLFWImage from that.

This is what I did, still need to get a GLFWImage object from that. Will post it as soon as it’s ready. This work is licensed under MIT.

public class image_parser {
    public static ByteBuffer load_image(String path) throws resource_error {
        ByteBuffer image;
        try (MemoryStack stack = MemoryStack.stackPush()) {
            IntBuffer comp = stack.mallocInt(1);
            IntBuffer w = stack.mallocInt(1);
            IntBuffer h = stack.mallocInt(1);

            image = stbi_load(path, w, h, comp, 4);
            if (image == null) {
                // throw new resource_error("Could not load image resources.");
            }
        }
        return image;
    }
}

Its done and its working, thanks for helping me out, I am posting my full code for others to freely use. This work is licensed under MIT.

Image Parsing class:

public class image_parser {
    public ByteBuffer get_image() {
        return image;
    }

    public int get_width() {
        return width;
    }

    public int get_heigh() {
        return heigh;
    }

    private ByteBuffer image;
    private int width, heigh;

    image_parser(int width, int heigh, ByteBuffer image) {
        this.image = image;
        this.heigh = heigh;
        this.width = width;
    }
    public static image_parser load_image(String path) {
        ByteBuffer image;
        int width, heigh;
        try (MemoryStack stack = MemoryStack.stackPush()) {
            IntBuffer comp = stack.mallocInt(1);
            IntBuffer w = stack.mallocInt(1);
            IntBuffer h = stack.mallocInt(1);

            image = stbi_load(path, w, h, comp, 4);
            if (image == null) {
                // throw new resource_error("Could not load image resources.");
            }
            width = w.get();
            heigh = h.get();
        }
        return new image_parser(width, heigh, image);
    }
}

Main class:

private final image_parser resource_01 = image_parser.load_image("icon_01.png");

public void init() {
        if (!glfwInit()) System.out.println("Could not initialise opengl4.5.");

        glfwWindowHint(GLFW_RESIZABLE, 0);
        glfwWindowHint(GLFW_DECORATED, 0);

        int width = 1200;
        int height = 800;
        window = glfwCreateWindow(width, height, "My Window", NULL, NULL);
        if (window == NULL) System.out.println("Could not create our window.");

        glfwSetWindowPos(window, 100, 100);
        glfwShowWindow(window);
        glfwMakeContextCurrent(window);
        
        GLFWImage image = GLFWImage.malloc(); GLFWImage.Buffer imagebf = GLFWImage.malloc(1);
        image.set(resource_01.get_width(), resource_01.get_heigh(), resource_01.get_image());
        imagebf.put(0, image);
        glfwSetWindowIcon(window, imagebf);
    }
2 Likes

Thanks for including your solution!

Hello !
I studied your solution and get
stbi_load(path, w, h, comp, 4); … is undefined
How do i have to handle that ?
Thanks in advance

Hi @appstation,

welcome to the GLFW forum.

To use the STBImage class you need to import it in Java.

If you want to load all the functions the easiest way is:

import static org.lwjgl.stb.STBImage.*

The static keyword is used here so you don’t have to type the STBImage.stbi_load() but can just type stbi_load().