1

I'm writing some text on top of an existing image and the font isn't very sharp. Is there some settings either with the Graphics2D or Font classes that help make fonts look nicer when writing text on top of images? The Dante font doesn't come out as Dante when I write it. I've tried to use antialiasing but it had no effect (see setRenderingHint). The images came out the same with or without the RenderingHint set. Any suggestions?

public class ImageCreator{

public void createImage(String text){
     Graphics2D g = img.createGraphics(); //img is a BufferedImage read in from file system
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
     Font fnt=new Font("Dante",1,20);
     Color fntC = new Color(4, 4, 109);       
     g.setColor(fntC);
     g.setFont(fnt);
     Dimension d = new Dimension(200, 113);
     drawCenteredString(text, d.width, d.height, g);
}
public static void drawCenteredString(String s, int w, int h, Graphics g) {
      FontMetrics fm = g.getFontMetrics();
      int x = (w - fm.stringWidth(s)) / 2;
      int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
      g.drawString(s, x, y);
}

}  
c12
  • 9,557
  • 48
  • 157
  • 253

1 Answers1

2

Use TextLayout, as shown here.

Addendum: The advantage of TextLayout is that RenderingHints may be applied to the FontRenderContext.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for the response trashgod, but that actually made it look worse – c12 May 04 '11 at 05:44
  • That's disappointing. What result did you get form the example I cited? Did you apply your `RenderingHints` to the `FontRenderContext` of the `TextLayout`? Can you update you question with the new code? – trashgod May 04 '11 at 15:57
  • the problem was the font was not available on the JVM. – c12 May 11 '11 at 18:07
  • Interesting. Was the system substituting a different font, presumably with different metrics? – trashgod May 11 '11 at 18:19