Here you can find the source of getStringBounds(Graphics2D g2d, String text, int x, int y)
Parameter | Description |
---|---|
g2d | The Graphics2D instance used to render the text. |
text | The text to calculate the bounds for. |
x | The x coordinate to use for drawing the text. |
y | The y coordinate to use for drawing the text. |
public static Rectangle2D getStringBounds(Graphics2D g2d, String text, int x, int y)
//package com.java2s; /*//from w w w. jav a 2 s .co m * Copyright (c) 2009 * * This file is part of HibernateJConsole. * * HibernateJConsole is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HibernateJConsole 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. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HibernateJConsole. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.*; import java.awt.geom.Rectangle2D; public class Main { /** * Calculates the string bounds for the given text. * * @param g2d The Graphics2D instance used to render the text. * @param text The text to calculate the bounds for. * @param x The x coordinate to use for drawing the text. * @param y The y coordinate to use for drawing the text. * @return A rectangle describing the outer bounds of the text when rendered. */ public static Rectangle2D getStringBounds(Graphics2D g2d, String text, int x, int y) { Rectangle2D bounds = g2d.getFontMetrics().getStringBounds(text, g2d); return setLocation(bounds, x, y); } /** * Sets the coordinates on the given rectangle. * * @param rectangle The rectangle to change. * @param x The new x coordinate to use. * @param y The new y coordinate to use. * @return The given rectangle instance. */ public static Rectangle2D setLocation(Rectangle2D rectangle, double x, double y) { rectangle.setRect(x, y, rectangle.getWidth(), rectangle.getHeight()); return rectangle; } }