paint Text - Java 2D Graphics

Java examples for 2D Graphics:Text

Description

paint Text

Demo Code


//package com.java2s;
import java.awt.*;

public class Main {
    /**//from  w w w . ja  v a  2s . c o m
     *
     * @param g Graphics that paint text
     * @param text String to be painted
     * @param w Width of string
     * @param h Height of string
     */
    public static void paintText(Graphics g, String text, int w, int h) {
        FontMetrics fm = g.getFontMetrics();
        int stringWidth = fm.stringWidth(text);
        g.drawString(text, (w - stringWidth) / 2,
                (h - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent());
    }
}

Related Tutorials