Java examples for 2D Graphics:Font
Given a character and its font, calculate the center point offset for that character.
/*/* w w w.ja va 2 s .c o m*/ * Copyright (c) 2001-2002 Regents of the University of California. * All rights reserved. * * This software was developed at the University of California, Irvine. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the University of California, Irvine. The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ //package com.java2s; import java.awt.Graphics; import java.awt.Point; import java.awt.Font; import java.awt.FontMetrics; public class Main { /** * Given a character and its font, calculate the center point offset * for that character. In otherwords, if the character were placed * at point (0,0), the result would be the center point of the * character. */ public static Point getCenterOffset(Graphics g, Font f, char ch) { FontMetrics fm = g.getFontMetrics(f); int xOffset = (int) (fm.charWidth(ch) / 2); int yOffset = (int) (fm.getAscent() / 2); return new Point(xOffset, yOffset); } }