20

I am trying to setup an application icon from the -desktop specific class with:

package org.osgameseed.games.animalflip;

import com.badlogic.gdx.Files;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;

public class Main {
    public static void main(String[] args) {
        LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
        cfg.title = "AnimalFlip";
        cfg.useGL20 = false;
        cfg.width = 800;
        cfg.height = 600;
        cfg.addIcon("data/ic_launcher.png", Files.FileType.Internal);

        new LwjglApplication(new AnimalFlipGame(), cfg);
    }
}

The icon is not set (at least on Linux), any idea on how to set it ?

P.T.
  • 24,557
  • 7
  • 64
  • 95
João Pinto
  • 5,521
  • 5
  • 21
  • 35
  • Maybe your image doesn't get included when you export or not in the right location. How do you export? – Sajal Dutta Aug 15 '13 at 10:46
  • Afaik you don't need to export the image, it is placed on the proper data path, the configuration loader will fail if I rename the file so I am sure it's being loaded. – João Pinto Aug 15 '13 at 10:51
  • That's not what I meant. For instance, if you export from eclipse as jar, your assets might not get exported as you have in your dir. – Sajal Dutta Aug 15 '13 at 10:53
  • I am assembling the jar using ANT build rules, the icon is included. But again like I said, the configuration loader is able to find the file, so is not really a resource locating issue. I just can't figure how to instruct libGDX to use the icon for the window icon. – João Pinto Aug 15 '13 at 11:25
  • Open your exported jar and check if you have a data folder and inside that if you have your icon. Your jar won't crash if the icon is not there. – Sajal Dutta Aug 15 '13 at 11:29
  • Please ignore the jar import/export part, I am running the explication directly from Android Studio, there is no jar generated, just the plain class path being used, the icon is not set. – João Pinto Aug 15 '13 at 12:26

2 Answers2

17

Take a look into the api (addIcon(...)):

Adds a window icon. Icons are tried in the order added, the first one that works is used. Typically three icons should be provided: 128x128 (for Mac), 32x32 (for Windows and Linux), and 16x16 (for Windows).

Maybe your icon has the wrong dimensions, so it won't get set. Else it should work!

Just to mention you just set the small icon in the left upper edge (if the application is started) with it not the icon you would see at the desktop!
the small icon

jdurston
  • 199
  • 2
  • 14
bemeyer
  • 6,154
  • 4
  • 36
  • 86
  • 1
    The icon does have the proper dimmensions: ic_launcher.png: PNG image data, 32 x 32, 8-bit/color RGBA, non-interlaced, however it does not show up. – João Pinto Aug 16 '13 at 11:03
  • 2
    Has anyone found the solution? It does not show up on my Mac too. – Lim Thye Chean Sep 02 '15 at 14:06
  • So how then do you set the desktop icon? This doesn't answer the original question. – Jake Oct 03 '16 at 17:09
  • @Jake use an installation wrapper to set the according icon while installation. The desktop icon is not a part of the app code but can be set while installation of an application. It is a part of windows/Linux folder/file Definition if I know that right. – bemeyer Oct 31 '16 at 20:35
  • 32x32 appears low-quality and pixelated. I switched to 128x128 and it seems to be working just fine. I'm on Linux if it matters – Spikatrix Apr 15 '20 at 03:44
0

that brought the Icon into my Mac-Dock! ;) Be sure to call it in your Lwjgl-Thread

/**
 * workaround for Mac
 */
private static void setApplicationIcon() {
    try {
        Class<?> cls = Class.forName("com.apple.eawt.Application");
        Object application = cls.newInstance().getClass().getMethod("getApplication").invoke(null);

        FileHandle icon = Gdx.files.local("icons/icon.png");
        application.getClass().getMethod("setDockIconImage", java.awt.Image.class)
                .invoke(application, new ImageIcon(icon.file().getAbsolutePath()).getImage());
    } catch (Exception e) {
        // nobody cares!
    }
}
Leo Hilbert
  • 395
  • 2
  • 12