001 package com.croftsoft.core.animation.sprite;
002
003 import java.awt.Graphics2D;
004 import java.awt.Rectangle;
005 import java.awt.Shape;
006 import javax.swing.Icon;
007 import javax.swing.JComponent;
008
009 import com.croftsoft.core.lang.NullArgumentException;
010 import com.croftsoft.core.animation.ComponentUpdater;
011 import com.croftsoft.core.animation.painter.NullComponentPainter;
012 import com.croftsoft.core.animation.updater.NullComponentUpdater;
013
014 /*********************************************************************
015 * A Sprite implementation backed by an Icon.
016 *
017 * @version
018 * 2002-07-11
019 * @since
020 * 2002-03-06
021 * @author
022 * <a href="https://www.croftsoft.com/">David Wallace Croft</a>
023 *********************************************************************/
024
025 public class IconSprite
026 extends AbstractSprite
027 //////////////////////////////////////////////////////////////////////
028 //////////////////////////////////////////////////////////////////////
029 {
030
031 private final Rectangle paintBounds;
032
033 //
034
035 private Icon icon;
036
037 //////////////////////////////////////////////////////////////////////
038 // constructor methods
039 //////////////////////////////////////////////////////////////////////
040
041 public IconSprite (
042 double x,
043 double y,
044 double z,
045 double heading,
046 double velocity,
047 ComponentUpdater componentUpdater,
048 Icon icon )
049 //////////////////////////////////////////////////////////////////////
050 {
051 super ( x, y, z, heading, velocity, componentUpdater,
052 NullComponentPainter.INSTANCE );
053
054 paintBounds = new Rectangle ( );
055
056 setX ( x );
057
058 setY ( y );
059
060 setIcon ( icon );
061 }
062
063 public IconSprite ( Icon icon )
064 //////////////////////////////////////////////////////////////////////
065 {
066 this ( 0.0, 0.0, 0.0, 0.0, 0.0,
067 NullComponentUpdater.INSTANCE, icon );
068 }
069
070 //////////////////////////////////////////////////////////////////////
071 // accessor methods
072 //////////////////////////////////////////////////////////////////////
073
074 public Icon getIcon ( ) { return icon; }
075
076 public Shape getCollisionShape ( )
077 //////////////////////////////////////////////////////////////////////
078 {
079 return paintBounds;
080 }
081
082 public void getCollisionBounds ( Rectangle collisionBounds )
083 //////////////////////////////////////////////////////////////////////
084 {
085 collisionBounds.setBounds ( paintBounds );
086 }
087
088 public void getPaintBounds ( Rectangle paintBounds )
089 //////////////////////////////////////////////////////////////////////
090 {
091 paintBounds.setBounds ( this.paintBounds );
092 }
093
094 //////////////////////////////////////////////////////////////////////
095 // mutator methods
096 //////////////////////////////////////////////////////////////////////
097
098 public void setX ( double x )
099 //////////////////////////////////////////////////////////////////////
100 {
101 super.setX ( x );
102
103 paintBounds.x = ( int ) x;
104 }
105
106 public void setY ( double y )
107 //////////////////////////////////////////////////////////////////////
108 {
109 super.setY ( y );
110
111 paintBounds.y = ( int ) y;
112 }
113
114 public void setIcon ( Icon icon )
115 //////////////////////////////////////////////////////////////////////
116 {
117 NullArgumentException.check ( this.icon = icon );
118
119 paintBounds.width = icon.getIconWidth ( );
120
121 paintBounds.height = icon.getIconHeight ( );
122 }
123
124 //////////////////////////////////////////////////////////////////////
125 //////////////////////////////////////////////////////////////////////
126
127 public void paint (
128 JComponent component,
129 Graphics2D graphics )
130 //////////////////////////////////////////////////////////////////////
131 {
132 icon.paintIcon ( component, graphics, ( int ) x, ( int ) y );
133 }
134
135 //////////////////////////////////////////////////////////////////////
136 //////////////////////////////////////////////////////////////////////
137 }