Java Graphics Draw String paintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size, final double tx, final double ty, final boolean isShadow)

Here you can find the source of paintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size, final double tx, final double ty, final boolean isShadow)

Description

paint Text Effect

License

Open Source License

Declaration

public static void paintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size,
            final double tx, final double ty, final boolean isShadow) 

Method Source Code

//package com.java2s;
/*// ww w  .  j  a  v  a 2 s . com
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library 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 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.*;

public class Main {
    public static void paintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size,
            final double tx, final double ty, final boolean isShadow) {
        // Effect "darkness"
        final float opacity = 0.8f;

        final Composite oldComposite = g2d.getComposite();
        final Color oldColor = g2d.getColor();

        // Use a alpha blend smaller than 1 to prevent the effect from becoming too dark when multiple paints occur on top of each other.
        float preAlpha = 0.4f;
        if (oldComposite instanceof AlphaComposite
                && ((AlphaComposite) oldComposite).getRule() == AlphaComposite.SRC_OVER) {
            preAlpha = Math.min(((AlphaComposite) oldComposite).getAlpha(), preAlpha);
        }
        g2d.setColor(c);

        g2d.translate(tx, ty);

        // If the effect is a shadow it looks better to stop painting a bit earlier - shadow will look softer
        final int maxSize = isShadow ? size - 1 : size;

        for (int i = -size; i <= maxSize; i++) {
            for (int j = -size; j <= maxSize; j++) {
                final double distance = i * i + j * j;
                float alpha = opacity;
                if (distance > 0.0d) {
                    alpha = (float) (1.0f / (distance * size * opacity));
                }
                alpha *= preAlpha;
                if (alpha > 1.0f) {
                    alpha = 1.0f;
                }
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
                g2d.drawString(s, i + size, j + size);
            }
        }

        // Restore graphics
        g2d.translate(-tx, -ty);
        g2d.setComposite(oldComposite);
        g2d.setColor(oldColor);

        g2d.drawString(s, 0, 0);

        //        final Color oldColor = g2d.getColor ();
        //        g2d.setColor ( c );
        //        g2d.drawString ( s, 1, 1 );
        //
        //        g2d.setColor ( oldColor );
        //        g2d.drawString ( s, 0, 0 );
    }
}

Related

  1. drawVerticalTextCenter(Graphics2D g2, String str, int x, int y)
  2. drawWrappedText(Graphics2D g2, float x, float y, float width, String text)
  3. paintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY)
  4. paintOutline(Graphics g, String s, int textX, int textY, int thickness)
  5. paintShadowTitleFat(Graphics g, String title, int x, int y, Color frente, Color shadow, int desp)
  6. paintTexture(BufferedImage pattern, BufferedImage template)