Java Draw String drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY)

Here you can find the source of drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY)

Description

Draw a string vertically centered between upper and lower y left aligned to x.

License

Open Source License

Parameter

Parameter Description
graphics Graphics to paint with
label label to draw
x left x
upperY upper y bound
lowerY lower y bound

Declaration

public static void drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY) 

Method Source Code

//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);
    }
}

Related

  1. drawStringOnScreen(String s, Color color, long milseconds)
  2. drawStringPair(Graphics2D g, String str1, String str2, int left, int right, int y, int size, Color color, boolean bold)
  3. drawStringRight(final Graphics g, final FontMetrics m, final String str, final int x, final int y)
  4. drawStringRightAlignedVTop(Graphics graphics, String string, int x, int yTop)
  5. drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
  6. drawStringVertical(Graphics graphics, String string, int x, int y)
  7. drawStringWithHighlighting(Graphics g, String s, int x, int y, Color foreground, Color highlighting)