Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine the pixel width of characters  RSS feed

 
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!