Here you can find the source of drawClippedString(Graphics g, String t, int x, int y, int width)
Parameter | Description |
---|---|
g | _more_ |
t | _more_ |
x | _more_ |
y | _more_ |
width | _more_ |
public static void drawClippedString(Graphics g, String t, int x, int y, int width)
//package com.java2s; /**/*from w w w . j av a 2s . c o 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 x _more_ * @param y _more_ * @param width _more_ */ public static void drawClippedString(Graphics g, String t, int x, int y, int width) { g.drawString(clipString(g, t, width), x, y); } /** * _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); } } } }