Here you can find the source of getTextBounds(FontMetrics fm, String s)
Parameter | Description |
---|---|
fm | the font metrics |
s | the string |
public static Rectangle2D getTextBounds(FontMetrics fm, String s)
//package com.java2s; // Distributed under the GNU Lesser General Public License import java.awt.geom.*; import java.awt.*; import javax.swing.*; public class Main { /**//from w w w . jav a2 s . c o m * Get bounds of a multi-line text * * @param fm the font metrics * @param s the string * @return the bounds */ public static Rectangle2D getTextBounds(FontMetrics fm, String s) { if (s.indexOf('\n') >= 0) { String[] a = s.split("\n"); int fh = fm.getFont().getSize(); int lh = (int) fh + 1; double h = lh * a.length; double w = 0; for (int ai = 0; ai < a.length; ai++) { int tw = SwingUtilities.computeStringWidth(fm, a[ai]); if (tw > w) w = tw; } return new Rectangle2D.Double(0, 0, w, h); } else { int h = fm.getFont().getSize(); int w = SwingUtilities.computeStringWidth(fm, s); return new Rectangle2D.Double(0, 0, w, h); } } /** * Get bounds of a multi-line text * * @param g the graphics context * @param s the string * @return the bounds */ public static Rectangle2D getTextBounds(Graphics g, String s) { FontMetrics fm = g.getFontMetrics(); return getTextBounds(fm, s); } }