2011-12-21 5 views
2

이것은 내가 지금까지 가지고있는 것이지만, 필드의 텍스트는 앤티 앨리어스가 아닙니다. 나는 그것을 잠시 동안 인터넷 검색을 시도했지만 어떤 스레드가 그것에 대해 (내 놀랍게도) 많은 논의를 찾을 수 없습니다. 누구든지이 작업을 수행하는 방법을 알고 있습니까?자바 : JTextField에서 텍스트 앤티 앨리어싱을 사용하는 방법?

public class SearchField extends JTextField{ 
    public SearchField(){ 
     super(); 
     this.setOpaque(false); 
     this.setPreferredSize(new Dimension(fieldWidth, fieldHeight)); 
     this.setBorder(new EmptyBorder(4,8,4,8)); 
     this.setFont(fieldFont); 
    } 

    public void paintComponent(Graphics paramGraphics){ 
      Graphics2D g = (Graphics2D) paramGraphics; 
      g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
          RenderingHints.VALUE_TEXT_ANTIALIAS_ON); 

      g.setColor(ColorConstants.LIGHT_GRAY); 
      g.fillRoundRect(0,0,fieldWidth,fieldHeight,4,4); 
      super.paintComponent(g); 
    } 
    } 
+0

어쩌면 도움에 StackOverflow에이 질문 : [? JTextPane의 앤티 앨리어싱 글꼴을 페인트 만드는 방법] (http://stackoverflow.com/q/2266199/851432) – Jomoos

+0

'g.fillRoundRect (0,0 , fieldWidth, fieldHeight, 4,4);'이것은 표준'JTextField'에서 [custom border] (http://stackoverflow.com/a/8463742/418556)에 대한 작업처럼 들립니다. –

+0

코드의 간단한 한 줄이 트릭을 수행하고 쉽게 충분히 효율적이라면 왜 커스텀 테두리가 귀찮을까요? 그러나 좋은 점은 인정해야합니다. – rtheunissen

답변

0

더 우아한 솔루션을 찾을 수있을 때까지이 작업을 수행했습니다.

private class SearchField extends JTextField{ 

    private final int fieldWidth = 375; 
    private final int fieldHeight = 30; 
    private final Font fieldFont = FontLoader.getCustomFont("Gotham-Bold.ttf", 15); 
    private final Color foreground = ColorConstants.SEARCH_FIELD_FOREGROUND; 
    private final Color background = ColorConstants.SEARCH_FIELD_BACKGROUND; 

    public SearchField(){ 
     super(); 
     this.setOpaque(false); 
     this.setPreferredSize(new Dimension(fieldWidth, fieldHeight)); 
     this.setBorder(new EmptyBorder(5,5,5,5)); 
     this.setFont(fieldFont); 
     this.setForeground(new Color(0,0,0,0)); 
     this.setSelectedTextColor(new Color(0,0,0,0)); 
    } 

    @Override 
    public void paintComponent(Graphics paramGraphics){ 
     Graphics2D g = (Graphics2D) paramGraphics.create(); 
     GraphicUtils.enableAntiAliasing(g); //RenderingHints 
     g.setColor(background); 
     g.fillRoundRect(0, 0, fieldWidth, fieldHeight, 4, 4); 
     super.paintComponent(g); 
     g.setColor(foreground); 
     g.drawString(this.getText(), 5, 20); 
    } 
} 
+1

GraphicUtils의 출처는 모르지만 그 행에 대해서는 다음을 할 수 있습니다. g.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ' – bobtheowl2

+0

다양한 RenderingHints를 적용하기 위해 작성한 클래스지만 감사합니다. :) – rtheunissen

1

나는 하나 isAntiAliasedusesFractionalMetrics 모두와 함께 FontRenderContext 컨디셔닝 수, here을 보여, 그것은 도움이 TextLayout을 사용하는 것으로 나타났습니다.

이 예제에서는 BufferedImage을 사용하는 것이 우연입니다.

관련 문제