Java Swing Font Metrics computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)

Here you can find the source of computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)

Description

Computes the dimensions of a multi-line string using the specified font.

License

Open Source License

Parameter

Parameter Description
oFontMetrics Font metrics used to size the string.
astr Multi-line string to be sized.

Return

Dimensions of the specified string.

Declaration


public static final Dimension computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)
        throws IllegalArgumentException 

Method Source Code


//package com.java2s;
/*//from w ww  .j a  v a 2s.  c o m
 * Utilities.java
 *
 * Copyright 2000-2013 by Steven Soloff.
 * All rights reserved.
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.Dimension;
import java.awt.FontMetrics;

import javax.swing.SwingUtilities;

public class Main {
    /**
     * Computes the dimensions of a multi-line string using the specified font.
     *
     * @param  oFontMetrics  Font metrics used to size the string.
     * @param  astr  Multi-line string to be sized.
     *
     * @return  Dimensions of the specified string.
     *
     * @exception  IllegalArgumentException  If oFontMetrics or astr is null.
     */

    public static final Dimension computeMultiLineStringDimension(FontMetrics oFontMetrics, String[] astr)
            throws IllegalArgumentException {
        /////////////////////////////////////////////////////////////////////
        // VARIABLE DECLARATIONS                                           //

        int nWidth, // Maximum width of the string
                nLines, // Number of lines in the string
                nI; // Loop control variable

        //                                                                 //
        /////////////////////////////////////////////////////////////////////

        // Make sure arguments are valid
        if (oFontMetrics == null || astr == null)
            throw new IllegalArgumentException();

        // Compute the maximum width of the string
        for (nI = 0, nWidth = 0, nLines = astr.length; nI < nLines; nI++)
            nWidth = Math.max(nWidth, SwingUtilities.computeStringWidth(oFontMetrics, astr[nI]));

        // Return the dimensions of the string
        return (new Dimension(nWidth, oFontMetrics.getHeight() * astr.length));
    }
}

Related

  1. clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
  2. clipString(JComponent c, FontMetrics fm, String string, int availTextWidth)
  3. clipStringIfNecessary(JComponent c, FontMetrics fm, String string, int availTextWidth)
  4. clipText(FontMetrics fm, String val, int width)
  5. computeStringWidth(FontMetrics fontMetrics, String str)
  6. drawLine(Graphics g, FontMetrics fm, Rectangle rect, String text, int hAlign, int y, int mnemonic)
  7. getBreakLocation(Segment s, FontMetrics metrics, int x0, int x, TabExpander e, int startOffset)
  8. getClippedText(String text, FontMetrics fm, int maxWidth)