Java application trayicon looks bad when OS is scaled because of high dpi screen

388 views Asked by At

With Java, you can use TrayIcon and java.awt.SystemTray to make a trayicon. But this icon looks bad when you use for example Windows in combination with a 4K monitor and Windows is automatically scaling everything bigger.

Java sets the trayiconsize to 16x16 but this is way too small for the scaled mode and therefore it looks bad. Do you know how to fix this?

Edit:

Here is an example code and screenshot of the result. I am using BMW's logo (256x256 pixel) :)

package trayicon;

import java.awt.AWTException;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Trayicon {

    public static void main(String[] args) throws IOException {
        //Check the SystemTray is supported
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        BufferedImage img = ImageIO.read(new File("256x256.png"));
        final TrayIcon trayIcon =
                new TrayIcon(img, "Trayicon");
        trayIcon.setImageAutoSize(true);
        final SystemTray tray = SystemTray.getSystemTray();
        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
        }
        System.in.read();
        System.exit(0);
    }

}

Result (look at the lower right icon):

Trayicon

0

There are 0 answers