Java Draw String drawString(Graphics2D g2, String str, int x, int y, int halign, int valign)

Here you can find the source of drawString(Graphics2D g2, String str, int x, int y, int halign, int valign)

Description

draw String

License

Open Source License

Declaration

public static int drawString(Graphics2D g2, String str, int x, int y, int halign, int valign) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * MIT License//from   w w  w  .ja v a  2  s  .  c  om
 *
 * Copyright (c) 2016 Ashur Rafiev
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************/

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

public class Main {
    public static int drawString(Graphics2D g2, String str, int x, int y, int halign, int valign) {
        FontMetrics fm = g2.getFontMetrics();
        int w = fm.stringWidth(str);
        int h = fm.getAscent() - fm.getDescent();
        Point a = new Point(w * halign / 2, h * valign / 2);
        g2.drawString(str, x - a.x, y + h - a.y);
        return y + h;
    }
}

Related

  1. drawString(Graphics g2, String string, int x, int y, Font font, Color color)
  2. drawString(Graphics2D g, String s, int x, int y, int width)
  3. drawString(Graphics2D g, String string, double x, double y, Color color)
  4. drawString(Graphics2D g, String string, int x, int y)
  5. drawString(Graphics2D g, String string, int x, int y, boolean includeOutline)
  6. drawString(Graphics2D gfx, String text, int x, int y)
  7. drawString(Graphics2D graphics, String text, double x, double y, boolean centerHorizontal, boolean centerVertical, Color backgroundColor)
  8. drawString(Image img, String text, Color color)
  9. drawString(String pString, Graphics2D pG, int pX, int pY, int pWidth, int pHeight, boolean pYOverflow, boolean pShrinkWords, String pJustification, boolean pDraw)