001 package com.croftsoft.core.animation.updater;
002
003 import java.awt.Rectangle;
004 import javax.swing.Icon;
005 import javax.swing.JComponent;
006
007 import com.croftsoft.core.lang.NullArgumentException;
008 import com.croftsoft.core.animation.Clock;
009 import com.croftsoft.core.animation.ComponentUpdater;
010 import com.croftsoft.core.animation.sprite.IconSprite;
011
012 /*********************************************************************
013 * Rotates through a sequence of Icons.
014 *
015 * @version
016 * 2003-07-11
017 * @since
018 * 2002-03-15
019 * @author
020 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
021 *********************************************************************/
022
023 public final class IconSequenceUpdater
024 implements ComponentUpdater
025 //////////////////////////////////////////////////////////////////////
026 //////////////////////////////////////////////////////////////////////
027 {
028
029 private final IconSprite iconSprite;
030
031 private final Icon [ ] icons;
032
033 private final long framePeriodNanos;
034
035 private final Clock clock;
036
037 private final Rectangle oldPaintBounds;
038
039 private final Rectangle newPaintBounds;
040
041 //
042
043 private long lastUpdateTimeNanos;
044
045 private int index;
046
047 //////////////////////////////////////////////////////////////////////
048 //////////////////////////////////////////////////////////////////////
049
050 public IconSequenceUpdater (
051 IconSprite iconSprite,
052 Icon [ ] icons,
053 long framePeriodNanos,
054 Clock clock )
055 //////////////////////////////////////////////////////////////////////
056 {
057 NullArgumentException.check ( this.iconSprite = iconSprite );
058
059 NullArgumentException.check ( this.icons = icons );
060
061 this.framePeriodNanos = framePeriodNanos;
062
063 NullArgumentException.check ( this.clock = clock );
064
065 oldPaintBounds = new Rectangle ( );
066
067 newPaintBounds = new Rectangle ( );
068 }
069
070 //////////////////////////////////////////////////////////////////////
071 //////////////////////////////////////////////////////////////////////
072
073 public void update ( JComponent component )
074 //////////////////////////////////////////////////////////////////////
075 {
076 long updateTimeNanos = clock.currentTimeNanos ( );
077
078 if ( updateTimeNanos < lastUpdateTimeNanos + framePeriodNanos )
079 {
080 return;
081 }
082
083 lastUpdateTimeNanos = updateTimeNanos;
084
085 iconSprite.getPaintBounds ( oldPaintBounds );
086
087 iconSprite.setIcon ( icons [ index ] );
088
089 iconSprite.getPaintBounds ( newPaintBounds );
090
091 newPaintBounds.add ( oldPaintBounds );
092
093 component.repaint ( newPaintBounds );
094
095 index = ( index + 1 ) % icons.length;
096 }
097
098 //////////////////////////////////////////////////////////////////////
099 //////////////////////////////////////////////////////////////////////
100 }