Matching Fonts Using Java FontMetrics and Graphics2D Generated Text

Posted on 2011-05-05
Last Modified: 2012-05-11
I'm trying to match my generated text font to that of a design composition (PSD file).  In the composition, the font is danteMt, but when I set that below my image text doesn't match. See attached images.  

private String writeTextThumbDream(File file, String text, Long userId) throws Exception{
			Image image =;
			BufferedImage img=bufferImage(image);
			Graphics2D g = (Graphics2D) img.getGraphics();
			[b]Font fnt=new Font("danteMt",1,20);[/b]
			Color fntC = new Color(0, 102, 153);      
			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

Design Comp Image - font I'm trying to match My Dynamically created image that doesn't match
Question by:cgray1223
    LVL 92

    Expert Comment

    you'll need to have the font file to achieve that
    LVL 92

    Accepted Solution


    Font f = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("dante.ttf") );

    Author Comment

    Is that for all fonts or just certain ones?  I also tried Georgia and it really didn't change anything...see attached.. Georgia font
    LVL 92

    Assisted Solution

    any fonts not available to java

    see the following for how to find available fonts
    (remove the chinese check)

    Author Comment

    Thanks, Georgia is a font there.  I think I may have a bug or I've not implemented something right.  When I use Font fnt=new Font("Wingdings-Regular",Font.PLAIN,80); It doesn't look like wingdings, see attached.  That is a supported java font...any ideas? wingdings font
    LVL 92

    Expert Comment

    Check you are using the right name
    use the link i posted above to list all fonts to check names

    Author Comment

    some reason my browser cached it.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now