如何在OS X上的Swing中启用LCD子像素抗锯齿?

时间:2013-06-19 17:34:37

标签: java macos swing

我被要求查看Swing中的旋钮,使文字尽可能清晰阅读。有没有办法在OS X上的Java 1.7+上的Swing应用程序中启用子像素文本抗锯齿? Java中是否还有其他任何可以使文本可读的设置?

观察:

  • 在OS X上的Java 1.7上,无论我做什么,所有文本都是灰色的。
  • 即使子像素渲染不起作用,RenderingHints.VALUE_TEXT_ANTIALIAS_ON也会比RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB更暗。为什么?
  • 默认情况下,Mac上的JComponent使用RenderingHints.VALUE_TEXT_ANTIALIAS_ON呈现(深灰色抗锯齿)。
  • 在Java 1.6上,-Dapple.awt.graphics.UseQuartz=true确实启用了LCD抗锯齿,但该设置对Oracle Java 1.7没有影响。
  • 在Linux上,所有子像素抗锯齿提示都能正常工作,JComponent默认使用HRGB抗锯齿。

Antialiasing Same screenshot at 300%

这是我使用的代码:

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);
            }
        };

        // Tell the JComponent to use the Graphics2d's text rendering hint
        // instead of the Toolkit default.
        label.putClientProperty(SwingUtilities2.AA_TEXT_PROPERTY_KEY, null);
        // To make all new JComponents use Graphics2d's rendering hint, do this:
        // UIManager.getDefaults().remove(SwingUtilities2.AA_TEXT_PROPERTY_KEY);

        return label;
    }
    public static void main(String[] argv) {
        System.out.format("Look and feel: %s%n", UIManager.getLookAndFeel());
        JFrame jframe = new JFrame();
        if (argv.length > 0)
            jframe.setTitle(argv[0]);
        jframe.getContentPane().setLayout(new BoxLayout(jframe.getContentPane(), BoxLayout.PAGE_AXIS));
        jframe.add(new JLabel("ABCDEabcde"));
        jframe.add(createJlabel("default", RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT));
        jframe.add(createJlabel("off", RenderingHints.VALUE_TEXT_ANTIALIAS_OFF));
        jframe.add(createJlabel("on", RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
        jframe.add(createJlabel("LCD_VRGB", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VRGB));
        jframe.add(createJlabel("LCD_VBGR", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_VBGR));
        jframe.add(createJlabel("LCD_HBGR", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HBGR));
        jframe.add(createJlabel("LCD_HRGB", RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB));
        jframe.pack();
        jframe.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

现在,OpenJDK 9 b70中的OSX可以使用正确的亚像素AA字体渲染:https://jdk9.java.net/download/。如果您的表面是半透明的,它就不可用。我已经写过如何启用它,例如。在我的博客上使用netbeans; http://www.nothome.com/2015/07/subpixel-font-rendering-with-netbeans.html或使用以下内容构建和修补:

hg clone http://hg.openjdk.java.net/jdk9/jdk9 jdk9
cd ./jdk9
bash ./get_source.sh 
cd jdk
curl http://cr.openjdk.java.net/~bae/8087201/9/webrev.00/jdk.patch | patch --dry-run -p1
curl https://gist.githubusercontent.com/iampivot/5677ccc024afe115d4bc/raw/f7b9acb1c6f356efa26f5772075a32dc899cd8ab/gistfile1.txt | patch -p0 
cd ..
sh ./configure --disable-warnings-as-errors
make all