Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine the pixel width of characters

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me how to determine the character width in pixels.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming that this is in the context of AWT or Swing code. You can use the FontMetrics class to determine the height and width of a given string (or a single character) for a given font.
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please send me a piece of code to get the character size in pixel for any font. Kindly do the needfull.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would anyone here write the code for you? Don't you want to learn something?

You can get a FontMetrics object by calling getFontMetrics() or getFontMetrics(Font) on a Graphics2D object. If you look through the javadocs of the FontMetrics class you'll see all kinds of interesting methods that should be of value for your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!