Java examples for 2D Graphics:Font
get Centering Start Point
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 Point getCenteringStartPoint(Graphics2D g, String string, Font font, Point center) { int x = (int) getCenteringStartX(g, string, font, center.x); int y = (int) getCenteringStartY(g, string, font, center.y); return new Point(x, y); }/*from w ww .j a v a2s. c o m*/ public static final float getCenteringStartX(Graphics2D g, String string, Font font, float centerX) { float textWidth = getTextWidth(g, string, font); return centerX - (textWidth / 2); } public static final float getCenteringStartY(Graphics2D g, String string, Font font, float centerY) { float textHeight = getTextHeight(g, string, font); return centerY - (textHeight / 2); } public static final float getTextWidth(Graphics2D g, String string, Font font) { return g.getFontMetrics(font).stringWidth(string); } public static final float getTextHeight(Graphics2D g, String string, Font font) { LineMetrics lm = font.getLineMetrics(string, g.getFontRenderContext()); return lm.getHeight(); } }