I was asked to find pens in Swing so that the text was as readable as possible. Is it possible to enable anti-aliasing of subpixel text in a Swing application on Java 1.7+ in OS X? Are there any other settings in Java for reading text?
remarks:
- In Java 1.7 on OS X, no matter what I do, all text is gray.
- Even if subpixel rendering does not work,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON darker than RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB . Why? - By default, JComponent on Mac is rendered using
RenderingHints.VALUE_TEXT_ANTIALIAS_ON (dark gray anti-aliasing). - In Java 1.6,
-Dapple.awt.graphics.UseQuartz=true activates LCD anti-aliasing, but this option does not affect Oracle Java 1.7. - On Linux, all sub-pixel smoothing hints work correctly, and JComponent uses HRGB anti-aliasing by default.
300% same screenshot
Here is the code I used:
import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; import sun.swing.SwingUtilities2; public class TextAntialiasDemo { public static JLabel createJlabel(String name, final Object textAntialiasing) { JLabel label = new JLabel("ABCDEabcde text_antialiasing=" + name) { private static final long serialVersionUID = 1L; @Override public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D)g.create(); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textAntialiasing); super.paintComponent(g2); } };
source share