Java Image createShadowPicture(Image buf)

Here you can find the source of createShadowPicture(Image buf)

Description

Creates an ImageIcon with a Shadow

License

Open Source License

Parameter

Parameter Description
buf , the Image where a Shadow will be applied

Return

with shadow

Declaration

public static ImageIcon createShadowPicture(Image buf) 

Method Source Code

//package com.java2s;
/**/*from  w  ww .  jav  a2  s.  c o  m*/
 * $RCSfile: ,v $
 * $Revision: $
 * $Date: $
 * 
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.awt.Color;

import java.awt.Graphics;
import java.awt.Graphics2D;

import java.awt.Image;

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

import javax.swing.ImageIcon;

import javax.swing.JLabel;

public class Main {
    /**
     * Creates an {@link ImageIcon} with a Shadow
     * 
     * @param buf
     *            , the {@link Image} where a Shadow will be applied
     * @return {@link ImageIcon} with shadow
     */
    public static ImageIcon createShadowPicture(Image buf) {
        buf = removeTransparency(buf);

        BufferedImage splash;

        JLabel label = new JLabel();
        int width = buf.getWidth(null);
        int height = buf.getHeight(null);
        int extra = 4;

        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(0, 0, width, height, 12, 12);

        g2.drawImage(shadow, getBlurOp(7), 0, 0);
        g2.drawImage(buf, 0, 0, label);
        return new ImageIcon(splash);
    }

    /**
     * removes the Transparency of an {@link Image}
     * 
     * @param image
     * @return {@link BufferedImage} without Transparency
     */
    public static BufferedImage removeTransparency(Image image) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        BufferedImage bi2 = new BufferedImage(w, h,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi2.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, w, h);
        g.drawImage(image, 0, 0, null);
        g.dispose();

        return bi2;
    }

    /**
     * 
     * @param size
     * @return
     */
    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

  1. createCursor(Image cursorImage, Point hotSpot, String name)
  2. createCursor(Image image, Point hotspot, String name)
  3. createMemoryImage(int width, int height, Color colour, JComponent someComponent)
  4. createQualityResizedImage( Image orginalImage, int width, int height, boolean keepRatio)
  5. createScaledImage(Image inImage, int inWidth, int inHeight)
  6. createTiledImage(Image img, int width, int height)
  7. crop(Image source, int x1, int y1, int x2, int y2)
  8. displayImage(Image image)
  9. displayImage(Image img)