Java Draw String drawString(Graphics g, String text, int underlinedChar, int x, int y)

Here you can find the source of drawString(Graphics g, String text, int underlinedChar, 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

Declaration

public static void drawString(Graphics g, String text, int underlinedChar, int x, int y) 

Method Source Code

//package com.java2s;
/**/*from  w w w  .  ja  v a2  s  .co m*/
 * Title: tn5250J
 * Copyright:   Copyright (c) 2001,202,2003
 * Company:
 * @author  Kenneth J. Pouncey
 * @version 0.4
 *
 * Description:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA
 *
 */

import java.awt.FontMetrics;
import java.awt.Graphics;

public class Main {
    /** Draw a string with the graphics g at location (x,y) just like g.drawString() would.
     *  The first occurence of underlineChar in text will be underlined. The matching is
     *  not case sensitive.
     */
    public static void drawString(Graphics g, String text, int underlinedChar, int x, int y) {

        //        char b[] = new char[1];
        //      String s;
        char lc, uc;
        int index = -1, lci, uci;

        if (underlinedChar != '\0') {
            //           b[0] = (char)underlinedChar;
            //           s = new String(b).toUpperCase();
            //       uc = s.charAt(0);
            uc = Character.toUpperCase((char) underlinedChar);

            //            s = new String(b).toLowerCase();
            lc = Character.toLowerCase((char) underlinedChar);

            uci = text.indexOf(uc);
            lci = text.indexOf(lc);

            if (uci == -1)
                index = lci;
            else if (lci == -1)
                index = uci;
            else
                index = (lci < uci) ? lci : uci;
        }

        g.drawString(text, x, y);
        if (index != -1) {
            FontMetrics fm = g.getFontMetrics();
            //            Rectangle underlineRect = new Rectangle();

            int underlineRectX = x + fm.stringWidth(text.substring(0, index));
            int underlineRectY = y;
            int underlineRectWidth = fm.charWidth(text.charAt(index));
            int underlineRectHeight = 1;
            g.fillRect(underlineRectX, underlineRectY + fm.getDescent() - 1, underlineRectWidth,
                    underlineRectHeight);
        }
    }
}

Related

  1. drawString(final Graphics g, final String text, final int x, final int y)
  2. drawString(Graphics g, String s, int alignment, Rectangle r)
  3. drawString(Graphics g, String s, int x, int y, int width, int height)
  4. drawString(Graphics g, String s, int x, int y, int width, int height)
  5. drawString(Graphics g, String text, int underlinedChar, int x, int y)
  6. drawString(Graphics g, String text, int underlinedChar, int x, int y)
  7. drawString(Graphics g, String text, int x, int y)
  8. drawString(Graphics g2, String string, int x, int y, Font font, Color color)
  9. drawString(Graphics2D g, String s, int x, int y, int width)