001: /*
002: * AnimatedIcon.java - Animated version of ImageIcon
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2002 Kris Kopicki
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import java.awt.*;
027: import java.awt.event.*;
028: import javax.swing.*;
029:
030: //}}}
031:
032: /**
033: * A animated version of ImageIcon. It can be used anywhere an ImageIcon can be.
034: */
035: public class AnimatedIcon extends ImageIcon {
036: //{{{ AnimatedIcon constructor
037: /**
038: * @param frames The frames to be used in the animation
039: * @param rate The frame rate of the animation, in frames per second
040: * @param host The container that the animation is used in
041: */
042: public AnimatedIcon(Image icon, Image[] frames, int rate,
043: Component host) {
044: super (icon);
045: this .icon = icon;
046: this .frames = frames;
047: delay = 1000 / rate;
048: this .host = host;
049: } //}}}
050:
051: //{{{ getFrames() method
052: public Image[] getFrames() {
053: return frames;
054: } //}}}
055:
056: //{{{ getIcon() method
057: public Image getIcon() {
058: return icon;
059: } //}}}
060:
061: //{{{ getRate() method
062: public int getRate() {
063: return 1000 / delay;
064: } //}}}
065:
066: //{{{ setFrames() method
067: public void setFrames(Image[] frames) {
068: this .frames = frames;
069: } //}}}
070:
071: //{{{ setIcon() method
072: public void setIcon(Image icon) {
073: this .icon = icon;
074: } //}}}
075:
076: //{{{ setRate() method
077: public void setRate(int rate) {
078: delay = 1000 / rate;
079: } //}}}
080:
081: //{{{ start() method
082: /**
083: * Starts the animation rolling
084: */
085: public void start() {
086: if (timer != null)
087: return;
088:
089: timer = new Timer(delay, new Animator());
090: timer.start();
091: } //}}}
092:
093: //{{{ stop() method
094: /**
095: * Stops the animation, and resets to frame 0
096: */
097: public void stop() {
098: current = 0;
099: if (timer != null) {
100: timer.stop();
101: timer = null;
102: }
103:
104: setImage(icon);
105: host.repaint();
106: } //}}}
107:
108: //{{{ Private members
109: private Image[] frames;
110: private int current;
111: private int delay;
112: private Timer timer;
113: private Component host;
114: private Image icon;
115:
116: //}}}
117:
118: //{{{ Animator class
119: class Animator implements ActionListener {
120: public void actionPerformed(ActionEvent evt) {
121: current = (current + 1) % frames.length;
122: setImage(frames[current]);
123: host.repaint();
124: }
125: } //}}}
126: }
|