KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > DropShadowEffect


1 /*
2  * @(#)DropShadowEffect.java 1.2 07/12/12
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import java.awt.image.BufferedImage JavaDoc;
10 import java.awt.image.Raster JavaDoc;
11 import java.awt.image.WritableRaster JavaDoc;
12 import java.util.Arrays JavaDoc;
13
14 /**
15  * DropShadowEffect
16  *
17  * @author Created by Jasper Potts (Jun 18, 2007)
18  * @version 1.0
19  */

20 class DropShadowEffect extends ShadowEffect {
21
22     // =================================================================================================================
23
// Effect Methods
24

25     /**
26      * Get the type of this effect, one of UNDER,BLENDED,OVER. UNDER means the result of apply effect should be painted
27      * under the src image. BLENDED means the result of apply sffect contains a modified src image so just it should be
28      * painted. OVER means the result of apply effect should be painted over the src image.
29      *
30      * @return The effect type
31      */

32     EffectType getEffectType() {
33         return EffectType.UNDER;
34     }
35
36     /**
37      * Apply the effect to the src image generating the result . The result image may or may not contain the source
38      * image depending on what the effect type is.
39      *
40      * @param src The source image for applying the effect to
41      * @param dst The dstination image to paint effect result into. If this is null then a new image will be created
42      * @param w The width of the src image to apply effect to, this allow the src and dst buffers to be bigger than
43      * the area the need effect applied to it
44      * @param h The height of the src image to apply effect to, this allow the src and dst buffers to be bigger than
45      * the area the need effect applied to it
46      * @return The result of appl
47      */

48     BufferedImage JavaDoc applyEffect(BufferedImage JavaDoc src, BufferedImage JavaDoc dst, int w, int h) {
49         // calculate offset
50
double trangleAngle = Math.toRadians(angle - 90);
51         int offsetX = (int) (Math.sin(trangleAngle) * distance);
52         int offsetY = (int) (Math.cos(trangleAngle) * distance);
53         // clac expanded size
54
int tmpOffX = offsetX + size;
55         int tmpOffY = offsetX + size;
56         int tmpW = w + offsetX + size + size;
57         int tmpH = h + offsetX + size;
58         // create tmp buffers
59
int[] lineBuf = getArrayCache().getTmpIntArray(w);
60         byte[] tmpBuf1 = getArrayCache().getTmpByteArray1(tmpW * tmpH);
61         Arrays.fill(tmpBuf1, (byte) 0x00);
62         byte[] tmpBuf2 = getArrayCache().getTmpByteArray2(tmpW * tmpH);
63         // extract src image alpha channel and inverse and offset
64
Raster JavaDoc srcRaster = src.getRaster();
65         for (int y = 0; y < h; y++) {
66             int dy = (y + tmpOffY);
67             int offset = dy * tmpW;
68             srcRaster.getDataElements(0, y, w, 1, lineBuf);
69             for (int x = 0; x < w; x++) {
70                 int dx = x + tmpOffX;
71                 tmpBuf1[offset + dx] = (byte) ((lineBuf[x] & 0xFF000000) >>> 24);
72             }
73         }
74         // blur
75
float[] kernel = EffectUtils.createGaussianKernel(size);
76         EffectUtils.blur(tmpBuf1, tmpBuf2, tmpW, tmpH, kernel, size); // horizontal pass
77
EffectUtils.blur(tmpBuf2, tmpBuf1, tmpH, tmpW, kernel, size);// vertical pass
78
//rescale
79
float spread = Math.min(1 / (1 - (0.01f * this.spread)), 255);
80         for (int i = 0; i < tmpBuf1.length; i++) {
81             int val = (int) (((int) tmpBuf1[i] & 0xFF) * spread);
82             tmpBuf1[i] = (val > 255) ? (byte) 0xFF : (byte) val;
83         }
84         // create color image with shadow color and greyscale image as alpha
85
if (dst == null) dst = EffectUtils.createCompatibleTranslucentImage(w, h);
86         WritableRaster JavaDoc shadowRaster = dst.getRaster();
87         int red = color.getRed(), green = color.getGreen(), blue = color.getBlue();
88         for (int y = 0; y < h; y++) {
89             int srcY = y + tmpOffY;
90             int shadowOffset = (srcY - offsetY) * tmpW;
91             for (int x = 0; x < w; x++) {
92                 int srcX = x + tmpOffX;
93                 lineBuf[x] = tmpBuf1[shadowOffset + (srcX - offsetX)] << 24 | red << 16 | green << 8 | blue;
94             }
95             shadowRaster.setDataElements(0, y, w, 1, lineBuf);
96         }
97         return dst;
98     }
99 }
100
Popular Tags