001 package breakout; 002 003 import java.awt.*; 004 import java.awt.geom.*; 005 006 /** 007 * SimpleBall is an active component that bounces around hitting 008 * things. 009 * 010 * @see breakout.ActiveBreakoutComponent 011 * 012 * @author benmv@olin.edu 013 * @version <tt>$Id: SimpleBall.java,v 1.2 2004/03/26 20:39:33 gus Exp $</tt> 014 */ 015 public class SimpleBall extends DefaultActiveBreakoutComponent 016 implements Ball { 017 018 Color color; 019 BreakoutComponent lastStruck; 020 021 public SimpleBall(Point location, Dimension size, World w, 022 Point direction, int timeBetweenActs, Color c) { 023 super(location,size,w,direction,timeBetweenActs); 024 this.color = c; 025 } 026 027 public SimpleBall(Point location, Dimension size, World w, 028 double theta, int timeBetweenActs, Color c) { 029 this(location,size,w, 030 new Point((int)(Math.cos(theta)*100), 031 (int)(Math.sin(theta)*100)), 032 timeBetweenActs, c); 033 } 034 035 // override getShape for circular shape 036 public Shape getShape() { 037 Point loc = getLocation(); 038 Dimension size = getSize(); 039 return new Ellipse2D.Float(loc.x,loc.y,size.width,size.height); 040 } 041 042 public void paint(Graphics g) { 043 g.setColor(this.color); 044 Dimension size = getSize(); 045 g.fillOval(0,0,size.width,size.height); 046 } 047 048 public void hitBy(BreakoutComponent striker) { 049 } 050 051 public void act() { 052 BreakoutComponent hitting = this.world.intersects(this); 053 if (hitting != null && !hitting.isTransient() && 054 hitting != lastStruck) { 055 lastStruck = hitting; 056 if (hitting instanceof SimpleBall) 057 ((SimpleBall)hitting).lastStruck = this; 058 hitting.hitBy(this); 059 this.world.rebound(this,hitting); 060 } 061 062 } 063 } 064