• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to measure a character width with a Font?

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to measure a character width (say 'A) in pixel with a Font in Java.
I just need to print the character width on console (not a GUI program).

i think need to call this method in Font, but how can I create the class 'FontRenderContext'?

LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc)
Returns a LineMetrics object created with the specified arguments.

Thank you.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ying lam:

I am trying to measure a character width (say 'A) in pixel with a Font in Java.



Check out the FontMetrics class. It has lots of useful methods. In your case you might want to use:


[ April 12, 2008: Message edited by: Maneesh Godbole ]
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. But FontMetrics constructor is a protected method. How can I get a FontMetrics of a Font class?

Thank you.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would use the java.awt.Toolkit.getFontMetrics(Font) method.
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

But the javadoc said it is a deprecated method. And suggests me to use getLineMetrics(). And that goes back to my original question: how can I create the class 'FontRenderContext'?

/**
* Gets the screen device metrics for rendering of the font.
* @param font a font
* @return the screen metrics of the specified font in this toolkit
* @deprecated As of JDK version 1.2, replaced by the <code>Font</code>
*method <code>getLineMetrics</code>.
* @see java.awt.font.LineMetrics
* @see java.awt.Font#getLineMetrics
* @see java.awt.GraphicsEnvironment#getScreenDevices
*/
@Deprecated
public abstract FontMetrics getFontMetrics(Font font);
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I have tried the following code on linux on java 6.

And it returns '1' for character 'A' for all the fonts on my system.
I am not sure why.

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how can I create the class 'FontRenderContext'?


From the javadocs of FontRenderContext:

Typically, instances of FontRenderContext are obtained from a Graphics2D object. A FontRenderContext which is directly constructed will most likely not represent any actual graphics device, and may lead to unexpected or incorrect results.

 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ying lam:
And I have tried the following code on linux on java 6.

And it returns '1' for character 'A' for all the fonts on my system.
I am not sure why.



If you print out font.getSize() in addition to font.getFontName()
I bet you'll find that all the fonts are tiny, perhaps 1-point.

You don't say what font size you are looking to measure, but if
you want the width of the character 'A' at 12-point sizes, make
this one change:

FontMetrics fontMetrics = toolkit.getFontMetrics(allFonts[i].deriveFont(12f));
 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Can you please tell me how can I find what are the available sizes for the font?

In your example, you show 12.0f, how can I find out what are the available size of a particular font?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you please tell me how can I find what are the available sizes for the font?



See this tread for why this question most likely is irrelevant.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
You would use the java.awt.Toolkit.getFontMetrics(Font) method.


If you have access to a Graphics object, for instance when painting or printing, you can use one of that object's getFontMetrics methods as well.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ying lam:
Thank you. Can you please tell me how can I find what are the available sizes for the font?

In your example, you show 12.0f, how can I find out what are the available size of a particular font?



Unless you're running on operating systems so old they can't even run a decent JDK, fonts are available in any size. The size isn't a property of the font (lower-case 'f') itself but is a parameter of how it is used. Less than 1.0f should be fine (but usually hard to read) and well larger than 200.0f should also be fine.

Your question is sort of like asking what are the available sizes for instances of the BufferedImage class.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic