Java Graphics Draw String drawClippedString(Graphics g, String t, int x, int y, int width)

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

Description

_more_

License

Open Source License

Parameter

Parameter Description
g _more_
t _more_
x _more_
y _more_
width _more_

Declaration

public static void drawClippedString(Graphics g, String t, int x, int y, int width) 

Method Source Code

//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);
            }
        }
    }
}

Related

  1. drawCenteredString(Graphics g, Rectangle rect, String str)
  2. drawCenteredString(Graphics g, String str, int x, int y)
  3. drawCenteredString(String s, Graphics g, int x, int y)
  4. drawCenteredText(Graphics2D g, String text, int x, int y, float align_x, float align_y, float font_size)
  5. drawCenteredText(java.awt.Graphics g, String s, java.awt.Rectangle rect)
  6. drawEmphasizedString(Graphics g, Color foreground, Color emphasis, String s, int underlinedIndex, int x, int y)
  7. drawFitText(Graphics2D g2, int x, int y, int width, int height, String text)
  8. drawFormattedString(Graphics2D g2, String htmlStr, int x, int y, int w, int h)
  9. drawGradient(Graphics g, JComponent c, String prefix)