public class AnimatedIcon extends Object implements Icon, ActionListener, Runnable
The size of the Icon is determined to be the largest width or height of any Icon. All other Icons are then aligned within the space available when the Icon is painted.
An AnimatedIcon cannot be shared by different components. However, the Icons added to an AnimatedIcon can be shared.
The animation sequence is a simple sequential display of each Icon. When the end is reached the animation restarts at the first Icon. Icons are displayed in the order in which they are added. To create custom animation sequences you will need to override the getNextIconIndex() and isCycleCompleted() methods.
This class was published by Rob Camick on June 21, 2009 as part of a Java Tips Weblog article called "Animated Icon":
http://tips4java.wordpress.com/2009/06/21/animated-icon/
Modifier and Type | Field and Description |
---|---|
static float |
BOTTOM
A constant representing alignment with the bottom edge of a
container.
|
static float |
CENTER
A constant representing alignment in the middle of a container.
|
static float |
LEFT
A constant representing alignment with the left edge of a container.
|
static float |
RIGHT
A constant representing alignment with the right edge of a
container.
|
static float |
TOP
A constant representing alignment with the top edge of a container.
|
Constructor and Description |
---|
AnimatedIcon(JComponent component,
Icon... icons)
Create an AnimatedIcon that will continuously cycle with the
default (500ms).
|
AnimatedIcon(JComponent component,
int delay,
Icon... icons)
Create an AnimatedIcon that will continuously cycle
|
AnimatedIcon(JComponent component,
int delay,
int cycles,
Icon... icons)
Create an AnimatedIcon specifying all the properties.
|
Modifier and Type | Method and Description |
---|---|
void |
actionPerformed(ActionEvent e)
Control the animation of the Icons when the Timer fires.
|
void |
addIcon(Icon... icons)
Add Icons to be used in the animation.
|
float |
getAlignmentX()
Get the alignment of the Icon on the x-axis
|
float |
getAlignmentY()
Get the alignment of the icon on the y-axis
|
int |
getCurrentIconIndex()
Get the index of the currently visible Icon
|
int |
getCycles()
Get the cycles to complete before animation stops.
|
int |
getDelay()
Get the delay between painting each Icon
|
Icon |
getIcon(int index)
Get the Icon at the specified index.
|
int |
getIconCount()
Get the number of Icons contained in this AnimatedIcon.
|
int |
getIconHeight()
Gets the height of this icon.
|
int |
getIconWidth()
Gets the width of this icon.
|
protected int |
getNextIconIndex(int currentIndex,
int iconCount)
Get the index of the next Icon to be displayed.
|
protected boolean |
isCycleCompleted(int currentIndex,
int iconCount)
Check if the currently visible Icon is the last Icon to be displayed
in the animation sequence.
|
boolean |
isShowFirstIcon()
Get the showFirstIcon
|
void |
paintIcon(Component c,
Graphics g,
int x,
int y)
Paint the icons of this compound icon at the specified location
|
void |
pause()
Pause the animation.
|
void |
restart()
Restart the animation from where the animation was paused.
|
void |
run() |
void |
setAlignmentX(float alignmentX)
Specify the horizontal alignment of the icon.
|
void |
setAlignmentY(float alignmentY)
Specify the vertical alignment of the Icon.
|
void |
setCurrentIconIndex(int index)
Set the index of the Icon to be displayed and then repaint the Icon.
|
void |
setCycles(int cycles)
Specify the number of times to repeat each animation sequence, or cycle.
|
void |
setDelay(int delay)
Specify the delay
|
void |
setShowFirstIcon(boolean showFirstIcon)
Display the first icon when animation is finished.
|
void |
start()
Start the animation from the beginning.
|
void |
stop()
Stop the animation.
|
public static final float TOP
public static final float LEFT
public static final float CENTER
public static final float BOTTOM
public static final float RIGHT
public AnimatedIcon(JComponent component, Icon... icons)
component
- the component the icon will be painted onicons
- the Icons to be painted as part of the animationpublic AnimatedIcon(JComponent component, int delay, Icon... icons)
component
- the component the icon will be painted ondelay
- the delay between painting each icon, in millisecondsicons
- the Icons to be painted as part of the animationpublic AnimatedIcon(JComponent component, int delay, int cycles, Icon... icons)
component
- the component the icon will be painted ondelay
- the delay between painting each icon, in millisecondscycles
- the number of times to repeat the animation sequenceicons
- the Icons to be painted as part of the animationpublic void addIcon(Icon... icons)
icons
- the icons to be addedpublic float getAlignmentX()
public void setAlignmentX(float alignmentX)
alignmentX
- common values are LEFT, CENTER (default) or RIGHT
although any value between 0.0 and 1.0 can be usedpublic float getAlignmentY()
public void setAlignmentY(float alignmentY)
alignmentY
- common values TOP, CENTER (default) or BOTTOM
although any value between 0.0 and 1.0 can be usedpublic int getCurrentIconIndex()
public void setCurrentIconIndex(int index)
index
- the index of the Icon to be displayedpublic int getCycles()
public void setCycles(int cycles)
cycles
- the number of cycles to complete before the animation
stops. The default is -1, which means the animation is
continuous.public int getDelay()
public void setDelay(int delay)
delay
- the delay between painting eachIcon (in milliseconds)public Icon getIcon(int index)
index
- the index of the Icon to be returnedIndexOutOfBoundsException
- if the index is out of rangepublic int getIconCount()
public boolean isShowFirstIcon()
public void setShowFirstIcon(boolean showFirstIcon)
showFirstIcon
- true when the first icon is to be displayed,
false otherwisepublic void pause()
public void start()
public void restart()
public void stop()
public int getIconWidth()
getIconWidth
in interface Icon
public int getIconHeight()
getIconHeight
in interface Icon
public void paintIcon(Component c, Graphics g, int x, int y)
public void actionPerformed(ActionEvent e)
actionPerformed
in interface ActionListener
protected int getNextIconIndex(int currentIndex, int iconCount)
currentIndex
- the index of the Icon currently displayediconCount
- the number of Icons to be displayedprotected boolean isCycleCompleted(int currentIndex, int iconCount)
currentIndex
- the index of the Icon currently displayediconCount
- the number of Icons to be displayed