01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * Software Foundation; either version 2 of the License, or (at your option)
10: * any later version.
11: *
12: * This program is distributed in the hope that it will be useful, but WITHOUT
13: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15: * more details.
16: *
17: * You should have received a copy of the GNU General Public License along
18: * with this program; if not, write to the Free Software Foundation, Inc.,
19: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: package proguard.gui.splash;
22:
23: import java.awt.*;
24:
25: /**
26: * This Sprite adds a drop shadow to another Sprite.
27: *
28: * @author Eric Lafortune
29: */
30: public class ShadowedSprite implements Sprite {
31: private final VariableInt xOffset;
32: private final VariableInt yOffset;
33: private final VariableDouble alpha;
34: private final VariableInt blur;
35: private final Sprite sprite;
36:
37: private float cachedAlpha = -1.0f;
38: private Color cachedColor;
39:
40: /**
41: * Creates a new ShadowedSprite.
42: * @param xOffset the variable x-offset of the shadow, relative to the sprite itself.
43: * @param yOffset the variable y-offset of the shadow, relative to the sprite itself.
44: * @param alpha the variable darkness of the shadow (between 0 and 1).
45: * @param blur the variable blur of the shadow (0 for sharp shadows, 1 or
46: * more for increasingly blurry shadows).
47: * @param sprite the Sprite to be painted with its shadow.
48: */
49: public ShadowedSprite(VariableInt xOffset, VariableInt yOffset,
50: VariableDouble alpha, VariableInt blur, Sprite sprite) {
51: this .xOffset = xOffset;
52: this .yOffset = yOffset;
53: this .alpha = alpha;
54: this .blur = blur;
55: this .sprite = sprite;
56: }
57:
58: // Implementation for Sprite.
59:
60: public void paint(Graphics graphics, long time) {
61: double l = alpha.getDouble(time);
62: int b = blur.getInt(time) + 1;
63:
64: float a = 1.0f - (float) Math.pow(1.0 - l, 1.0 / (b * b));
65: if (a != cachedAlpha) {
66: cachedAlpha = a;
67: cachedColor = new Color(0f, 0f, 0f, a);
68: }
69:
70: // Set up the shadow graphics.
71: //OverrideGraphics2D g = new OverrideGraphics2D((Graphics2D)graphics);
72: //g.setOverrideColor(cachedColor);
73:
74: // Set the shadow color.
75: Color actualColor = graphics.getColor();
76: graphics.setColor(cachedColor);
77:
78: int xo = xOffset.getInt(time) - b / 2;
79: int yo = yOffset.getInt(time) - b / 2;
80:
81: // Draw the sprite's shadow.
82: for (int x = 0; x < b; x++) {
83: for (int y = 0; y < b; y++) {
84: int xt = xo + x;
85: int yt = yo + y;
86: graphics.translate(xt, yt);
87: sprite.paint(graphics, time);
88: graphics.translate(-xt, -yt);
89: }
90: }
91:
92: // Restore the actual sprite color.
93: graphics.setColor(actualColor);
94:
95: // Draw the sprite itself in the ordinary graphics.
96: sprite.paint(graphics, time);
97: }
98: }
|