2014-09-22 5 views
4

JButton 내에 Font Awesome을 사용하여 클릭 가능한 아이콘을 만들었지 만 작은 크기 일 때 결과 아이콘의 별칭이 나타납니다. 약간의 배경 지식과 마찬가지로 Font Awesome은 다운로드 가능한 ttf 파일 (글꼴 파일)이며 각 문자는 '확장 가능한 벡터 아이콘'입니다. Google에서 스택 오버플로 및 이전 답변을 살펴본 결과 JB30의 paintComponent 메서드를 재정 의하여 앤티 앨리어싱을 강제 적용하려고 시도했습니다. 글꼴은 30, 100, 200 크기에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(); 
    } 
} 

다음 이미지가 결과 글꼴 아이콘을 표시 :이 그러나 겉보기에 아무런 영향을 미치지 않습니다

enter image description here enter image description here enter image description here

내가 강제로 어떻게 방지 작은 글꼴 크기에 대한 앨리어싱?

업데이트 : 글꼴 Awesome이 아닌 내장 된 Java 글꼴을 사용하여 동일한 코드를 테스트했으며 똑같은 문제가 적용됩니다.

+1

['JDigit'] (http://stackoverflow.com/a/4151403/230513)에서 볼 수 있듯이'BufferedImage'를 스케일링 할 수 있습니다. – trashgod

답변

1

JLabel의 적절한 텍스트 힌트를 않습니다, 그래서 당신은 JButton의에서의 JLabel을 넣어 다음 JLabel의에서 글꼴 굉장 텍스트를 넣어 수 있습니다

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

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

로 해결할 수있는 문제를 성공적으로 리눅스에서 문제를 해결 작동 NimROD L & F. 다른 구성에서도 작동 할 것으로 기대되지만 YMMV.