Java Draw String drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)

Here you can find the source of drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)

Description

draw String Ex

License

Open Source License

Declaration

public static void drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign) 

Method Source Code

//package com.java2s;
/*/* w  w w  .jav  a 2  s . co  m*/
 * PortUtil.cs
 * Copyright ? 2009-2011 kbinani
 *
 * This file is part of org.kbinani.
 *
 * org.kbinani is free software; you can redistribute it and/or
 * modify it under the terms of the BSD License.
 *
 * org.kbinani is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Rectangle;

public class Main {
    public static void drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign) {
        Graphics2D g = (Graphics2D) g1;
        g.setFont(font);

        FontMetrics fm = g.getFontMetrics();
        Dimension ret = new Dimension(fm.stringWidth(s), fm.getHeight());
        float x = 0;
        float y = 0;

        if (align > 0) {
            x = (rect.x + rect.width) - ret.width;
        } else if (align < 0) {
            x = rect.x;
        } else {
            x = (rect.x + (rect.width / 2.0f)) - (ret.width / 2.0f);
        }

        if (valign > 0) {
            y = rect.y + rect.height; // - ret.height;
        } else if (valign < 0) {
            y = rect.y + ret.height;
        } else {
            y = rect.y + (rect.height / 2.0f) + (ret.height / 2.0f);
        }

        g.drawString(s, x, y);
    }
}

Related

  1. drawString(String pString, Graphics2D pG, int pX, int pY, int pWidth, int pHeight, boolean pYOverflow, boolean pShrinkWords, String pJustification, boolean pDraw)
  2. drawString(String txt, int x, int y, Graphics g)
  3. drawStringAlignCenter(Graphics2D g2d, String s, int x, int y)
  4. drawStringAt(Graphics g, String t, int x, int y, int where)
  5. drawStringAtPoint(Graphics g, String s, Point p)
  6. drawStringInBox(Graphics g, String string, int x, int y)
  7. drawStringLeft(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
  8. drawStringOnImage(Image image, String string, int x, int y)
  9. drawStringOnScreen(String s, Color color, long milseconds)