001 package breakout; 002 003 import java.awt.*; 004 import java.awt.event.*; 005 006 /** A basic implementation of the Paddle interface. **/ 007 public class BasicPaddle implements Paddle, MouseMotionListener { 008 /** The size of this BasicPaddle. **/ 009 protected Dimension size; 010 /** The location of this BasicPaddle. **/ 011 protected Point location; 012 013 /** The Board containing this BasicPaddle **/ 014 protected Board board; 015 016 /** Leftmost point able to be occupied by this BasicPaddle. **/ 017 protected final int westmost; 018 /** Rightmost point able to be occupied by this BasicPaddle. **/ 019 protected final int eastmost; 020 021 /** Creates a BasicPaddle of a default size and location, belonging to a specified Board. 022 * @param b the Board object of which this BasicPaddle is a member. 023 **/ 024 public BasicPaddle(Board b) { 025 this.board = b; 026 this.size = new Dimension(60,10); 027 this.location = new Point(b.getLowerLeft(true).x + b.getWidth(true)/2 - this.size.width/2, 028 b.getLowerLeft(true).y - this.size.height); 029 this.westmost = b.getLowerLeft(false).x; 030 this.eastmost = b.getLowerRight(false).x - this.size.width; 031 032 b.getBoardPanel().addMouseMotionListener(this); 033 } 034 035 /** Inactive. BasicPaddle manages its own location. **/ 036 public void setLocation(Point p) {} 037 /** Gets the current location of this BasicPaddle. 038 * @return a new Point object representing the current location of this BasicPaddle. 039 **/ 040 public Point getLocation() { 041 return new Point(this.location.x, this.location.y); 042 } 043 044 /** Gets the size of this BasicPaddle. 045 * @return a new Dimension object representing the size of this BasicPaddle. 046 **/ 047 public Dimension getSize() { 048 return new Dimension(this.size.width, this.size.height); 049 } 050 051 /** Called by World every tick; does nothing. **/ 052 public void update() {} 053 054 /** Called by a Rebounding BreakoutComponent when it has detected an intersection 055 * with this BasicPaddle. 056 * @param bc the Rebounding/BreakoutComponent object that detected the hit. 057 * @return <code>false</code> -- a BasicPaddle doesn't die when hit. 058 **/ 059 public boolean hit(BreakoutComponent bc) { 060 return false; 061 } 062 063 /** Gets the java.awt.Shape of this BasicPaddle. 064 * @return a new Shape label at the same location and of the same size/shape as this BasicPaddle. 065 **/ 066 public Shape getShape() { 067 return new Rectangle(this.location, this.size); 068 } 069 070 /** Paints the BasicPaddle. 071 * @param g Graphics object for this BasicPaddle's coordinate frame(ie, already located where we are). 072 **/ 073 public void paint(Graphics g) { 074 g.setColor(Color.darkGray); 075 g.fillRect(0,0,this.size.width,this.size.height); 076 } 077 078 /** (unused) **/ 079 public void mouseDragged(MouseEvent me) {} 080 /** Listens to mouse movements, and sets the x-coordinate of the BasicPaddle accordingly. 081 * Keeps the BasicPaddle between <code>this.westmost</code> and <code>this.eastmost</code>. 082 **/ 083 public void mouseMoved(MouseEvent me) { 084 if(me.getX() > this.westmost && me.getX() < this.eastmost) { 085 this.location.setLocation(me.getX(), this.location.y); 086 } 087 } 088 }