Java FontMetrics getCenterOffset(Graphics g, Font f, char ch)

Here you can find the source of getCenterOffset(Graphics g, Font f, char ch)

Description

Given a character and its font, calculate the center point offset for that character.

License

Open Source License

Declaration

public static Point getCenterOffset(Graphics g, Font f, char ch) 

Method Source Code


//package com.java2s;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Font;
import java.awt.FontMetrics;

public class Main {
    /**//from ww  w.j  a  v  a 2 s  . c  o  m
     * 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);
    }
}

Related

  1. clipString(FontMetrics metrics, int availableWidth, String fullText)
  2. clipString(Graphics g, String t, int width)
  3. cutString(String text, FontMetrics fontMetrics, int maxWidth)
  4. fitString(String s, Graphics2D g2, Rectangle2D rect)
  5. getAllStylesSameWidthsFontsFamillyName()
  6. getComponentAverageCharacterWidth(Component component)
  7. getConsoleFontMetrics(Font font)
  8. getLineBreakIndex(Graphics g, String text, int index, double maxWidth)
  9. getLineHeight(Component c, int defaultHeight)