KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > base > AnimatedGIFComponent


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.core.gui.base;
19
20 import java.awt.Component JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.MediaTracker JavaDoc;
25
26 /**
27  * Component containing an animated gif, which can be started and
28  * stopped.
29  *
30  * @author fdietz
31  */

32
33 public class AnimatedGIFComponent extends Component JavaDoc {
34     private Image JavaDoc image;
35     private Image JavaDoc restImage;
36     private boolean stop = false;
37
38     public AnimatedGIFComponent(Image JavaDoc image, Image JavaDoc restImage) {
39
40         super();
41         
42         this.image = image;
43         this.restImage = restImage;
44         
45         MediaTracker JavaDoc mt = new MediaTracker JavaDoc(this);
46         mt.addImage(image, 9);
47         try {
48             mt.waitForAll();
49         } catch (Exception JavaDoc e) {
50             e.printStackTrace();
51         }
52         
53         stop();
54     }
55
56     public void paint(Graphics JavaDoc g) {
57         if ( stop )
58             g.drawImage(restImage, 0, 0, this);
59         else
60             g.drawImage(image, 0, 0, this);
61     }
62
63     public void update(Graphics JavaDoc g) {
64         paint(g);
65     }
66
67     public boolean imageUpdate(Image JavaDoc img, int infoflags, int x, int y,
68             int width, int height) {
69         if (stop)
70             return false;
71         
72         if ((infoflags & FRAMEBITS) != 0) {
73             //repaint(x, y, width, height);
74

75             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
76                 public void run() {
77                     repaint();
78                 }
79             });
80             
81         }
82         
83         return true;
84     }
85     
86     public void stop() {
87         this.stop = true;
88         
89         javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
90             public void run() {
91                 repaint();
92             }
93         });
94     }
95
96     public void go() {
97         this.stop = false;
98         
99         javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
100             public void run() {
101                 repaint();
102             }
103         });
104     }
105
106     public boolean stopped() {
107         return this.stop;
108     }
109
110     public Dimension JavaDoc getMinimumSize() {
111         return new Dimension JavaDoc(image.getWidth(this), image.getHeight(this));
112     }
113
114     public Dimension JavaDoc getPreferredSize() {
115         return getMinimumSize();
116     }
117
118 }
119
Popular Tags