Here you can find the source of clipString(Graphics g, String t, int width)
Parameter | Description |
---|---|
g | _more_ |
t | _more_ |
width | _more_ |
public static String clipString(Graphics g, String t, int width)
//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); } } } }