• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Width & Height of the fonts(VERY URGENT)

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know that how to get width & Height of the string using String class methods or FontMetrics class methods

Plz explain me by an example,
Thanks in advance,
angela
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you would create a FontMetrics object for the current font with
<pre><h4>getFontMetrics(font object)</h4></pre>
Once you have your fontmetrics object, you can get various info about your font with these methods: (each returns an int of pixels)
<pre><h4>stringWidth(String)
charWidth(char)
getHeight()</h4>
</pre>
These methods would be called on a fontmetrics object. Here is an example:
<pre><h4>
Font f = new Font( "Courier", Font.BOLD, 20 ) ;
FontMetrics fm = getFontMetrics( f ) ;
componentname.setFont( f ) ;
String s = "All your base are belong to us" ;
int sWidth = fm.stringWidth( s ) ;
int sHeight = fm.getHeight() ;
</h4>
</pre>
Hope this is what you were looking for, if not, post your additional questions and we'll try again...

[This message has been edited by ryan burgdorfer (edited March 17, 2001).]
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ryan,
 
That is a really big piece of pie for such a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic