Java Draw String drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)

Here you can find the source of drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)

Description

Draw a string with the graphics g at location ( x , y ) just like g.drawString would.

License

Open Source License

Parameter

Parameter Description
g Graphics to draw with
text String to draw
underlinedIndex Index of character in text to underline
x x coordinate to draw at
y y coordinate to draw at

Declaration

public static void drawStringUnderlineCharAt(Graphics g, String text,
        int underlinedIndex, int x, int y) 

Method Source Code

//package com.java2s;
/*//  w w w.j  a  v a 2 s .  c  o m
 * @(#)QuaquaUtilities.java  
 *
 * Copyright (c) 2003-2013 Werner Randelshofer, Switzerland.
 * You may not use, copy or modify this file, except in compliance with the
 * accompanying license terms.
 */

import java.awt.*;

public class Main {
    /**
     * Draw a string with the graphics {@code g} at location
     * ({@code x}, {@code y})
     * just like {@code g.drawString} would.
     * The character at index {@code underlinedIndex}
     * in text will be underlined. If {@code index} is beyond the
     * bounds of {@code text} (including < 0), nothing will be
     * underlined.
     *
     * @param g Graphics to draw with
     * @param text String to draw
     * @param underlinedIndex Index of character in text to underline
     * @param x x coordinate to draw at
     * @param y y coordinate to draw at
     * @since 1.4
     */
    public static void drawStringUnderlineCharAt(Graphics g, String text,
            int underlinedIndex, int x, int y) {
        g.drawString(text, x, y);
        if (underlinedIndex >= 0 && underlinedIndex < text.length()) {
            FontMetrics fm = g.getFontMetrics();
            int underlineRectX = x
                    + fm.stringWidth(text.substring(0, underlinedIndex));
            int underlineRectY = y;
            int underlineRectWidth = fm.charWidth(text
                    .charAt(underlinedIndex));
            int underlineRectHeight = 1;
            g.fillRect(underlineRectX,
                    underlineRectY + fm.getDescent() - 1,
                    underlineRectWidth, underlineRectHeight);
        }
    }
}

Related

  1. drawStringOnImage(Image image, String string, int x, int y)
  2. drawStringOnScreen(String s, Color color, long milseconds)
  3. drawStringPair(Graphics2D g, String str1, String str2, int left, int right, int y, int size, Color color, boolean bold)
  4. drawStringRight(final Graphics g, final FontMetrics m, final String str, final int x, final int y)
  5. drawStringRightAlignedVTop(Graphics graphics, String string, int x, int yTop)
  6. drawStringVCentered(Graphics graphics, String string, int x, int upperY, int lowerY)
  7. drawStringVertical(Graphics graphics, String string, int x, int y)
  8. drawStringWithHighlighting(Graphics g, String s, int x, int y, Color foreground, Color highlighting)