KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > AnimatedIcon


1 /*
2  * AnimatedIcon.java - Animated version of ImageIcon
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 2002 Kris Kopicki
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import java.awt.*;
27 import java.awt.event.*;
28 import javax.swing.*;
29 //}}}
30

31 /**
32  * A animated version of ImageIcon. It can be used anywhere an ImageIcon can be.
33  */

34 public class AnimatedIcon extends ImageIcon
35 {
36     //{{{ AnimatedIcon constructor
37
/**
38      * @param frames The frames to be used in the animation
39      * @param rate The frame rate of the animation, in frames per second
40      * @param host The container that the animation is used in
41      */

42     public AnimatedIcon(Image icon, Image[] frames, int rate, Component host)
43     {
44         super(icon);
45         this.icon = icon;
46         this.frames = frames;
47         delay = 1000/rate;
48         this.host = host;
49     } //}}}
50

51     //{{{ getFrames() method
52
public Image[] getFrames()
53     {
54         return frames;
55     } //}}}
56

57     //{{{ getIcon() method
58
public Image getIcon()
59     {
60         return icon;
61     } //}}}
62

63     //{{{ getRate() method
64
public int getRate()
65     {
66         return 1000/delay;
67     } //}}}
68

69     //{{{ setFrames() method
70
public void setFrames(Image[] frames)
71     {
72         this.frames = frames;
73     } //}}}
74

75     //{{{ setIcon() method
76
public void setIcon(Image icon)
77     {
78         this.icon = icon;
79     } //}}}
80

81     //{{{ setRate() method
82
public void setRate(int rate)
83     {
84         delay = 1000/rate;
85     } //}}}
86

87     //{{{ start() method
88
/**
89      * Starts the animation rolling
90      */

91     public void start()
92     {
93         if(timer != null)
94             return;
95
96         timer = new Timer(delay,new Animator());
97         timer.start();
98     } //}}}
99

100     //{{{ stop() method
101
/**
102      * Stops the animation, and resets to frame 0
103      */

104     public void stop()
105     {
106         current = 0;
107         if(timer != null)
108         {
109             timer.stop();
110             timer = null;
111         }
112
113         setImage(icon);
114         host.repaint();
115     } //}}}
116

117     //{{{ Private members
118
private Image[] frames;
119     private int current;
120     private int delay;
121     private Timer timer;
122     private Component host;
123     private Image icon;
124     //}}}
125

126     //{{{ Animator class
127
class Animator implements ActionListener
128     {
129         public void actionPerformed(ActionEvent evt)
130         {
131             current = (current + 1) % frames.length;
132             setImage(frames[current]);
133             host.repaint();
134         }
135     } //}}}
136
}
137
Popular Tags