Here you can find the source of drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
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 |
public static void drawStringUnderlineCharAt(Graphics g, String text, int underlinedIndex, int x, int y)
//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); } } }