PageRenderTime 71ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/AnimatedIcon.java

#
Java | 136 lines | 72 code | 15 blank | 49 comment | 4 complexity | 4e28695872345a15d8b6dd253873a518 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  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. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import javax.swing.*;
  27. //}}}
  28. /**
  29. * A animated version of ImageIcon. It can be used anywhere an ImageIcon can be.
  30. */
  31. public class AnimatedIcon extends ImageIcon
  32. {
  33. //{{{ AnimatedIcon constructor
  34. /**
  35. * @param frames The frames to be used in the animation
  36. * @param rate The frame rate of the animation, in frames per second
  37. * @param host The container that the animation is used in
  38. */
  39. public AnimatedIcon(Image icon, Image[] frames, int rate, Component host)
  40. {
  41. super(icon);
  42. this.icon = icon;
  43. this.frames = frames;
  44. delay = 1000/rate;
  45. this.host = host;
  46. } //}}}
  47. //{{{ getFrames() method
  48. public Image[] getFrames()
  49. {
  50. return frames;
  51. } //}}}
  52. //{{{ getIcon() method
  53. public Image getIcon()
  54. {
  55. return icon;
  56. } //}}}
  57. //{{{ getRate() method
  58. public int getRate()
  59. {
  60. return 1000/delay;
  61. } //}}}
  62. //{{{ setFrames() method
  63. public void setFrames(Image[] frames)
  64. {
  65. this.frames = frames;
  66. } //}}}
  67. //{{{ setIcon() method
  68. public void setIcon(Image icon)
  69. {
  70. this.icon = icon;
  71. } //}}}
  72. //{{{ setRate() method
  73. public void setRate(int rate)
  74. {
  75. delay = 1000/rate;
  76. } //}}}
  77. //{{{ start() method
  78. /**
  79. * Starts the animation rolling
  80. */
  81. public void start()
  82. {
  83. if(timer != null)
  84. return;
  85. timer = new Timer(delay,new Animator());
  86. timer.start();
  87. } //}}}
  88. //{{{ stop() method
  89. /**
  90. * Stops the animation, and resets to frame 0
  91. */
  92. public void stop()
  93. {
  94. current = 0;
  95. if(timer != null)
  96. {
  97. timer.stop();
  98. timer = null;
  99. }
  100. setImage(icon);
  101. host.repaint();
  102. } //}}}
  103. //{{{ Private members
  104. private Image[] frames;
  105. private int current;
  106. private int delay;
  107. private Timer timer;
  108. private Component host;
  109. private Image icon;
  110. //}}}
  111. //{{{ Animator class
  112. class Animator implements ActionListener
  113. {
  114. public void actionPerformed(ActionEvent evt)
  115. {
  116. current = (current + 1) % frames.length;
  117. setImage(frames[current]);
  118. host.repaint();
  119. }
  120. } //}}}
  121. }