Java Graphics Draw String centerStringX(String s, int w, Graphics2D g2d)

Here you can find the source of centerStringX(String s, int w, Graphics2D g2d)

Description

center String X

License

Open Source License

Declaration

public static int centerStringX(String s, int w, Graphics2D g2d) 

Method Source Code

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

import java.awt.FontMetrics;
import java.awt.Graphics2D;

public class Main {
    public static int centerStringX(String s, int w, Graphics2D g2d) {
        FontMetrics fm = g2d.getFontMetrics(g2d.getFont());

        return (w - fm.stringWidth(s)) / 2;
    }/*www  .  jav a  2  s. com*/

    public static int stringWidth(String s, Graphics2D g2d) {
        FontMetrics fm = g2d.getFontMetrics(g2d.getFont());

        return fm.stringWidth(s);
    }
}

Related

  1. centerString(Graphics g, String s, Font f, int w0, int w, int h)
  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)