Here you can find the source of drawVerticalTextCenter(Graphics2D g2, String str, int x, int y)
public static void drawVerticalTextCenter(Graphics2D g2, String str, int x, int y)
//package com.java2s; //License from project: Open Source License import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; public class Main { private static final double NINETY_DEGREES = Math.toRadians(90.0); public static void drawVerticalTextCenter(Graphics2D g2, String str, int x, int y) { int strWidth = getStringWidth(g2, str); drawVerticalText(g2, str, x + g2.getFontMetrics().getHeight() / 2, y + strWidth / 2);/*from w w w.java 2 s . co m*/ } public static int getStringWidth(Graphics2D g2, final String str) { Rectangle2D bounds = g2.getFont().getStringBounds(str, g2.getFontRenderContext()); return (int) bounds.getWidth(); } public static void drawVerticalText(Graphics2D g2, String str, int x, int y) { g2.translate(x, y); g2.rotate(-NINETY_DEGREES); g2.drawString(str, 0, 0); g2.rotate(NINETY_DEGREES); g2.translate(-x, -y); } }