I'd like to process strings in Java which contain emojis, like this one:
When I put this string into a JLabel, the graphic result is this
How can I make it look like the first one inside the JLabel? Thank you.
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.
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.