Link to home
Start Free TrialLog in
Avatar of cgray1223
cgray1223

asked on

Matching Fonts Using Java FontMetrics and Graphics2D Generated Text

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();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
			[b]Font fnt=new Font("danteMt",1,20);[/b]
			Color fntC = new Color(0, 102, 153);      
			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);
	  }

Open in new window

User generated image User generated image
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you'll need to have the font file to achieve that
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cgray1223
cgray1223

ASKER

Is that for all fonts or just certain ones?  I also tried Georgia and it really didn't change anything...see attached.. User generated image
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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? User generated image
Check you are using the right name
use the link i posted above to list all fonts to check names
some reason my browser cached it.