1 22 23 package org.gjt.sp.jedit.gui; 24 25 import java.awt.*; 27 import java.awt.event.*; 28 import javax.swing.*; 29 31 34 public class AnimatedIcon extends ImageIcon 35 { 36 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 } 51 public Image[] getFrames() 53 { 54 return frames; 55 } 57 public Image getIcon() 59 { 60 return icon; 61 } 63 public int getRate() 65 { 66 return 1000/delay; 67 } 69 public void setFrames(Image[] frames) 71 { 72 this.frames = frames; 73 } 75 public void setIcon(Image icon) 77 { 78 this.icon = icon; 79 } 81 public void setRate(int rate) 83 { 84 delay = 1000/rate; 85 } 87 91 public void start() 92 { 93 if(timer != null) 94 return; 95 96 timer = new Timer(delay,new Animator()); 97 timer.start(); 98 } 100 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 } 117 private Image[] frames; 119 private int current; 120 private int delay; 121 private Timer timer; 122 private Component host; 123 private Image icon; 124 126 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 } } 137 | Popular Tags |