Java lwjgl+OpenGL Display.setIcon(...) question

Accname

2D-Graphics enthusiast
Reaction score
1,462
Hi, i am currently trying to use OpenGL with lwjgl in java.
My current problem is to set the icon used by the display (that one in the upper left corner on windows).
The function takes a ByteBuffer array, but i want to use png-files i have on the harddrive as images.

So, after a little bit of google search i came up with the following code:
Code:
ByteBuffer[] icon_array = new ByteBuffer[ICON_PATHS.length];
        try {
            for (int i = 0; i < ICON_PATHS.length; i++){
                icon_array[i] = ByteBuffer.allocateDirect(1);
                String path = ICON_PATHS[i];
               
                File icon_file = new File(path);
                BufferedImage bufferedImage = ImageIO.read(icon_file);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ImageIO.write(bufferedImage, "png", byteArrayOutputStream);
                byte[] imageData = byteArrayOutputStream.toByteArray();
                icon_array[i] = ByteBuffer.wrap(imageData);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Display.setIcon(icon_array);
ICON_PATHS is a String array containing 3 strings for 3 different png-files with sizes 16x16, 32x32 and 128x128 as the java.doc for Display.setIcon(...) specifies i need at least these 3.

The problem is, it doesnt quite work correctly.
Fact is, i get something to be displayed, but definitely not the icon i wanted to have. It seems to be like random colors.
If i try to use different png-files (with the same dimensions) it doesnt work at all.

Can somebody help me?
Edit: No exceptions or errors are thrown, it just doesnt display the images correctly or (if i use other files) does not take them at all.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I suspect that the images are in a wrong file format.
Display.setIcon requires them to be in RGBA mode, while the ByteBuffer probably keeps the data in some other way.

I can't figure out how the ByteBuffer color model conversions work, but maybe that'll help:
http://lwjgl.org/forum/index.php?topic=3422.0
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
Although i had to tweak it a little bit that actually helped, thank you alot!

If anybody has the same problem or is just interested in general:
Code:
{...
        ByteBuffer[] icon_array = new ByteBuffer[ICON_PATHS.length];
        try {
            for (int i = 0; i < ICON_PATHS.length; i++){
                icon_array[i] = ByteBuffer.allocateDirect(1);
                String path = ICON_PATHS[i];
                icon_array[i] = loadIcon(path);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Display.setIcon(icon_array);
...}
 
private static ByteBuffer loadIcon(String path) throws IOException {
        InputStream inputStream = new FileInputStream(path);
        try {
            PNGDecoder decoder = new PNGDecoder(inputStream);
            ByteBuffer bytebuf = ByteBuffer.allocateDirect(decoder.getWidth()*decoder.getHeight()*4);
            decoder.decode(bytebuf, decoder.getWidth()*4, PNGDecoder.Format.RGBA);
            bytebuf.flip();
            return bytebuf;
        } finally {
            inputStream.close();
        }
    }

The PNGDecoder used is from an external .jar which can be downloaded here:
http://twl.l33tlabs.org/#downloads
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top