Java Utililty Methods FontMetrics

List of utility methods to do FontMetrics

Description

The list of methods to do FontMetrics are organized into topic(s).

Method

StringlimitWithEllipsis(String str, Font font, int maxWidth, Component c)
If the string is longer than maxWidth, limit it to fit within maxWidth with an ellipsis (...).
FontMetrics metrics = c.getFontMetrics(font);
int strWidth = metrics.stringWidth(str);
if (strWidth <= maxWidth)
    return str;
for (int len = str.length() - 1; len > 0; len--) {
    String subStr = str.substring(0, len) + "...";
    if (metrics.stringWidth(subStr) <= maxWidth)
        return subStr;
...
DimensionmaxSize(Component component, String maxString)
max Size
FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
return new Dimension(Math.max(fontMetrics.stringWidth(maxString), component.getPreferredSize().width),
        fontMetrics.getHeight());
intmaxStringPixelWidth(String[] strings, FontMetrics fm)
max String Pixel Width
int maxWidth = 0;
int width;
String s;
for (int i = 0; i < strings.length; i++) {
    s = strings[i];
    width = stringPixelWidth(s, fm);
    if (maxWidth < width) {
        maxWidth = width;
...
Rectangle2DpaintString(String s, Graphics2D g2, Rectangle2D rect, float horizontal, float vertical)
horizontal is from -1 to 1 where -1 means text is aligned left, 0 centered and 1 right vertical likewise (-1 is bottom) shrink text if necessary!
return paintString(s, g2, rect, 0, 0, 0, horizontal, vertical, null);
voidrender(Graphics2D graphics, String str, Rectangle2D box, double xalign, double yalign)
render text
FontMetrics fm = graphics.getFontMetrics();
LineMetrics lm = fm.getLineMetrics(str, graphics);
float h = 0;
String[] lines = str.split("\\\n");
float[] ws = new float[lines.length];
for (int i = 0; i < lines.length; i++) {
    Rectangle2D r = fm.getStringBounds(lines[i], graphics);
    ws[i] = (float) r.getWidth();
...
voidsetComponentSize(Component component, int rows, int columns)
set Component Size
final FontMetrics fontMetrics = component.getFontMetrics(component.getFont());
final int width = fontMetrics.charWidth('m') * columns;
component.setPreferredSize(new Dimension(width, fontMetrics.getHeight() * rows));
voidsetSizedFont(Graphics g, String text, float maxFontSize, int maxWidth)
set Sized Font
if (g.getFont().getSize() != maxFontSize) {
    Font newFont = g.getFont().deriveFont(maxFontSize);
    g.setFont(newFont);
float currentSize = maxFontSize;
while (g.getFontMetrics().getStringBounds(text, g).getWidth() > maxWidth) {
    g.setFont(g.getFont().deriveFont(--currentSize));
StringshortenString(FontMetrics fm, String str, int maxWidth)
Trim a string from the left to fit within the specified width.
for (int i = str.length(); i > 0; i -= 5) {
    String shortedString = "..." + str.substring(str.length() - i);
    int width = fm.stringWidth(shortedString);
    if (width < maxWidth) {
        return shortedString;
return "";
...
String[]splitStringWithLength(String s, int length, FontMetrics fm)
This methods will split a String in different lines according to a maximum length.
List<String> result = new ArrayList<String>();
String[] array = split(s, ' ');
String current = array[0];
int currentLength = fm.stringWidth(current);
for (int i = 1; i < array.length; i++) {
    String tmp = array[i];
    currentLength += fm.stringWidth(" " + tmp);
    if (currentLength < length) {
...
ListsplitText(String text, String delim, FontMetrics fontMetrics, int width)
Split some text at word boundaries into a list of strings that take up no more than a given width.
List<String> result = new ArrayList<>();
final int len = text.length();
int i = 0, start;
String top = "";
Character d = null;
for (;;) {
    for (; i < len; i++) {
        if (delim.indexOf(text.charAt(i)) < 0)
...