Title: Java AWT Graphics2D drawString method does not work with arabic text
Tags: java;fonts;awt
Question: I have a requirement to draw using the method .drawString from Graphics2D (java.awt) a text in arabic.
But when I use the method using the following code it will just show a bunch of empty squares
BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.drawString(&quot;مرحبا هذا نص عربي&quot;,
xStartText, endY + smallRectHeight);
The resulted output
Can somebody help? I've search for code snippets to solve this problem but I could not find anything out there?
Comment: @Berto99 I recently faced this problem too, unfornunately could not solve it yet. But I tried encoding the text in UTF-8 and it did not solve the issue.

Comment: maybe try `graphics.setFont(new Font("Arial", Font.PLAIN, 24));` or with any font that can display that characters - posted code worked perfectly for me (Java 16, default "Dialog" or "Monospaced" or "Aria" fonts)

Comment: Have you tried using a JFrame to show text?

Comment: try encoding the string with a different standard

Comment: @Triple3XH have you tried using the encoded string instead of the "original" one? for example using https://www.browserling.com/tools/utf8-encode

Here is the accepted answer: Unless supplying a font (with the app.) that can support Arabic characters, best to find a suitable mvalentine@example.com.
I supplied Noto Sans Arabic as a font ..
Supplying the font is the most robust solution IMO. A couple of tips: See also the accepted answer to How do you import a font? &amp; note the font will become an embedded resource by the time the app. is deployed. The info page for embedded resources provides tips on how to get a URL pointing to the font.
.. and it worked as desired.
Glad you got it sorted.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:06/02/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><fonts><awt>', 'Title': 'Java AWT Graphics2D drawString method does not work with arabic text', 'ViewCount': 86, 'id': 67810131, 'user_AboutMe': None, 'user_AccountId': 20188880.0, 'user_DisplayName': 'JohnM', 'user_Reputation': 13.0, 'user_Views': 1.0, 'user_id': 14807584.0}}