001    package breakout;
002    
003    import java.awt.*;
004    import java.awt.geom.*;
005    import cs101.awt.geom.ShapeUtils;
006    
007    /**
008     * abstract class DefaultBreakoutComponent provides a partial
009     * implementation of the BreakoutComponent interface.
010     * It manages the position, size, shape, death, and transient parts
011     * of the BreakoutComponent, leaving the implementation of
012     * paint and hitBy to the subclass.  The shape is a rectangle.
013     *
014     * @see breakout.BreakoutComponent
015     * @see breakout.BenSimpleBrick
016     *
017     * @author benmv@olin.edu
018     * @version <tt>$Id: DefaultBreakoutComponent.java,v 1.2 2004/03/26 20:39:33 gus Exp $</tt>
019     */
020    public abstract class DefaultBreakoutComponent implements BreakoutComponent {
021    
022      /** location of component */
023      private Point location;
024    
025      /** size of component */
026      private Dimension size;
027    
028      /** dead? */
029      private boolean dead;
030    
031      /** world the component lives in */
032      World world;
033    
034      /**
035       * Default constructor creates the component at a given location
036       * and size.
037       *
038       * @param location Initial location of component.
039       * @param size Initial size of component.
040       * @param w World the component lives in.
041       */
042      public DefaultBreakoutComponent(Point location, Dimension size, World w) {
043        this.size = new Dimension(size);
044        this.location = new Point(location);                
045        this.world = w;
046        this.dead = false;
047      }
048      
049      /**
050       * without world constructor.
051       */
052      public DefaultBreakoutComponent(Point location, Dimension size) {
053            this(location,size,null);
054      }
055         
056      /**
057       * returns a new Point corresponding to the location of the 
058       * component's upper-left corner.
059       * Changing the returned point will <B>NOT</B> result in the 
060       * component's location changing.  To change the location of this
061       * component, use setLocation. 
062       *
063       * @return New Point corresponding to location of component.
064       */
065      public Point getLocation() {
066        return new Point(this.location);
067      }
068    
069      /**
070       * changes the location of the component's upper left corner to
071       * the given location.
072       * Copies the given point, so subsequent changes to the Point 
073       * provided will not affect the location of the compenent.
074       *
075       * @param location Point to locate the component at.
076       */
077      public void setLocation(Point location) {
078        this.location = new Point(location);
079      }
080    
081      /**
082       * returns a point that corresponds to the center of the
083       * component.  getLocation() returns upper-left corner.
084       *
085       * @return Point located at the center of the component.
086       */
087      public Point getCenter() {
088        return new Point( this.location.x + this.size.width / 2,
089                          this.location.y + this.size.height / 2 );
090      }    
091        
092      /**
093       * returns the Dimensions of the component.  Mutating the
094       * returned value will <B>NOT</B> change the size of the component.
095       *
096       * @return Dimension of the component.
097       */
098      public Dimension getSize() {
099        return new Dimension(this.size);
100      }
101    
102      /**
103       * returns a Rectangle at location with size.
104       *
105       * @return Shape Rectangle according to components location and
106       * size.
107       */
108      public Shape getShape() {
109        Point loc = getLocation();
110            Dimension size = getSize();
111        return new Rectangle2D.Float(loc.x,loc.y,size.width,size.height);
112      }
113    
114      public void kill() {
115        this.dead = true;
116      }
117    
118      public boolean isDead() {
119        return this.dead;
120      }
121        
122      /**
123       * The default component is not transient.
124       *
125       * @return false by default.
126       */
127      public boolean isTransient() {
128        return false;
129      }
130    
131      public void setWorld(World world) {
132        this.world = world;
133      }
134    
135      /**
136       * subclasses must implement the paint method in order to draw
137       * their component.
138       *
139       * @param g Graphics object used to draw with.
140       */
141      public abstract void paint( Graphics g );
142        
143      /**
144       * subclasses must implement the hitBy method to have the
145       * component react to being struck.
146       *
147       * @param striker BreakoutComponent that struck this component
148       */
149      public abstract void hitBy( BreakoutComponent striker );
150    
151    }
152    
153    /*
154     * $Log: DefaultBreakoutComponent.java,v $
155     * Revision 1.2  2004/03/26 20:39:33  gus
156     * take ownership
157     *
158     * Revision 1.1  2003/07/10 21:33:47  jon
159     * Initial commit of spring03 breakout source.
160     *
161     * Revision 1.6  2003/03/07 19:53:51  bvandiver
162     * formatting and WorldState cleanup
163     *
164     * Revision 1.5  2003/03/07 17:50:22  bvandiver
165     * documented most of code
166     *
167     * Revision 1.4  2003/03/07 17:05:26  gus
168     * now make our shape a precise shape.
169     *
170     * Revision 1.3  2003/03/07 16:48:59  gus
171     * added a constructor for shape based components and generalized the
172     * getShape method.
173     *
174     */
175