Java Draw Centered drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c)

Here you can find the source of drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c)

Description

draw String Centered

License

Open Source License

Declaration

public static void drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c) 

Method Source Code


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

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

public class Main {
    public static void drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c) {
        drawStringCentered(g, text, x, y, fontHeight, c, false);
    }//from   w w  w . ja v a2 s . co  m

    public static void drawStringCentered(Graphics2D g, String text, int x, int y, int fontHeight, Color c,
            boolean bold) {
        Font f = new Font("Serif", bold ? Font.BOLD : Font.PLAIN, fontHeight);
        g.setFont(f);
        FontMetrics fm = g.getFontMetrics();
        g.setColor(c);
        g.drawString(text, x - fm.stringWidth(text) / 2, y);
    }
}

Related

  1. drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
  2. drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)
  3. drawStringCentered(final Graphics2D g2d, final Dimension dim, final String drawString, final int fontSize)
  4. drawStringCentered(Graphics g, String str, int x, int y, int width, int height)
  5. drawStringCentered(Graphics graphics, String string, Rectangle area)
  6. drawVerticallyCenteredText(String text, int margin, Rectangle rect, Graphics g2D, boolean rightJustify)