Here you can find the source of drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)
public static void drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)
//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); } }