Java FontMetrics clipString(Graphics g, String t, int width)

Here you can find the source of clipString(Graphics g, String t, int width)

Description

_more_

License

Open Source License

Parameter

Parameter Description
g _more_
t _more_
width _more_

Return

_more_

Declaration

public static String clipString(Graphics g, String t, int width) 

Method Source Code

//package com.java2s;
/**/*  w  w  w.  jav a  2s . co  m*/
* Copyright (c) 2008-2015 Geode Systems LLC
* This Software is licensed under the Geode Systems RAMADDA License available in the source distribution in the file 
* ramadda_license.txt. The above copyright notice shall be included in all copies or substantial portions of the Software.
*/

import java.awt.*;

public class Main {
    /**
     * _more_
     *
     * @param g _more_
     * @param t _more_
     * @param width _more_
     *
     * @return _more_
     */
    public static String clipString(Graphics g, String t, int width) {
        if (width <= 0) {
            return "";
        }
        FontMetrics fm = g.getFontMetrics();
        int length = t.length();
        int maxAdvance = fm.getMaxAdvance();
        if (length * maxAdvance < width) {
            return t;
        } else {
            if (fm.stringWidth(t) < width) {
                return t;
            } else {
                int runningWidth = 0;
                int i;
                for (i = 0; (i < length) && (runningWidth < width); i++) {
                    runningWidth += fm.charWidth(t.charAt(i));
                }
                i--;

                return t.substring(0, i);
            }
        }
    }
}

Related

  1. abbreviateName(String str, FontMetrics fm, int width)
  2. adjustFontSizeToGetPreferredWidthOfLabel(JLabel jLabel, int initialWidth)
  3. alignRight(FontMetrics fm, String string, int align)
  4. clipString(FontMetrics fm, String text, int availableWidth)
  5. clipString(FontMetrics metrics, int availableWidth, String fullText)
  6. cutString(String text, FontMetrics fontMetrics, int maxWidth)
  7. fitString(String s, Graphics2D g2, Rectangle2D rect)
  8. getAllStylesSameWidthsFontsFamillyName()
  9. getCenterOffset(Graphics g, Font f, char ch)