I cant use unicode characters in java. I put the tree unicode in and it just comes out as a small box and a 4. It's in an applet, and I put the code in public void paint(Graphics g) anyway the code is below
g.drawString("\u1F334",150,150);
I cant use unicode characters in java. I put the tree unicode in and it just comes out as a small box and a 4. It's in an applet, and I put the code in public void paint(Graphics g) anyway the code is below
g.drawString("\u1F334",150,150);
From JLS 3.3:
A compiler for the Java programming language ("Java compiler") first recognizes Unicode escapes in its input, translating the ASCII characters \u followed by four hexadecimal digits to the UTF-16 code unit (§3.1) for the indicated hexadecimal value, and passing all other characters unchanged. Representing supplementary characters requires two consecutive Unicode escapes.
(emphasis added)
Specifically, you need the UTF-16 BE (big endian) encoding. For the palm tree, the sequence (as noted on the page you linked) is "\uD83C\uDF34"
.
The Unicode code point U+1F334 represents a "character" outside the java char range. Java uses the UTF-16 Unicode encoding, using 16 bits for a char. And U+1F334 goes in the 3-byte range.
You can do:
int[] codepoints = { 0x1F334 };
String emoji = new String(codepoints, 0, codepoints.length);
This will yield a String with a pair of chars, though using the escaping format of UTF-16, and not something simple like \u1F33\u0004
(wrong).
However if the font cannot represent that Unicode char, you will receive a small box, now with the six digits 01F / 334.
Font font = ...
if (!font.canDisplay(0x1F334)) {
s = s.replace(emoji, " :) ");
}
It is unlikely that every browser has a full Unicode font available. You could try usings Swing's HTML capability and use an embedded image in an HTML JTextPane or such.