001 package com.croftsoft.apps.sprite;
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.ComponentUpdater;
009 import com.croftsoft.core.animation.sprite.IconSprite;
010
011 /*********************************************************************
012 * Switches the Icon based upon the Sprite heading.
013 *
014 * @version
015 * 2002-03-23
016 * @since
017 * 2002-02-24
018 * @author
019 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020 *********************************************************************/
021
022 public final class LeftRightUpdater
023 implements ComponentUpdater
024 //////////////////////////////////////////////////////////////////////
025 //////////////////////////////////////////////////////////////////////
026 {
027
028 private final IconSprite iconSprite;
029
030 private final Icon lookLeftIcon;
031
032 private final Icon lookRightIcon;
033
034 private final Rectangle repaintRegion;
035
036 //
037
038 private double oldHeading;
039
040 private Icon oldIcon;
041
042 //////////////////////////////////////////////////////////////////////
043 //////////////////////////////////////////////////////////////////////
044
045 public LeftRightUpdater (
046 IconSprite iconSprite,
047 Icon lookLeftIcon,
048 Icon lookRightIcon )
049 //////////////////////////////////////////////////////////////////////
050 {
051 NullArgumentException.check ( this.iconSprite = iconSprite );
052
053 NullArgumentException.check ( this.lookLeftIcon = lookLeftIcon );
054
055 NullArgumentException.check ( this.lookRightIcon = lookRightIcon );
056
057 repaintRegion = new Rectangle ( );
058 }
059
060 //////////////////////////////////////////////////////////////////////
061 //////////////////////////////////////////////////////////////////////
062
063 public void update ( JComponent component )
064 //////////////////////////////////////////////////////////////////////
065 {
066 double heading = iconSprite.getHeading ( );
067
068 if ( heading == oldHeading )
069 {
070 return;
071 }
072
073 oldHeading = heading;
074
075 Icon icon = lookRightIcon;
076
077 if ( ( heading > Math.PI / 2 )
078 && ( heading < 3 * Math.PI / 2 ) )
079 {
080 icon = lookLeftIcon;
081 }
082
083 if ( icon == oldIcon )
084 {
085 return;
086 }
087
088 iconSprite.getPaintBounds ( repaintRegion );
089
090 component.repaint ( repaintRegion );
091
092 iconSprite.setIcon ( icon );
093
094 oldIcon = icon;
095
096 iconSprite.getPaintBounds ( repaintRegion );
097
098 component.repaint ( repaintRegion );
099 }
100
101 //////////////////////////////////////////////////////////////////////
102 //////////////////////////////////////////////////////////////////////
103 }