Java Draw Centered drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)

Here you can find the source of drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)

Description

draw String Center

License

Open Source License

Declaration

public static void drawStringCenter(Graphics2D g, String text, Rectangle rect, 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;
import java.awt.Rectangle;

public class Main {
    public static void drawStringCenter(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c) {
        Font f = new Font("Serif", Font.PLAIN, fontHeight);
        g.setFont(f);/*from  www.ja  va 2 s  .c  om*/
        FontMetrics fm = g.getFontMetrics();
        g.setColor(c);
        g.drawString(text, (int) rect.getCenterX() - fm.stringWidth(text) / 2, rect.y + rect.height);
    }
}

Related

  1. drawCenteredCircle(Graphics2D g, int x, int y, int r)
  2. drawCenterString(Graphics g, Point p, String text)
  3. drawCentred(Graphics2D g2, String message, Rectangle2D box)
  4. drawCentredText(Graphics g, String str, int x, int y, int width, int height)
  5. drawEllipseOld(Graphics g, double xCenter, double yCenter, double semiMA, double semiMI, double angle)
  6. drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)
  7. drawStringCentered(final Graphics2D g2d, final Dimension dim, final String drawString, final int fontSize)
  8. drawStringCentered(Graphics g, String str, int x, int y, int width, int height)
  9. drawStringCentered(Graphics graphics, String string, Rectangle area)