• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

Font Sharpness with Java Graphics Class

Hello,

I'm writing some text on top of an image and the font isn't very sharp.  Is there some settings either with the Graphics 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.  See attached two images.  I need to create both a large and thumbnail.  
public class ImageCreator{

public void createImage(String text){
     Graphics g = img.createGraphics(); //img is a BufferedImage read in from file system
     [b]Font fnt=new Font("Dante",1,20);
     Color fntC = new Color(4, 4, 109);       
     g.setColor(fntC);
     g.setFont(fnt);[/b]
     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);
}

}  

Open in new window

                     
                          large image with text thumbnail image with text
0
cgray1223
Asked:
cgray1223
  • 9
  • 5
  • 2
  • +1
1 Solution
 
objectsCommented:
0
 
cgray1223Author Commented:
I changed my object to Graphics2D and added the below, but no change.  In one example you sent its applying the antialiasing in the Graphics2D drawImage method.  I wonder if antialiasing doesn't apply when writing text on top of an existing image using the Graphics2D drawString method?

      Graphics2D g = img.createGraphics();
                  g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                              RenderingHints.VALUE_ANTIALIAS_ON);
        //continue of previously posted code
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
no change seems strange, check you aren't looking at previously created images
0
 
objectsCommented:
you're not resizing the text are you?
That would get messy, better to create two images of different sizes.
0
 
objectsCommented:
if you are resing then make sure you're using appropriate interpolation
http://helpdesk.objects.com.au/java/how-do-i-scale-a-bufferedimage
0
 
cgray1223Author Commented:
I'm creating two images one large and one thumb.
0
 
for_yanCommented:

Here they write that anti-aliasing may sometimes not be visible
depneding on LookAndFeel:
http://forums.java.net/node/686261
0
 
objectsCommented:
> Here they write that anti-aliasing may sometimes not be visible
> depneding on LookAndFeel:

thats not relevant for rendering images
0
 
cgray1223Author Commented:
looks like the effect worked on the larger image but the thumb is grainy.  Any suggestions? large image, looks good thumb image, blury
0
 
for_yanCommented:


Anti-aliasing Pros and Cons
Pros

    * Makes fonts look smoother
    * Rounded edges look round
    * Type is easier to read (for some) because it looks more like what printed type looks like
    * It's just plain prettier (some would argue)

Cons

    * Small fonts become too fuzzy to read  <-----------------------------------------------------
     * Sharp edges may be fuzzy and not precise
    * You can't print anti-aliased text as it comes out blurred
    * Images are generally larger
    * Type is easier to read (for some) because the blurring is reduced and the fonts are clear
0
 
objectsCommented:
> looks like the effect worked on the larger image but the thumb is grainy.  Any suggestions?

java is not the best at rendering small images
how are you creating the thumbnail, is it the same as what you posted?
0
 
objectsCommented:
> looks like the effect worked on the larger image but the thumb is grainy.  Any suggestions?

its better than it was so antialiasing has certainly helped
0
 
cgray1223Author Commented:
@Objects, yeah that's how I'm doing the thumb, same as my original post.
0
 
CEHJCommented:
You might be better off using vector graphics - scaling works much better.

Can you post just the background image too?
0
 
objectsCommented:
What scaling?
0
 
cgray1223Author Commented:
here is the background image thumb original pic
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now