Groovy Documentation

com.wordpress.tipsforjava.swing
[Java] Class AnimatedIcon

java.lang.Object
  com.wordpress.tipsforjava.swing.AnimatedIcon
All Implemented Interfaces:
Icon, ActionListener, Runnable

public class AnimatedIcon
extends Object

The AnimatedIcon will display a series of Icons in a predetermined sequence. This animation sequence can be configured to keep repeating or to stop after a specified number of cycles. 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.

Authors:
Rob Camick
Darryl Burke


Field Summary
static float BOTTOM

static float CENTER

private static int DEFAULT_CYCLES

private static int DEFAULT_DELAY

static float LEFT

static float RIGHT

static float TOP

private float alignmentX

private float alignmentY

private boolean animationFinished

private JComponent component

private int currentIconIndex

private int cycles

private int cyclesCompleted

private int iconHeight

private int iconWidth

private int iconX

private int iconY

private List icons

private boolean showFirstIcon

private Timer timer

 
Constructor Summary
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.

 
Method Summary
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.

private void calculateIconDimensions()

Calculate the width and height of the Icon based on the maximum width and height of any individual Icon.

float getAlignmentX()

Get the alignment of the Icon on the x-axis

float getAlignmentY()

Get the alignment of the icon on the y-axis

JComponent getComponent()

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.

private int getOffset(int maxValue, int iconValue, float alignment)

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.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

BOTTOM

public static final float BOTTOM


CENTER

public static final float CENTER


DEFAULT_CYCLES

private static final int DEFAULT_CYCLES


DEFAULT_DELAY

private static final int DEFAULT_DELAY


LEFT

public static final float LEFT


RIGHT

public static final float RIGHT


TOP

public static final float TOP


alignmentX

private float alignmentX


alignmentY

private float alignmentY


animationFinished

private boolean animationFinished


component

private JComponent component


currentIconIndex

private int currentIconIndex


cycles

private int cycles


cyclesCompleted

private int cyclesCompleted


iconHeight

private int iconHeight


iconWidth

private int iconWidth


iconX

private int iconX


iconY

private int iconY


icons

private List icons


showFirstIcon

private boolean showFirstIcon


timer

private Timer timer


 
Constructor Detail

AnimatedIcon

public AnimatedIcon(JComponent component, Icon... icons)
Create an AnimatedIcon that will continuously cycle with the default (500ms).
Parameters:
component - the component the icon will be painted on
icons - the Icons to be painted as part of the animation


AnimatedIcon

public AnimatedIcon(JComponent component, int delay, Icon... icons)
Create an AnimatedIcon that will continuously cycle
Parameters:
component - the component the icon will be painted on
delay - the delay between painting each icon, in milli seconds
icons - the Icons to be painted as part of the animation


AnimatedIcon

public AnimatedIcon(JComponent component, int delay, int cycles, Icon... icons)
Create an AnimatedIcon specifying all the properties.
Parameters:
component - the component the icon will be painted on
delay - the delay between painting each icon, in milli seconds
cycles - the number of times to repeat the animation sequence
icons - the Icons to be painted as part of the animation


 
Method Detail

actionPerformed

public void actionPerformed(ActionEvent e)
Control the animation of the Icons when the Timer fires.


addIcon

public void addIcon(Icon... icons)
Add Icons to be used in the animation.
Parameters:
icons - the icons to be added


calculateIconDimensions

private void calculateIconDimensions()
Calculate the width and height of the Icon based on the maximum width and height of any individual Icon.


getAlignmentX

public float getAlignmentX()
Get the alignment of the Icon on the x-axis
Returns:
the alignment


getAlignmentY

public float getAlignmentY()
Get the alignment of the icon on the y-axis
Returns:
the alignment


getComponent

public JComponent getComponent()


getCurrentIconIndex

public int getCurrentIconIndex()
Get the index of the currently visible Icon
Returns:
the index of the Icon


getCycles

public int getCycles()
Get the cycles to complete before animation stops.
Returns:
the number of cycles


getDelay

public int getDelay()
Get the delay between painting each Icon
Returns:
the delay


getIcon

public Icon getIcon(int index)
Get the Icon at the specified index.
Throws:
IndexOutOfBoundsException if the index is out of range
Parameters:
index - the index of the Icon to be returned
Returns:
the Icon at the specifed index


getIconCount

public int getIconCount()
Get the number of Icons contained in this AnimatedIcon.
Returns:
the total number of Icons


getIconHeight

de
	public
public int getIconHeight()
Gets the height of this icon.
Returns:
the height of the icon in pixels.


getIconWidth

de
	public
public int getIconWidth()
Gets the width of this icon.
Returns:
the width of the icon in pixels.


getNextIconIndex

protected int getNextIconIndex(int currentIndex, int iconCount)
Get the index of the next Icon to be displayed. This implementation displays the Icons in the order in which they were added to this class. When the end is reached it will start back at the first Icon. Typically this method, along with the isCycleCompleted() method, would be extended to provide a custom animation sequence.
Parameters:
index - the index of the Icon currently displayed
iconCount - the number of Icons to be displayed
Returns:
the index of the next Icon to be displayed


getOffset

private int getOffset(int maxValue, int iconValue, float alignment)


isCycleCompleted

protected boolean isCycleCompleted(int currentIndex, int iconCount)
Check if the currently visible Icon is the last Icon to be displayed in the animation sequence. If so, this indicates the completion of a single cycle. The animation can continue for an unlimited number of cycles or for a specified number of cycles. This implemention checks if the last icon is currently displayed. Typically this method, along with the getNextIconIndex() method, would be extended to provide a custom animation sequence.
Parameters:
index - the index of the Icon currently displayed
iconCount - the number of Icons to be displayed
Returns:
the index of the next Icon to be displayed


isShowFirstIcon

public boolean isShowFirstIcon()
Get the showFirstIcon
Returns:
the showFirstIcon value


paintIcon

de
	public
public void paintIcon(Component c, Graphics g, int x, int y)
Paint the icons of this compound icon at the specified location
Parameters:
c - The component on which the icon is painted
g - the graphics context
x - the X coordinate of the icon's top-left corner
y - the Y coordinate of the icon's top-left corner


pause

public void pause()
Pause the animation. The animation can be restarted from the current Icon using the restart() method.


restart

public void restart()
Restart the animation from where the animation was paused. Or, if the animation has finished, it will be restarted from the beginning.


run

public void run()


setAlignmentX

public void setAlignmentX(float alignmentX)
Specify the horizontal alignment of the icon.
Parameters:
alignmentX - common values are LEFT, CENTER (default) or RIGHT although any value between 0.0 and 1.0 can be used


setAlignmentY

public void setAlignmentY(float alignmentY)
Specify the vertical alignment of the Icon.
Parameters:
alignmentY - common values TOP, CENTER (default) or BOTTOM although any value between 0.0 and 1.0 can be used


setCurrentIconIndex

public void setCurrentIconIndex(int index)
Set the index of the Icon to be displayed and then repaint the Icon.
Parameters:
index - the index of the Icon to be displayed


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. The default is -1, which means the animation is continuous.


setDelay

public void setDelay(int delay)
Specify the delay
Parameters:
delay - the delay between painting eachIcon (in milli seconds)


setShowFirstIcon

public void setShowFirstIcon(boolean showFirstIcon)
Display the first icon when animation is finished. Otherwise the Icon that was visible when the animation stopped will remain visible.
Parameters:
showFirstIcon - true when the first icon is to be displayed, false otherwise


start

public void start()
Start the animation from the beginning.


stop

public void stop()
Stop the animation. The first icon will be redisplayed.


 

Groovy Documentation