001 package breakout; 002 003 import java.awt.*; 004 005 /** Example of Brick subclassing. This subclass of BasicBrick 006 * keeps track of a certain number of "hit points," and takes 007 * that many hits before it dies. 008 **/ 009 public class HitpointsBrick extends BasicBrick { 010 private int hp; 011 /** Creates a new HitpointsBrick with the specified dimensions. 012 * @param width The intended width of the HitpointsBrick. 013 * @param height The intended height of the HitpointsBrick. 014 **/ 015 public HitpointsBrick(int width, int height) { 016 super(width, height); 017 this.hp=3; 018 } 019 020 /** Determines whether this HitpointsBrick has been hit enough times yet to die. 021 * @param bc Rebounding/BreakoutComponent that hit this HitpointsBrick. 022 * @return <code>true</code> if we died; <code>false</code> otherwise. 023 **/ 024 public boolean hit(BreakoutComponent bc) { 025 this.hp -= 1; 026 if(this.hp < 0) return true; 027 return false; 028 } 029 030 /** Paints this HitpointsBrick. **/ 031 public void paint(Graphics g) { 032 g.setColor(Color.orange); 033 g.drawRect(0,0,super.getSize().width, super.getSize().height); 034 switch(this.hp) { 035 case 0: return; 036 case 3: 037 g.setColor(Color.green); 038 break; 039 case 2: 040 g.setColor(Color.yellow); 041 break; 042 case 1: 043 g.setColor(Color.red); 044 break; 045 } 046 int h = super.getSize().height; 047 g.fillOval(super.getSize().width/2 - (int)(h/4), h/2 - (int)(h/4), (int)(h/2), (int)(h/2)); 048 } 049 }