KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > gui > splash > BufferedSprite


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * 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 import java.awt.image.BufferedImage JavaDoc;
25
26 /**
27  * This Sprite encapsulates another Sprite, which is then buffered in an Image.
28  *
29  * @author Eric Lafortune
30  */

31 public class BufferedSprite implements Sprite
32 {
33     private int bufferX;
34     private int bufferY;
35     private Image JavaDoc bufferImage;
36     private Color backgroundColor;
37     private Sprite sprite;
38     private VariableInt x;
39     private VariableInt y;
40
41     private long cachedTime = -1;
42
43
44     /**
45      * Creates a new BufferedSprite with an ABGR image.
46      * @param bufferX the x offset of the buffer image.
47      * @param bufferY the y offset of the buffer image.
48      * @param width the width of the buffer image.
49      * @param height the height of the buffer image.
50      * @param sprite the Sprite that is painted in the buffer.
51      * @param x the variable x ordinate of the image buffer for painting.
52      * @param y the variable y ordinate of the image buffer for painting.
53      *
54      */

55     public BufferedSprite(int bufferX,
56                           int bufferY,
57                           int width,
58                           int height,
59                           Sprite sprite,
60                           VariableInt x,
61                           VariableInt y)
62     {
63
64         this(bufferX,
65              bufferY,
66              new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_4BYTE_ABGR),
67              null,
68              sprite,
69              x,
70              y);
71     }
72
73
74     /**
75      * Creates a new BufferedSprite with the given image.
76      * @param bufferX the x offset of the buffer image.
77      * @param bufferY the y offset of the buffer image.
78      * @param bufferImage the Image that is used for the buffering.
79      * @param backgroundColor the background color that is used for the buffer.
80      * @param sprite the Sprite that is painted in the buffer.
81      * @param x the variable x ordinate of the image buffer for
82      * painting.
83      * @param y the variable y ordinate of the image buffer for
84      * painting.
85      */

86     public BufferedSprite(int bufferX,
87                           int bufferY,
88                           Image JavaDoc bufferImage,
89                           Color backgroundColor,
90                           Sprite sprite,
91                           VariableInt x,
92                           VariableInt y)
93     {
94         this.bufferX = bufferX;
95         this.bufferY = bufferY;
96         this.bufferImage = bufferImage;
97         this.backgroundColor = backgroundColor;
98         this.sprite = sprite;
99         this.x = x;
100         this.y = y;
101     }
102
103
104    // Implementation for Sprite.
105

106     public void paint(Graphics graphics, long time)
107     {
108         if (time != cachedTime)
109         {
110             Graphics bufferGraphics = bufferImage.getGraphics();
111
112             // Clear the background.
113
if (backgroundColor != null)
114             {
115                 Graphics2D bufferGraphics2D = (Graphics2D)bufferGraphics;
116                 bufferGraphics2D.setComposite(AlphaComposite.Clear);
117                 bufferGraphics.fillRect(0, 0, bufferImage.getWidth(null), bufferImage.getHeight(null));
118                 bufferGraphics2D.setComposite(AlphaComposite.Src);
119             }
120             else
121             {
122                 bufferGraphics.setColor(backgroundColor);
123                 bufferGraphics.fillRect(0, 0, bufferImage.getWidth(null), bufferImage.getHeight(null));
124             }
125
126             // Set up the buffer graphics.
127
bufferGraphics.translate(-bufferX, -bufferY);
128             bufferGraphics.setColor(graphics.getColor());
129             bufferGraphics.setFont(graphics.getFont());
130
131             // Draw the sprite.
132
sprite.paint(bufferGraphics, time);
133
134             bufferGraphics.dispose();
135
136             cachedTime = time;
137         }
138
139         // Draw the buffer image.
140
graphics.drawImage(bufferImage,
141                            bufferX + x.getInt(time),
142                            bufferY + y.getInt(time),
143                            null);
144     }
145 }
146
Popular Tags