Matching Fonts Using Java FontMetrics and Graphics2D Generated Text

Posted on 2011-05-05
Medium Priority
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 = ImageIO.read(file);
			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
  • 4
  • 3
LVL 92

Expert Comment

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

Accepted Solution

objects earned 2000 total points
ID: 35703415

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

Author Comment

ID: 35703472
Is that for all fonts or just certain ones?  I also tried Georgia and it really didn't change anything...see attached.. Georgia font
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 92

Assisted Solution

objects earned 2000 total points
ID: 35703491
any fonts not available to java

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

Author Comment

ID: 35703682
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

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

Author Comment

ID: 35703717
some reason my browser cached it.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

850 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