Java Graphics Draw String paintShadowTitleFat(Graphics g, String title, int x, int y, Color frente, Color shadow, int desp)

Here you can find the source of paintShadowTitleFat(Graphics g, String title, int x, int y, Color frente, Color shadow, int desp)

Description

paint Shadow Title Fat

License

Open Source License

Declaration

static void paintShadowTitleFat(Graphics g, String title, int x, int y,
            Color frente, Color shadow, int desp) 

Method Source Code

//package com.java2s;
/*/*from  www.  ja v a2s .c o m*/
 *                 (C) Copyright 2005 Nilo J. Gonzalez
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser Gereral Public Licence as published by the Free
 * Software Foundation; either version 2 of the Licence, or (at your opinion) any
 * later version.
 * 
 * This library is distributed in the hope that it will be usefull, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of merchantability or fitness for a
 * particular purpose. See the GNU Lesser General Public Licence for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public Licence along
 * with this library; if not, write to the Free Software Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, Ma 02111-1307 USA.
 *
 * http://www.gnu.org/licenses/lgpl.html (English)
 * http://gugs.sindominio.net/gnu-gpl/lgpl-es.html (Espa?ol)
 *
 *
 * Original author: Nilo J. Gonzalez
 */

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;

import javax.swing.*;

public class Main {
    static final int THIN = 0;
    static final int FAT = 1;
    static final int MATRIX_FAT = 5;
    static Kernel kernelFat;
    static final int MATRIX_THIN = 3;
    static Kernel kernelThin;

    static void paintShadowTitleFat(Graphics g, String title, int x, int y,
            Color frente) {
        paintShadowTitle(g, title, x, y, frente, Color.black, 1, FAT,
                SwingConstants.HORIZONTAL);
    }

    static void paintShadowTitleFat(Graphics g, String title, int x, int y,
            Color frente, Color shadow) {
        paintShadowTitle(g, title, x, y, frente, shadow, 1, FAT,
                SwingConstants.HORIZONTAL);
    }

    static void paintShadowTitleFat(Graphics g, String title, int x, int y,
            Color frente, Color shadow, int desp) {
        paintShadowTitle(g, title, x, y, frente, shadow, desp, FAT,
                SwingConstants.HORIZONTAL);
    }

    static void paintShadowTitle(Graphics g, String title, int x, int y,
            Color frente, Color shadow, int desp, int tipo, int orientation) {

        // Si hay que rotar la fuente, se rota 
        Font f = g.getFont();
        if (orientation == SwingConstants.VERTICAL) {
            AffineTransform rotate = AffineTransform
                    .getRotateInstance(Math.PI / 2);
            f = f.deriveFont(rotate);
        }

        // Si hay que pintar sombra, se hacen un monton de cosas
        if (shadow != null) {
            int matrix = (tipo == THIN ? MATRIX_THIN : MATRIX_FAT);

            Rectangle2D rect = g.getFontMetrics().getStringBounds(title, g);

            int w, h;
            if (orientation == SwingConstants.HORIZONTAL) {
                w = (int) rect.getWidth() + 6 * matrix; // Hay que dejar espacio para las sombras y el borde
                h = (int) rect.getHeight() + 6 * matrix; // que ConvolveOp ignora por el EDGE_NO_OP
            } else {
                h = (int) rect.getWidth() + 6 * matrix; // Hay que dejar espacio para las sombras y el borde
                w = (int) rect.getHeight() + 6 * matrix; // que ConvolveOp ignora por el EDGE_NO_OP
            }

            // La sombra del titulo
            BufferedImage iTitulo = new BufferedImage(w, h,
                    BufferedImage.TYPE_INT_ARGB);
            BufferedImage iSombra = new BufferedImage(w, h,
                    BufferedImage.TYPE_INT_ARGB);

            Graphics2D g2 = iTitulo.createGraphics();
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

            g2.setFont(f);
            g2.setColor(shadow);
            g2.drawString(title, 3 * matrix, 3 * matrix); // La pintamos en el centro

            ConvolveOp cop = new ConvolveOp((tipo == THIN ? kernelThin
                    : kernelFat), ConvolveOp.EDGE_NO_OP, null);
            cop.filter(iTitulo, iSombra); // A ditorsionar

            // Por fin, pintamos el jodio titulo
            g.drawImage(iSombra, x - 3 * matrix + desp, // Lo llevamos a la posicion original y le sumamos 1          
                    y - 3 * matrix + desp, // para que la sombra quede pelin desplazada 
                    null);
        }

        // Si hay que pintar el frente, se pinta
        if (frente != null) {
            g.setFont(f);
            g.setColor(frente);
            g.drawString(title, x, y);
        }
    }
}

Related

  1. drawVerticalText(Graphics2D graphics, Font font, Dimension2D dimension, String text)
  2. drawVerticalTextCenter(Graphics2D g2, String str, int x, int y)
  3. drawWrappedText(Graphics2D g2, float x, float y, float width, String text)
  4. paintCenteredString(Graphics2D g, String str, Font font, int centerX, int centerY)
  5. paintOutline(Graphics g, String s, int textX, int textY, int thickness)
  6. paintTextEffect(final Graphics2D g2d, final String s, final Color c, final int size, final double tx, final double ty, final boolean isShadow)
  7. paintTexture(BufferedImage pattern, BufferedImage template)