001: /*
002: * ProGuard -- shrinking, optimization, obfuscation, and preverification
003: * of Java bytecode.
004: *
005: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the Free
009: * Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
015: * more details.
016: *
017: * You should have received a copy of the GNU General Public License along
018: * with this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: package proguard.gui.splash;
022:
023: import java.awt.*;
024: import java.awt.image.BufferedImage;
025:
026: /**
027: * This Sprite encapsulates another Sprite, which is then buffered in an Image.
028: *
029: * @author Eric Lafortune
030: */
031: public class BufferedSprite implements Sprite {
032: private final int bufferX;
033: private final int bufferY;
034: private final Image bufferImage;
035: private final Color backgroundColor;
036: private final Sprite sprite;
037: private final VariableInt x;
038: private final VariableInt y;
039:
040: private long cachedTime = -1;
041:
042: /**
043: * Creates a new BufferedSprite with an ABGR image.
044: * @param bufferX the x offset of the buffer image.
045: * @param bufferY the y offset of the buffer image.
046: * @param width the width of the buffer image.
047: * @param height the height of the buffer image.
048: * @param sprite the Sprite that is painted in the buffer.
049: * @param x the variable x ordinate of the image buffer for painting.
050: * @param y the variable y ordinate of the image buffer for painting.
051: *
052: */
053: public BufferedSprite(int bufferX, int bufferY, int width,
054: int height, Sprite sprite, VariableInt x, VariableInt y) {
055:
056: this (bufferX, bufferY, new BufferedImage(width, height,
057: BufferedImage.TYPE_4BYTE_ABGR), null, sprite, x, y);
058: }
059:
060: /**
061: * Creates a new BufferedSprite with the given image.
062: * @param bufferX the x offset of the buffer image.
063: * @param bufferY the y offset of the buffer image.
064: * @param bufferImage the Image that is used for the buffering.
065: * @param backgroundColor the background color that is used for the buffer.
066: * @param sprite the Sprite that is painted in the buffer.
067: * @param x the variable x ordinate of the image buffer for
068: * painting.
069: * @param y the variable y ordinate of the image buffer for
070: * painting.
071: */
072: public BufferedSprite(int bufferX, int bufferY, Image bufferImage,
073: Color backgroundColor, Sprite sprite, VariableInt x,
074: VariableInt y) {
075: this .bufferX = bufferX;
076: this .bufferY = bufferY;
077: this .bufferImage = bufferImage;
078: this .backgroundColor = backgroundColor;
079: this .sprite = sprite;
080: this .x = x;
081: this .y = y;
082: }
083:
084: // Implementation for Sprite.
085:
086: public void paint(Graphics graphics, long time) {
087: if (time != cachedTime) {
088: Graphics bufferGraphics = bufferImage.getGraphics();
089:
090: // Clear the background.
091: if (backgroundColor != null) {
092: Graphics2D bufferGraphics2D = (Graphics2D) bufferGraphics;
093: bufferGraphics2D.setComposite(AlphaComposite.Clear);
094: bufferGraphics.fillRect(0, 0, bufferImage
095: .getWidth(null), bufferImage.getHeight(null));
096: bufferGraphics2D.setComposite(AlphaComposite.Src);
097: } else {
098: bufferGraphics.setColor(backgroundColor);
099: bufferGraphics.fillRect(0, 0, bufferImage
100: .getWidth(null), bufferImage.getHeight(null));
101: }
102:
103: // Set up the buffer graphics.
104: bufferGraphics.translate(-bufferX, -bufferY);
105: bufferGraphics.setColor(graphics.getColor());
106: bufferGraphics.setFont(graphics.getFont());
107:
108: // Draw the sprite.
109: sprite.paint(bufferGraphics, time);
110:
111: bufferGraphics.dispose();
112:
113: cachedTime = time;
114: }
115:
116: // Draw the buffer image.
117: graphics.drawImage(bufferImage, bufferX + x.getInt(time),
118: bufferY + y.getInt(time), null);
119: }
120: }
|