LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Messman on July 13, 2011, 09:41:52

Title: How to create an windowIcon from 16x16 png via ByteBuffer
Post by: Messman on July 13, 2011, 09:41:52
Hello There,

could somebody please be so kind to explain how to set the windowIcon next to the title bar?
As far as I figured out, the Display.setWindowIcon() takes an array of ByteBuffers. The ByteBuffer is created with some
static ByteBuffer-method alike:

ByteBuffer.allocateDirect(width * height * 4).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();

Alright. I guess the "width"/"height"-stuff corresponceses to the Image-Stats. In case of a small icon:

ByteBuffer.allocateDirect(16 * 16 * 4).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();

But where do I put the image-Data into the ByteBuffer? I read several times (at this forum) about some "IL.xxx"-calls.
What's that? How does it work? Can it be done with "GL.xxx" ?

Greetings,
Messman


Title: Re: How to create an windowIcon from 16x16 png via ByteBuffer
Post by: Simon Felix on July 13, 2011, 11:08:56
Check my post which shows how to set up a custom cursor: http://lwjgl.org/forum/index.php/topic,3935.msg21419.html#msg21419

Using this to set the icon is trivial.
Title: Re: How to create an windowIcon from 16x16 png via ByteBuffer
Post by: Messman on July 13, 2011, 18:32:11
Hello dr_evil,

for me it is not so trivial. I didnt manage even to create a ByteBuffer from an IntBuffer.
Fortunatly I could adapted the NeHe - OpenGL - Font (which seems to be currently not avaiable??):
  public static ByteBuffer[] getIcon(String path) throws FileNotFoundException, IOException {
   
    Image img = Toolkit.getDefaultToolkit().getImage(path);   
    MediaTracker lTracker = new MediaTracker(new JPanel());
    lTracker.addImage(img, 0);
    try {
      lTracker.waitForAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    lTracker.removeImage(img);

    BufferedImage lBImg = new BufferedImage(16, 16, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g2d = lBImg.createGraphics();
    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();
    img.flush();

    ByteBuffer buffer = ByteBuffer.allocateDirect(4 * 16 * 16);
    buffer.clear();
    byte[] data = (byte[]) lBImg.getRaster().getDataElements(0, 0, 16, 16, null);
    buffer.put(data);
    buffer.rewind();
   
    return (new ByteBuffer[] {buffer});
}


At least, it works. Although the dummy-JPanel for the MediaTracker isnt that beautiful. I didnt knew that one can use something like
   
while (! bi.createGraphics().drawImage(img, 0, 0, null)) {
      try {
        Thread.sleep(5);
      } catch (InterruptedException e) {
      }
}


Thanks,
Messman