Create the shadow of a bufferedimage, returns a buffered image with the shadow directly behind it. - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

Create the shadow of a bufferedimage, returns a buffered image with the shadow directly behind it.

Demo Code


//package com.java2s;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;

public class Main {
    /**//ww  w . jav  a 2  s.  co m
     * Create the shadow of a bufferedimage, returns a
     * buffered image with the shadow directly behind it.
     * 
     * @param image
     * @param extra
     * @return
     */
    public static BufferedImage createShadowPicture(BufferedImage image,
            int extra) {
        int width = image.getWidth();
        int height = image.getHeight();

        BufferedImage splash = new BufferedImage(width + extra, height
                + extra, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) splash.getGraphics();

        BufferedImage shadow = new BufferedImage(width + extra, height
                + extra, BufferedImage.TYPE_INT_ARGB);
        Graphics g = shadow.getGraphics();
        g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f));
        g.fillRoundRect(6, 6, width, height, 12, 12);

        g2.drawImage(shadow, getBlurOp(7), 0, 0);
        g2.drawImage(image, 0, 0, null);

        return splash;
    }

    private static ConvolveOp getBlurOp(int size) {
        float[] data = new float[size * size];
        float value = 1 / (float) (size * size);
        for (int i = 0; i < data.length; i++) {
            data[i] = value;
        }
        return new ConvolveOp(new Kernel(size, size, data));
    }
}

Related Tutorials