Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a unicode character in java?

Tags:

java

unicode

Short of copying the character to the clipboard and pasting it inside my string, is there a way to draw a greek letter (or for that matter, any unicode character)? I know the code for the character I am trying to draw is U+03F4 according to here. I have tried the following:

g.drawString("U+03F4", 100, 100); // works as a string literal as it should
g.drawString("\U+03F4", 100, 100); // error: Illegal escape character
g.drawString("\\U+03F4", 100, 100); // thought I could trick it. Just draws "\U+03F4"

I saw in this question that the 'u' was lowercase but that didn't make any difference.

Why would this not be working?

like image 362
BitNinja Avatar asked May 08 '26 23:05

BitNinja


2 Answers

The general format for Java unicode escapes is

UnicodeEscape:
    \ UnicodeMarker HexDigit HexDigit HexDigit HexDigit

UnicodeMarker:
    u
    UnicodeMarker u

Therefore correct sequence for this character in Java is \u03F4.

http://www.fileformat.info/info/unicode/char/03F4/index.htm

like image 163
Matt Ball Avatar answered May 11 '26 12:05

Matt Ball


I think the correct way to represent a unicode character is as follows:

g.drawString("\u03f4", 100, 100);
like image 24
Cᴏʀʏ Avatar answered May 11 '26 11:05

Cᴏʀʏ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!