JButton文本的消除锯齿

问题描述:

我在JButton中使用Font Awesome来创建可点击图标,但是如果尺寸较小,结果图标会显示为别名。正如一点背景,Font Awesome是一个可下载的ttf文件(字体文件),其中每个字符是一个'可缩放矢量图标'。看过谷歌以前的答案和堆栈溢出后,我试图通过覆盖JButton的paintComponent方法来强制执行反走样;然而,这似乎已没有任何影响:JButton文本的消除锯齿

import java.awt.*; 
import java.io.File; 
import java.io.IOException; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

public class Test extends JFrame{ 

    public Test(){ 
     Font fontAwesome = null; 
     try { 
      fontAwesome = Font.createFont(Font.TRUETYPE_FONT, new File("font-awesome-4.2.0\\fonts\\fontawesome-webfont.ttf")); 
      fontAwesome = fontAwesome.deriveFont(Font.PLAIN, 100); 
     } catch (FontFormatException | IOException e) { 
      e.printStackTrace(); 
     } 

     JButton iconButton = new JButton("\uf0a8"){ 
      @Override 
      public void paintComponent(Graphics g) { 
       Graphics2D graphics2d = (Graphics2D) g; 
       graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
       //graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 
       //graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); 
       //graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
       super.paintComponent(graphics2d); 
      } 
     }; 

     iconButton.setFont(fontAwesome); 
     iconButton.setFocusPainted(false); 

     this.add(iconButton); 
     this.setVisible(true); 
     this.pack(); 
    } 

    public static void main(String[] args){ 
     new Test(); 
    } 
} 

下图显示了所产生的字体图标在字体大小30,100和200:

enter image description hereenter image description hereenter image description here

如何强制抗小字体大小的锯齿?

更新:我测试了相同的代码使用内置的java字体而不是字体真棒,并且完全相同的问题适用。

+1

您可以尝试缩放'BufferedImage',如['JDigit'](http://*.com/a/4151403/230513)所示。 – trashgod 2014-09-23 02:52:12

的JLabel不正确的文本提示,所以你可以把在JLabel的字体真棒文本,然后把一个JLabel在一个JButton:

JLabel iconLabel = new JLabel("\uf0a8"); 
    iconLabel.setFont(fontAwesome); 

    JButton iconButton = new JButton(); 
    iconButton.add(iconLabel); 

解决了 成功工作解决问题的Linux上F.似乎合理地期望它可以在其他配置上工作,但YMMV也是如此。