001 package com.croftsoft.core.animation.sprite;
002
003 import java.awt.Graphics2D;
004 import java.awt.Rectangle;
005 import javax.swing.JComponent;
006
007 import com.croftsoft.core.animation.ComponentPainter;
008 import com.croftsoft.core.animation.ComponentUpdater;
009 import com.croftsoft.core.animation.Sprite;
010 import com.croftsoft.core.animation.painter.NullComponentPainter;
011 import com.croftsoft.core.animation.updater.NullComponentUpdater;
012 import com.croftsoft.core.lang.NullArgumentException;
013
014 /*********************************************************************
015 * An abstract Sprite implementation.
016 *
017 * @version
018 * 2003-07-12
019 * @since
020 * 2002-03-07
021 * @author
022 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
023 *********************************************************************/
024
025 public abstract class AbstractSprite
026 implements Sprite
027 //////////////////////////////////////////////////////////////////////
028 //////////////////////////////////////////////////////////////////////
029 {
030
031 protected double x;
032
033 protected double y;
034
035 protected double z;
036
037 protected double heading;
038
039 protected double velocity;
040
041 protected ComponentUpdater componentUpdater;
042
043 protected ComponentPainter componentPainter;
044
045 //////////////////////////////////////////////////////////////////////
046 // constructor methods
047 //////////////////////////////////////////////////////////////////////
048
049 public AbstractSprite (
050 double x,
051 double y,
052 double z,
053 double heading,
054 double velocity,
055 ComponentUpdater componentUpdater,
056 ComponentPainter componentPainter )
057 //////////////////////////////////////////////////////////////////////
058 {
059 this.x = x;
060
061 this.y = y;
062
063 this.z = z;
064
065 this.heading = heading;
066
067 this.velocity = velocity;
068
069 setComponentUpdater ( componentUpdater );
070
071 setComponentPainter ( componentPainter );
072 }
073
074 public AbstractSprite ( )
075 //////////////////////////////////////////////////////////////////////
076 {
077 this ( 0.0, 0.0, 0.0, 0.0, 0.0, null, null );
078 }
079
080 //////////////////////////////////////////////////////////////////////
081 // accessor methods
082 //////////////////////////////////////////////////////////////////////
083
084 public double getX ( ) { return x; }
085
086 public double getY ( ) { return y; }
087
088 public double getZ ( ) { return z; }
089
090 public double getHeading ( ) { return heading; }
091
092 public double getVelocity ( ) { return velocity; }
093
094 public void getCollisionBounds ( Rectangle collisionBounds )
095 //////////////////////////////////////////////////////////////////////
096 {
097 collisionBounds.setBounds ( getCollisionShape ( ).getBounds ( ) );
098 }
099
100 //////////////////////////////////////////////////////////////////////
101 // mutator methods
102 //////////////////////////////////////////////////////////////////////
103
104 public void setX ( double x ) { this.x = x; }
105
106 public void setY ( double y ) { this.y = y; }
107
108 public void setZ ( double z ) { this.z = z; }
109
110 public void setHeading ( double heading )
111 //////////////////////////////////////////////////////////////////////
112 {
113 this.heading = heading;
114 }
115
116 public void setVelocity ( double velocity )
117 //////////////////////////////////////////////////////////////////////
118 {
119 this.velocity = velocity;
120 }
121
122 //////////////////////////////////////////////////////////////////////
123 //////////////////////////////////////////////////////////////////////
124
125 public void setComponentUpdater (
126 ComponentUpdater componentUpdater )
127 //////////////////////////////////////////////////////////////////////
128 {
129 if ( componentUpdater == null )
130 {
131 componentUpdater = NullComponentUpdater.INSTANCE;
132 }
133
134 this.componentUpdater = componentUpdater;
135 }
136
137 public void setComponentPainter (
138 ComponentPainter componentPainter )
139 //////////////////////////////////////////////////////////////////////
140 {
141 if ( componentPainter == null )
142 {
143 componentPainter = NullComponentPainter.INSTANCE;
144 }
145
146 this.componentPainter = componentPainter;
147 }
148
149 //////////////////////////////////////////////////////////////////////
150 // interface ComponentAnimator methods
151 //////////////////////////////////////////////////////////////////////
152
153 public void update ( JComponent component )
154 //////////////////////////////////////////////////////////////////////
155 {
156 componentUpdater.update ( component );
157 }
158
159 public void paint (
160 JComponent component,
161 Graphics2D graphics )
162 //////////////////////////////////////////////////////////////////////
163 {
164 componentPainter.paint ( component, graphics );
165 }
166
167 //////////////////////////////////////////////////////////////////////
168 //////////////////////////////////////////////////////////////////////
169 }