Class FontMetrics


  • public final class FontMetrics
    extends java.lang.Object
    FontMetrics 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.stringWidth(s);
     ...
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      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).
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int charWidth​(char c)
      Returns the width in pixels of the given character.
      int charWidth​(java.lang.StringBuffer s, int i)
      Returns the width in pixels of the char located at the given index in the StringBuffer.
      int getMaxWidth​(java.lang.String[] names, int start, int count)
      Returns the maximum text width from the given list of names.
      int sbWidth​(java.lang.StringBuffer s)
      Returns the width in pixels of the given StringBuffer.
      int sbWidth​(java.lang.StringBuffer s, int start, int count)
      Returns the width in pixels of the given StringBuffer range.
      int stringWidth​(char[] chars, int start, int count)
      Returns the width in pixels of the given char array range.
      int stringWidth​(java.lang.String s)
      Returns the width in pixels of the given text string.
      • 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.
      • height

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

      • charWidth

        public int charWidth​(char c)
        Returns the width in pixels of the given character.
      • stringWidth

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

        public int stringWidth​(char[] chars,
                               int start,
                               int count)
        Returns the width in pixels of the given char array range.
        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 Edit(""),xx, SAME);
         ...
         
        Parameters:
        start - The starting index
        count - The number of elements to check.
        Since:
        SuperWaba 5.72
      • sbWidth

        public int sbWidth​(java.lang.StringBuffer s)
        Returns the width in pixels of the given StringBuffer. This method is used to preserve memory, since it avoids the creation of a String just to get the width.
        Since:
        TotalCross 1.0
      • sbWidth

        public int sbWidth​(java.lang.StringBuffer s,
                           int start,
                           int count)
        Returns the width in pixels of the given StringBuffer range. This method is used to preserve memory, since it avoids the creation of a String just to get the width.
        Since:
        TotalCross 1.0
      • charWidth

        public int charWidth​(java.lang.StringBuffer s,
                             int i)
        Returns the width in pixels of the char located at the given index in the StringBuffer.
        Since:
        TotalCross 1.0