Java Draw Text drawTextInCenter(Graphics2D g2d, int x, int y, int width, int height, String predictInStepStr)

Here you can find the source of drawTextInCenter(Graphics2D g2d, int x, int y, int width, int height, String predictInStepStr)

Description

draw Text In Center

License

Open Source License

Declaration

public static void drawTextInCenter(Graphics2D g2d, int x, int y, int width, int height,
            String predictInStepStr) 

Method Source Code


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

public class Main {
    private static final Font sanSerifFont = new Font("SanSerif", Font.BOLD, 11);

    public static void drawTextInCenter(Graphics2D g2d, int x, int y, int width, int height,
            String predictInStepStr) {
        g2d.setFont(sanSerifFont);/*from  ww  w. ja  v a2s.c  om*/
        g2d.setColor(Color.WHITE);
        FontMetrics fm = g2d.getFontMetrics();
        int w = fm.stringWidth(predictInStepStr);
        int h = fm.getAscent();
        g2d.drawString(predictInStepStr, x + width / 2 - (w / 2) + 2, y + height / 2 + (h / 4) + 2);
    }
}