Displaying Image and Text in Java 2D

Thread Solved

Join Date: Aug 2007
Posts: 790
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Displaying Image and Text in Java 2D

 
0
  #1
Mar 14th, 2008
Hi all,

I am wanting to display an image that is aligned so that the centre of a string of text is in line with the centre of the image. I know the image's dimensions and the length of the string, as well as the font I am using to draw the text, but I can't work out how to calculate the coordinates of the centre of the string. I am using the java.awt.Graphics.drawString and drawImage methods to draw each part of my object. If I have set my graphics component's font to have a size of 12 say, does this mean that each character takes up 12 x 12 pixels?

Thanks in advance for any suggestions,
darkagn
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,432
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 507
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Displaying Image and Text in Java 2D

 
1
  #2
Mar 15th, 2008
Originally Posted by darkagn View Post
...I am using the java.awt.Graphics.drawString and drawImage methods to draw each part of my object. If I have set my graphics component's font to have a size of 12 say, does this mean that each character takes up 12 x 12 pixels?
No, it's a little more complicated than that, but still easily available. You'll need to call Graphics.getFontMetrics() to obtain the font metrics for the current font. FontMetrics provides methods for obtaining dimensions of a particular string for that font and graphics context, such as getHeight() and stringWidth(java.lang.String). That should be all you need to center up any given string on your image.

Edit: You can find more detailed info on FontMetrics and handling text in the graphics context if you need it in the 2D graphics tutorial: http://java.sun.com/docs/books/tutor...uringtext.html
Last edited by Ezzaral; Mar 15th, 2008 at 12:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 790
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 109
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Displaying Image and Text in Java 2D

 
0
  #3
Mar 16th, 2008
Thanks Ezzaral Will try this out and repost if I still have probs.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC