Package ca.cgjennings.ui
Class AnimatedIcon
- java.lang.Object
-
- ca.cgjennings.ui.AnimatedIcon
-
- All Implemented Interfaces:
javax.swing.Icon
public class AnimatedIcon extends java.lang.Object implements javax.swing.Icon
An icon that animates through a series of images at a fixed rate. This has a similar effect to setting a GIF image on an icon, without the drawbacks of the GIF image format.- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description AnimatedIcon(javax.swing.JComponent component, int delay, int cycles, java.awt.Image... frames)
Create an AnimatedIcon.AnimatedIcon(javax.swing.JComponent component, int delay, java.awt.Image... frames)
Create an AnimatedIcon that will repeat endlessly.AnimatedIcon(javax.swing.JComponent component, java.awt.Image... frames)
Create an AnimatedIcon using the default frame rate (approximately 16 fps).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getCurrentFrame()
Gets the currently displayed frame.int
getCycles()
Get the cycles to complete before animation stops.int
getDelay()
Gets the delay between frames, in ms.java.awt.Image
getFrame(int index)
Returns the frame with the specified index.int
getFrameCount()
Returns the number of frames in the animation.int
getIconHeight()
Gets the height of this icon.int
getIconWidth()
Gets the width of this icon.void
paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
Paint the current framevoid
pause()
Pause the animation.void
play()
Continue the animation from where the animation was paused, or restart it if it has completed.void
setCurrentFrame(int frame)
Set the currently displayed frame.void
setCycles(int cycles)
Specify the number of times to repeat each animation sequence, or cycle.void
setDelay(int delay)
Sets the delay between frames, in ms.void
stop()
Stop the animation.
-
-
-
Constructor Detail
-
AnimatedIcon
public AnimatedIcon(javax.swing.JComponent component, java.awt.Image... frames)
Create an AnimatedIcon using the default frame rate (approximately 16 fps).- Parameters:
component
- the component the icon will be painted onframes
- the animation frames
-
AnimatedIcon
public AnimatedIcon(javax.swing.JComponent component, int delay, java.awt.Image... frames)
Create an AnimatedIcon that will repeat endlessly.- Parameters:
component
- the component the icon will be painted ondelay
- the delay between painting each icon, in msframes
- the Icons to be painted as part of the animation
-
AnimatedIcon
public AnimatedIcon(javax.swing.JComponent component, int delay, int cycles, java.awt.Image... frames)
Create an AnimatedIcon.- Parameters:
component
- the component the icon will be painted ondelay
- the delay between painting each icon, in mscycles
- the number of times to repeat the animation sequence, -1 for no limitframes
- the Icons to be painted as part of the animation
-
-
Method Detail
-
getCurrentFrame
public int getCurrentFrame()
Gets the currently displayed frame.- Returns:
- the index of the displayed image
-
setCurrentFrame
public void setCurrentFrame(int frame)
Set the currently displayed frame.- Parameters:
frame
- the index of the image to be displayed
-
getCycles
public int getCycles()
Get the cycles to complete before animation stops.- Returns:
- the number of cycles
-
setCycles
public void setCycles(int cycles)
Specify the number of times to repeat each animation sequence, or cycle.- Parameters:
cycles
- the number of cycles to complete before the animation stops, or -1 for continuous
-
getDelay
public int getDelay()
Gets the delay between frames, in ms.- Returns:
- the delay between successive frames
-
setDelay
public void setDelay(int delay)
Sets the delay between frames, in ms. To set the frame rate in frames per second, usesetDelay( 1000/frameRate )
.- Parameters:
delay
- the delay between successive frames
-
getFrame
public java.awt.Image getFrame(int index)
Returns the frame with the specified index.- Parameters:
index
- the index of the frame to be returned- Returns:
- the frame at the specified index
- Throws:
java.lang.IndexOutOfBoundsException
- if the index is out of range
-
getFrameCount
public int getFrameCount()
Returns the number of frames in the animation.- Returns:
- the total number of frames
-
pause
public void pause()
Pause the animation.
-
play
public void play()
Continue the animation from where the animation was paused, or restart it if it has completed.
-
stop
public void stop()
Stop the animation.
-
getIconWidth
public int getIconWidth()
Gets the width of this icon.- Specified by:
getIconWidth
in interfacejavax.swing.Icon
- Returns:
- the width of the icon in pixels.
-
getIconHeight
public int getIconHeight()
Gets the height of this icon.- Specified by:
getIconHeight
in interfacejavax.swing.Icon
- Returns:
- the height of the icon in pixels.
-
paintIcon
public void paintIcon(java.awt.Component c, java.awt.Graphics g, int x, int y)
Paint the current frame- Specified by:
paintIcon
in interfacejavax.swing.Icon
- Parameters:
c
- The component on which the icon is paintedg
- the graphics contextx
- the X coordinate of the icon's top-left cornery
- the Y coordinate of the icon's top-left corner
-
-