4

I'd like to process strings in Java which contain emojis, like this one:

enter image description here

When I put this string into a JLabel, the graphic result is this

enter image description here

How can I make it look like the first one inside the JLabel? Thank you.

sirdan
  • 1,018
  • 2
  • 13
  • 34

2 Answers2

5

Emoji are just unicode characters, and it's up to the font to specify a glyph to represent that character.

So, all you'd need to do is set the font of your JLabel to something with nice emoji glyphs, for example Noto Color Emoji from Google.

MarioDS
  • 12,895
  • 15
  • 65
  • 121
0

There are emoji unicode symbols, simply characters is a font. Those are like all characters monochrome, black. However you can replace them with an embedded image, it you make the text use a StyledDocument, HTML. Have your own set of emoji icons.

int[] emojis = { 0x1F600 };
String someText = new String(emojis, 0, emojis.length);

for (int emoji : emojis) {
    String emojiString = new String(new int[] { emoji }, 0, 1);
    if (!someText.contains(emojiString)) {
        continue;
    }
    String imgPath = String.format("/images/emoji%50x.png";
    byte[] imgContent = Files.readAllBytes(Paths.get(imgPath));
    String img = "<img src='data:image/png;base64,"
            + Base64.getEncoder().encode(imgContent)
            + "' width='16' height='16' alt='happy'>"; 
    someText = someText.replace(emojiString, img);
}

label.setText("<html>" + someText);

Not tested.

  • Unicode code points & strings
  • HTML & embedded image
  • Base64
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I don't have any set of emojis. Is it something I can find on the web or should I create it? – sirdan Nov 28 '17 at 11:51
  • 1
    Emoji should not be represented by images, that's the whole point: it's up to the font to render them. – MarioDS Nov 28 '17 at 11:52
  • @MarioDS but then they are monochrome, what the OP did not want. – Joop Eggen Nov 28 '17 at 11:53
  • Maybe that for Linux exist free emoji icons. Have fun. And do not forget the alt text, for the meaning ;) – Joop Eggen Nov 28 '17 at 11:54
  • @JoopEggen go to https://unicode.org/emoji/charts/full-emoji-list.html, select any you like from the "Browser" column and copy-paste it into a word document. What do you see? What font does it use? On my system it uses "Segoe UI Emoji" and displays a nicely colored Emoji. It's definitely not an image. This is how Emoji are supposed to work. You can install any font you like that supports emoji and switch it around in Word and you'll see the "drawing" of that character change as you change the font. – MarioDS Nov 28 '17 at 11:56
  • @MarioDS I feel old, your are right. +1 from me. For delivery of the font with the application, say as resource file in the jar, use `GraphicsEnvironment.registerFont`. I hope the font rendering in swing is capable of colors. – Joop Eggen Nov 28 '17 at 12:07
  • @JoopEggen Please feel free to expand my answer to add information on how to manage fonts in Java and how to set the font on a `JLabel` element, I have no experience with that. – MarioDS Nov 28 '17 at 12:28