waba.fx
Class FontMetrics

java.lang.Object
  |
  +--waba.fx.FontMetrics

public final class FontMetrics
extends java.lang.Object

FontMetrics computes font metrics including string width and height.

FontMetrics are usually used to obtain information about the widths and heights of characters and strings when drawing text on a surface.

Here is an example that uses FontMetrics to get the width of a string:

 ...
 Font font = Font.getFont("Tiny", true, Font.BIG_SIZE);
 FontMetrics fm = font.fm;
 String s = "This is a line of text.";
 int stringWidth = fm.getTextWidth(s);
 ...
 


Field Summary
 int ascent
          READ-ONLY member: indicates the average height of this font from the baseline to up.
 int descent
          READ-ONLY member: indicates the average height of this font from the baseline to down.
protected  Font font
           
 int height
          READ-ONLY member: total height of this font (ascent+descent).
 int leading
          READ-ONLY member: distance between two lines of text
 
Constructor Summary
FontMetrics(Font font)
          Deprecated. Don't use this constructor; access the myFont.fm member instead.
 
Method Summary
 int getCharWidth(char c)
          Returns the width of the given character in pixels.
 int getMaxWidth(java.lang.String[] names, int start, int count)
          Returns the maximum text width from the given list of names.
 int getTextWidth(char[] chars, int start, int count)
          Returns the width of the given text in pixels.
 int getTextWidth(java.lang.String s)
          Returns the width of the given text string in pixels.
 int stringWidth(java.lang.String s)
          Calls getTextWidth, just provided to be more compatible with JDK.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

font

protected Font font

ascent

public int ascent
READ-ONLY member: indicates the average height of this font from the baseline to up.

descent

public int descent
READ-ONLY member: indicates the average height of this font from the baseline to down.

leading

public int leading
READ-ONLY member: distance between two lines of text

height

public int height
READ-ONLY member: total height of this font (ascent+descent).
Constructor Detail

FontMetrics

public FontMetrics(Font font)
Deprecated. Don't use this constructor; access the myFont.fm member instead.

Constructs a font metrics object referencing the given font.
Method Detail

getCharWidth

public int getCharWidth(char c)
Returns the width of the given character in pixels.

getTextWidth

public int getTextWidth(java.lang.String s)
Returns the width of the given text string in pixels.

stringWidth

public int stringWidth(java.lang.String s)
Calls getTextWidth, just provided to be more compatible with JDK.
Since:
SuperWaba 5.0

getTextWidth

public int getTextWidth(char[] chars,
                        int start,
                        int count)
Returns the width of the given text in pixels.
Parameters:
chars - the text character array
start - the start position in array
count - the number of characters

getMaxWidth

public int getMaxWidth(java.lang.String[] names,
                       int start,
                       int count)
Returns the maximum text width from the given list of names. It is useful to compute the best x position to place the controls, in order to align them in the container. For example:
 String []labels = {"Name","Age","Address"};
 int xx = this.fm.getMaxWidth(labels, 0, labels.length);
 add(new Label(labels[0]), LEFT, AFTER);
 add(edName = new Edti(""),xx, SAME);
 ...
 
Parameters:
start - The starting index
count - The number of elements to check.
Since:
SuperWaba 5.72