Java Draw Centered drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)

Here you can find the source of drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g)

Description

draw String Center

License

Open Source License

Declaration

public static void drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g) 

Method Source Code


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

import java.awt.Font;
import java.awt.Graphics2D;

import java.awt.Rectangle;

public class Main {

    public static void drawStringCenter(Object o, Rectangle r, Font f, Graphics2D g) {
        int w = r.x + (r.width - g.getFontMetrics(f).stringWidth(o.toString())) / 2;
        int h = r.y + g.getFontMetrics(f).getAscent();
        h -= g.getFontMetrics(f).getDescent();
        h += (r.height - f.getSize()) / 2;
        g.setFont(f);/*from  w w  w .ja  v a 2s . c o m*/
        g.drawString(o.toString(), w, h);
    }
}

Related

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