Example usage for java.awt FontMetrics getWidths

List of usage examples for java.awt FontMetrics getWidths

Introduction

In this page you can find the example usage for java.awt FontMetrics getWidths.

Prototype

public int[] getWidths() 

Source Link

Document

Gets the advance widths of the first 256 characters in the Font .

Usage

From source file:Main.java

/**
 * Calculates the average character width for the given {@link Component}.
 * This can be useful as a scaling factor when designing for font scaling.
 *
 * @param component the {@link Component} for which to calculate the
 *        average character width./*from ww  w. j  a v  a  2s  .  c o  m*/
 * @return The average width in pixels
 */
public static float getComponentAverageCharacterWidth(Component component) {
    FontMetrics metrics = component.getFontMetrics(component.getFont());
    int i = 0;
    float avgWidth = 0;
    for (int width : metrics.getWidths()) {
        avgWidth += width;
        i++;
    }
    return avgWidth / i;
}