Java 如何为 libGDX 桌面应用程序设置应用程序图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18250989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the application icon for a libGDX desktop application?
提问by Jo?o Pinto
I am trying to setup an application icon from the -desktop specific class with:
我正在尝试使用以下命令从 -desktop 特定类设置应用程序图标:
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 ?
图标未设置(至少在 Linux 上),知道如何设置吗?
回答by BennX
Take a look into the api (addIcon(...)):
看一看 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).
添加窗口图标。按照添加的顺序尝试图标,使用第一个有效的。通常应提供三个图标:128x128(对于 Mac)、32x32(对于 Windows 和 Linux)和 16x16(对于 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!
只是提一下,您只需在左上边缘设置小图标(如果应用程序已启动),它不是您在桌面上看到的图标!
回答by Leo Hilbert
that brought the Icon into my Mac-Dock! ;) Be sure to call it in your Lwjgl-Thread
将 Icon 带入了我的 Mac-Dock!;) 一定要在你的 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!
}
}