Here you can find the source of drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY)
Parameter | Description |
---|---|
graphics | Graphics to paint with |
label | label to draw |
x | left x |
upperY | upper y bound |
lowerY | lower y bound |
public static void drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY)
//package com.java2s; /*/*w ww . jav a 2 s .c om*/ * File: GraphicsHelper.java * Copyright (c) 2004-2007 Peter Kliem (Peter.Kliem@jaret.de) * A commercial license is available, see http://www.jaret.de. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html */ import java.awt.Graphics; import java.awt.geom.Rectangle2D; public class Main { /** * Draw a string vertically centered between upper and lower y left aligned to x. * * @param graphics Graphics to paint with * @param label label to draw * @param x left x * @param upperY upper y bound * @param lowerY lower y bound */ public static void drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY) { Rectangle2D rect = graphics.getFontMetrics().getStringBounds(string, graphics); int yy = (int) upperY + (lowerY - upperY) / 2 + (int) rect.getHeight() / 2; graphics.drawString(string, x, yy); } }