Why has the displayed GUI different font style/rendering with graphics.drawString()
and a default JLabel with activated cleartype? And how can i fix it?
Asked
Active
Viewed 2,873 times
4

tomkpunkt
- 1,393
- 2
- 15
- 24
-
It is probably related to the options used to render the string. Try some alternatives using the source on [this thread](http://stackoverflow.com/questions/6444869/how-do-i-read-pixels-from-a-png-file-in-java/6445283#6445283). – Andrew Thompson Jun 23 '11 at 08:40
1 Answers
5
Try this
Graphics2D g2d = (Graphics2D)g;
Font font = new Font("Arial", Font.PLAIN, 12);
g2d.setFont(font);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.drawString("Hello World", 25, 100);

Sumit Singh
- 15,743
- 6
- 59
- 89

Nour E Ghtami
- 66
- 1
- 2
-
Note that although the proposed solution makes font rendering a bit better (or at least _different_) it is not turning on cleartype but only font antialiasing. Font antialiasing uses gray translucent pixels and give good picture on CRT displays. Cleartype uses translucent R and B pixels to give a sub-pixel preceived resolution on TFT displays. The TFTs may have different subpixel ordering, hence you can never get the same quality as JLabel. Except, if you subclass a JLabel (or a Graphics content initialized by Swing) and extract _all_ the antialiasing settings in place for your later use. – Gee Bee Mar 09 '18 at 02:28