Draws the string at the specified location underlining the specified character : Text Layout « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » Text LayoutScreenshots 
Draws the string at the specified location underlining the specified character
    
/*
 * Copyright (c) 2001-2009 JGoodies Karsten Lentzsch. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  o Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  o Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  o Neither the name of JGoodies Karsten Lentzsch nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


import java.awt.*;
import java.awt.print.PrinterGraphics;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicGraphicsUtils;


/**
 * Provides convenience behavior used by the JGoodies Looks.<p>
 *
 * <strong>Note:</strong> This class is not part of the public Looks API.
 * It should be treated as library internal and should not be used by
 * API users. It may be removed or changed with the Looks version
 * without further notice.
 *
 @author  Karsten Lentzsch
 @author  Andrej Golovnin
 @version $Revision: 1.12 $
 *
 @since 2.0
 */
public final class RenderingUtils {

    private static final String PROP_DESKTOPHINTS = "awt.font.desktophints";

    private static final String SWING_UTILITIES2_NAME ="sun.swing.SwingUtilities2";

    /**
     * In Java 5 or later, this field holds the public static method
     * <code>SwingUtilities2#drawStringUnderlinedAt</code> that has been added
     * for Java 5.
     */
    private static Method drawStringUnderlineCharAtMethod = null;

    static {
   
            drawStringUnderlineCharAtMethod = getMethodDrawStringUnderlineCharAt();
       
    }


    private RenderingUtils() {
        // Overrides default constructor; prevents instantiation.
    }

    /**
     * Draws the string at the specified location underlining the specified
     * character.
     *
     @param c JComponent that will display the string, may be null
     @param g Graphics to draw the text to
     @param text String to display
     @param underlinedIndex Index of a character in the string to underline
     @param x X coordinate to draw the text at
     @param y Y coordinate to draw the text at
     */
    public static void drawStringUnderlineCharAt(JComponent c,Graphics g,
                           String text, int underlinedIndex, int x,int y) {
      
            if (drawStringUnderlineCharAtMethod != null) {
                try {
                    drawStringUnderlineCharAtMethod.invoke(null,
                            new Object[] {c, g, text, new Integer(underlinedIndex),
                                          new Integer(x)new Integer(y)});
                    return;
                catch (IllegalArgumentException e) {
                    // Use the BasicGraphicsUtils as fallback
                catch (IllegalAccessException e) {
                    // Use the BasicGraphicsUtils as fallback
                catch (InvocationTargetException e) {
                    // Use the BasicGraphicsUtils as fallback
                }
            }
            Graphics2D g2 = (Graphics2Dg;
            Map oldRenderingHints = installDesktopHints(g2);
            BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, underlinedIndex, x, y);
            if (oldRenderingHints != null) {
                g2.addRenderingHints(oldRenderingHints);
            }
            return;
      
        BasicGraphicsUtils.drawStringUnderlineCharAt(g, text, underlinedIndex, x, y);
    }


    // Private Helper Code ****************************************************


    private static Method getMethodDrawStringUnderlineCharAt() {
        try {
            Class clazz = Class.forName(SWING_UTILITIES2_NAME);
            return clazz.getMethod(
                    "drawStringUnderlineCharAt",
                    new Class[] {JComponent.class, Graphics.class, String.class, Integer.TYPE, Integer.TYPE, Integer.TYPE}
                    );
        catch (ClassNotFoundException e) {
            // returns null
        catch (SecurityException e) {
            // returns null
        catch (NoSuchMethodException e) {
            // returns null
        }
        return null;
    }


    private static Map installDesktopHints(Graphics2D g2) {
        Map oldRenderingHints = null;
 
            Map desktopHints = desktopHints(g2);
            if (desktopHints != null && !desktopHints.isEmpty()) {
                oldRenderingHints = new HashMap(desktopHints.size());
                RenderingHints.Key key;
                for (Iterator i = desktopHints.keySet().iterator(); i.hasNext();) {
                    key = (RenderingHints.Keyi.next();
                    oldRenderingHints.put(key, g2.getRenderingHint(key));
                }
                g2.addRenderingHints(desktopHints);
            }
    
        return oldRenderingHints;
    }


    private static Map desktopHints(Graphics2D g2) {
        if (isPrinting(g2)) {
            return null;
        }
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        GraphicsDevice device = g2.getDeviceConfiguration().getDevice();
        Map desktopHints = (Maptoolkit.getDesktopProperty(
                PROP_DESKTOPHINTS + '.' + device.getIDstring());
        if (desktopHints == null) {
            desktopHints = (Maptoolkit.getDesktopProperty(PROP_DESKTOPHINTS);
        }
        // It is possible to get a non-empty map but with disabled AA.
        if (desktopHints != null) {
            Object aaHint = desktopHints.get(RenderingHints.KEY_TEXT_ANTIALIASING);
            if ((aaHint == RenderingHints.VALUE_TEXT_ANTIALIAS_OFF||
                (aaHint == RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)) {
                desktopHints = null;
            }
        }
        return desktopHints;
    }


    private static boolean isPrinting(Graphics g) {
        return instanceof PrintGraphics || g instanceof PrinterGraphics;
    }


}

   
    
    
    
  
Related examples in the same category
1.JFreeChart: Draw String DemoJFreeChart: Draw String Demo
2.Unicode: test layoutUnicode: test layout
3.Unicode displayUnicode display
4.Line break for textlayoutLine break for textlayout
5.Mouse hit and textlayoutMouse hit and textlayout
6.TextLayout demoTextLayout demo
7.Draw text along a curveDraw text along a curve
8.TextHitInfo Demo: tell you which is the letter you are clickingTextHitInfo Demo: tell you which is the letter you are clicking
9.TextAttribute: Underline and strike throughTextAttribute: Underline and strike through
10.TextAttribute: color and fontTextAttribute: color and font
11.Hightlight text by drag and selectionHightlight text by drag and selection
12.LineMetrics: the metrics to layout characters along a lineLineMetrics: the metrics to layout characters along a line
13.Paragraph LayoutParagraph Layout
14.Caret actionCaret action
15.Caret and TextLayoutCaret and TextLayout
16.Another Line Break Demo
17.A display of text, formatted by us instead of by AWT/SwingA display of text, formatted by us instead of by AWT/Swing
18.Draw text with TextLayout
19.Drawing a Paragraph of Text
20.Getting the Shape from the Outline of Text
21.Drawing Text with Mixed Styles
22.Drawing multi-line text with AttributedString and LineBreakMeasurer
23.Renders a paragraph of text (line breaks ignored) to an image (created and returned).
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.