Small contribution for icons

Hello everybody. I am working on my project and when i tried to set an icon to it, i found that i needed to use a BufferedImage (yeah, the one from java.awt.image). It’s pretty strange as if i use this i will not be able to export my project on android (i think, i don’t have an android, so i don’t know exactly how it works). So, i decided to find a workaround, and this is the result of my work.

To create an icon :
[java]
private ByteBuffer loadImage(AssetManager assetManager, String imageName)
{
TextureKey key = new TextureKey(imageName, false);
Texture texture = assetManager.loadTexture(key);
Image image = texture.getImage();

ByteBuffer result;
int w, h;
w = image.getWidth();
h = image.getHeight();

int size = w * h * 4;
byte[] bytes = new byte[size];

ImageRaster raster = ImageRaster.create(image);
ColorRGBA color = new ColorRGBA();
int k = 0;
for (int i = 0; i < h; i++)
{
  for (int j = 0; j < w; j++)
  {
    raster.getPixel(j, i, color);
    
    bytes[k++] = (byte) (color.r * 255);
    bytes[k++] = (byte) (color.g * 255);
    bytes[k++] = (byte) (color.b * 255);
    bytes[k++] = (byte) (color.a * 255);
    
  }
}


result = ByteBuffer.wrap(bytes);

return result;

}
[/java]

to display icons:

[java]
ByteBuffer[] images;

images = new ByteBuffer[]
{
  loadImage(assetManager, "Textures/icons/32.png"),
  loadImage(assetManager, "Textures/icons/16.png")
};

Display.setIcon(images);

[/java]

However, i am aware that this solution is not perfect, as i don’t use the appsettings stuff. I use directly the Display class, from org.lwjgl.opengl. I don’t know if it works on jogl (and according to a doc i found somewhere i don’t remember, it doesn’t work).
This “however” it not really a problem as you have access to the core and you can modify it if you want.

Acutally, i don’t know if it’s even relevant to do something like that as i don’t know if there is anything like “icon” on android. But who know ? As it seems that jme try to stay away as much as possible from the awt.image, this could help in the futur.

1 Like

Which icons are you talking about? The ones set in main()?

I am talking about the icon at the uper left on the window and in the taskbar. I am NOT talking about the icon in the explorer (or anything equivalent on other os than Windows), whith is a whole different thing, as it has nothing to do with code execution.

@bubuche said: I am talking about the icon at the uper left on the window and in the taskbar. I am NOT talking about the icon in the explorer (or anything equivalent on other os than Windows), whith is a whole different thing, as it has nothing to do with code execution.

You were worried about android and that’s why I wondered. Since it’s a desktop specific thing, I wasn’t sure why the concern.