0

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:

enter image description here

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
corgrath
  • 11,673
  • 15
  • 68
  • 99
  • 1
    Maybe this example: https://stackoverflow.com/a/29028982/131872 will help with your problem. It will verify if the font has been registered. – camickr Feb 20 '22 at 15:34
  • The only way to get multi-colored emojis in Java AWT/Swing would be to obtain the shape of them and color them explicitly using a `GradientPaint` (or some such) as offered by Java-2D. Java-FX offers a `WebView` component that (AFAIU) embeds a native browser. That might paint them more as you expect. Of course, if you just needed a single, solid color rendition of the character as in a red heart embedded in black text, that would be achievable using HTML in a Swing rich-text component (like `JLabel` of `JEditorPane`). – Andrew Thompson Feb 20 '22 at 18:15
  • @camickr - Yeah, its installed. – corgrath Feb 20 '22 at 18:49

0 Answers0