2

This is what I have so far, but the text in the field isn't anti-aliased. I've tried googling it for a while but couldn't find any threads discussing it (much to my surprise). Does anyone know how to do this?

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);
     }
  }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • This question on StackOverflow maybe of help: [how to make JTextPane paint anti-aliased font?](http://stackoverflow.com/q/2266199/851432) – Jomoos Dec 21 '11 at 09:16
  • `g.fillRoundRect(0,0,fieldWidth,fieldHeight,4,4);` This sounds like the job for a [custom border](http://stackoverflow.com/a/8463742/418556) on a standard `JTextField`. – Andrew Thompson Dec 21 '11 at 09:29
  • If one simple line of code does the trick and it's easily efficient enough, why bother with a custom border? But a good point, I must admit. – rtheunissen Dec 21 '11 at 09:41
  • _why bother_ because in the long run you'll be most productive if you follow the winds of any framework you're using (instead of fighting it in detours). The wind of borders is ... using borders – kleopatra Dec 21 '11 at 09:51
  • Why not use the tools at our disposal to build the components we require? A border is also just another class, so in this case the efficiency / consequences of a `RoundRectangle` does the job very nicely. Not arguing though - there are many ways that you can do it. – rtheunissen Dec 21 '11 at 09:57

2 Answers2

1

I've found it helpful to use TextLayout, shown here, as one can condition the FontRenderContext with both isAntiAliased and usesFractionalMetrics.

The example's use of BufferedImage is coincidental.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

This is what I've decided to do until I can find a more elegant solution - works well.

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);
   }
}
rtheunissen
  • 7,347
  • 5
  • 34
  • 65
  • 1
    Not sure where GraphicUtils is from, but for that line the following can be done `g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);` – bobtheowl2 Feb 13 '12 at 15:32
  • It's a class I wrote to apply various `RenderingHints`, but thanks. :) – rtheunissen Feb 13 '12 at 18:45