Java Graphics Draw String drawCenteredString(Graphics g, String str, int x, int y)

Here you can find the source of drawCenteredString(Graphics g, String str, int x, int y)

Description

draw Centered String

License

Open Source License

Declaration

public static void drawCenteredString(Graphics g, String str, int x, int y) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.FontMetrics;
import java.awt.Graphics;

public class Main {
    public static void drawCenteredString(Graphics g, String str, int x, int y) {
        FontMetrics fm = g.getFontMetrics();
        int strW = fm.stringWidth(str), strH = fm.getHeight();
        x -= strW / 2;/*from www .  ja  v  a  2 s. c  o  m*/
        y += strH / 5;
        g.drawString(str, x, y);
    }
}

Related

  1. centerStringX(String s, int w, Graphics2D g2d)
  2. centerStringY(String s, int h, Graphics2D g2d)
  3. drawCentered(Graphics g, String text, Point p)
  4. drawCenteredChar(Graphics g, char[] chars, int x, int y, int w, int h)
  5. drawCenteredString(Graphics g, Rectangle rect, String str)
  6. drawCenteredString(String s, Graphics g, int x, int y)
  7. drawCenteredText(Graphics2D g, String text, int x, int y, float align_x, float align_y, float font_size)
  8. drawCenteredText(java.awt.Graphics g, String s, java.awt.Rectangle rect)
  9. drawClippedString(Graphics g, String t, int x, int y, int width)