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

How to measure a character width with a Font?  RSS feed

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 42970
73
  • Mark post as helpful
  • send pies
  • 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.
 
Brian Cole
Author
Ranch Hand
Posts: 937
1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 42970
73
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 937
1
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!