List of usage examples for java.awt FontMetrics getWidths
public int[] getWidths()
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; }