2

in the a swing app i'm rendering text with a custom JComponent, using Graphics.drawString(). here is a sample:
aa text http://img525.imageshack.us/img525/4928/drawstringsample.jpg
in that same app, i'm rendering text using a JTextPane. here is a sample:
alt text http://img28.imageshack.us/img28/1134/jtextpanesample.jpg

can you notice how the lower sample is a little 'smudged'? well, i can't figure out how to make it look like the upper sample.

thanks, asaf :-)


update:

  • System.setProperty("awt.useSystemAAFontSettings","false") and "lcd" too aren't working.
  • ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) in paint() isn't working
  • putClientProperty(sun.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE) gives java.lang.ClassCastException: java.lang.Boolean cannot be cast to sun.swing.SwingUtilities2$AATextInfo
Asaf
  • 2,480
  • 4
  • 25
  • 33

3 Answers3

6

This will result in an antialiased font in a JLabel. Make sure you call super.paintComponent(g); after setting the RenderingHints.

JLabel lblFont = new JLabel(){

            @Override
            public void paintComponent(Graphics g) {
                Graphics2D graphics2d = (Graphics2D) g;
                graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                        RenderingHints.VALUE_ANTIALIAS_ON);
                super.paintComponent(g);
            }
        };
Lukas Glowania
  • 498
  • 6
  • 12
  • you're answering 'cause you've tried it and that solved your problem? – Asaf Dec 09 '10 at 16:52
  • yes, in my case i could switch anti-aliasing on and off with this approach. – Lukas Glowania Mar 17 '11 at 14:35
  • @b1tk0pf Combining your aproach together with the following code makes for even smoother text - `jTextPane1.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);` – Igor Dec 20 '12 at 23:53
  • I'd prefer this solution as it antialiases text when drawing on any graphics (in my case BufferedImage), but the "putClientProperty" would be better for Swing applications. (Probably, I still hate the idea of using sun.* packages) – Jakub Jun 19 '13 at 06:23
  • It's not working on JTextPane whereas SwingUtilities2.AA_TEXT_PROPERTY_KEY does. – ron190 Jun 07 '16 at 17:53
5

putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);

Asaf
  • 2,480
  • 4
  • 25
  • 33
1

If you want your result to look like the the upper sample, then you want to disable anti-aliasing.

The first sample in your question has anti-aliasing disabled and the second sample has it enabled.

According to http://mindprod.com/jgloss/antialiasing.html the following code should help:

jtextArea.putClientProperty(com.sun.java.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE);

Notice that the reference to com.sun.java.* will make your application non-portable to non-Sun JVMs (and possibly to different versions of Sun JVMs).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • tried System.setProperty("awt.useSystemAAFontSettings","false") and "lcd" too, but w/o results. – Asaf Feb 15 '10 at 14:11
  • tried ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF) in paint(), but w/o results. – Asaf Feb 15 '10 at 14:12
  • and putClientProperty(sun.swing.SwingUtilities2.AA_TEXT_PROPERTY_KEY, Boolean.TRUE) gives java.lang.ClassCastException: java.lang.Boolean cannot be cast to sun.swing.SwingUtilities2$AATextInfo – Asaf Feb 15 '10 at 14:15