Java Graphics Draw String centerString(Graphics g, String s, Font f, int w0, int w, int h)

Here you can find the source of centerString(Graphics g, String s, Font f, int w0, int w, int h)

Description

center String

License

Open Source License

Declaration

public static void centerString(Graphics g, String s, Font f, int w0,
            int w, int h) 

Method Source Code

//package com.java2s;

import java.awt.Font;
import java.awt.Graphics;

public class Main {
    public static void centerString(Graphics g, String s, Font f, int w0,
            int w, int h) {
        int p, sw;

        g.setFont(f);//w  ww.j ava 2  s.  c  o m
        sw = g.getFontMetrics(f).stringWidth(s);
        g.drawString(s, w0 + (w - sw) / 2, h);
    }

    public static void centerString(Graphics g, String s, int w0, int w,
            int h) {
        int p, sw;

        Font f = g.getFont();
        g.setFont(f);
        sw = g.getFontMetrics(f).stringWidth(s);
        g.drawString(s, w0 + (w - sw) / 2, h);
    }
}

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)