get Centering Start X - Java 2D Graphics

Java examples for 2D Graphics:Font

Description

get Centering Start X

Demo Code


import org.apache.log4j.Logger;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.util.*;
import java.util.regex.Pattern;

public class Main{
    public static final float getCenteringStartX(Graphics2D g,
            String string, Font font, float centerX) {
        float textWidth = getTextWidth(g, string, font);
        return centerX - (textWidth / 2);
    }//from   ww w  . j av  a2  s .c o  m
    public static final float getTextWidth(Graphics2D g, String string,
            Font font) {
        return g.getFontMetrics(font).stringWidth(string);
    }
}

Related Tutorials