I am trying to figure out how to show colored emojis in Java Swing.
I am on Windows and I have tried using multiple built in fonts and as my last attempt I tried using the "Noto Emoji":
https://github.com/googlefonts/noto-emoji/tree/main/fonts
Mentioned in this Stackoverflow:
How to get emoji with colors in JLabel
I have written a very simple JFrame
example to try and get this working:
package com.example;
import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
public class Main {
public static void main(String[] args) {
Font font;
try {
InputStream is = Main.class.getResourceAsStream("NotoColorEmoji.ttf");
font = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(Font.PLAIN, 24);
System.out.println("can show \uD83D\uDE02: " + font.canDisplayUpTo("\uD83D\uDE02"));
System.out.println("can show : " + font.canDisplayUpTo(""));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
} catch (Exception exception) {
exception.printStackTrace();
return;
}
JFrame frame = new JFrame("Emoji example");
frame.setSize(400, 400);
frame.setLayout(null);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField textField = new JTextField("\uD83D\uDE02 ");
textField.setBounds(20, 20, 350, 50);
textField.setFont(font);
frame.add(textField);
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setBounds(20, 80, 350, 50);
comboBox.addItem("\uD83D\uDE02");
comboBox.addItem("");
comboBox.setFont(font);
frame.add(comboBox);
frame.validate();
frame.setVisible(true);
}
}
However, when I run this program it looks like this:
It is not important that I use "Noto Emoji".
I just want to be able to show any Emoji in color, in any font, (and not mono chromed/black & white) in Java Swing.
Does anyone know what could be wrong? Or know how I can modify this example to show colored emojis?