import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; /* * Represents asteroids in the game. */ public class Asteroid extends GameObject { private int type; // Type of asteroid: Level 3 is biggest, level 1 is smallest public Asteroid(double locationX, double locationY, double speedX, double speedY, double sizeX, double sizeY, int type) { super(locationX, locationY, speedX, speedY, sizeX, sizeY); this.type = type; } public Asteroid explode() { if (type <= 1) // Only explode asteroids greater than type 1 return null; int newType = type-1; Asteroid asteroid = new Asteroid(this.locationX, this.locationY, this.speedX, this.speedY, newType*10, newType*10, newType); return asteroid; } public String toString() { StringBuffer buf = new StringBuffer(100); buf.append(super.toString()); buf.append(" Type: "+type); return buf.toString(); } public int getType() { return type; } public void setType(int type) { this.type = type; } public void draw(Graphics2D g2) { int maxw = (int) sizeX, maxh = (int) sizeY; int x = (int) locationX, y = (int) locationY; // The size of the asteroid is determined by its type. // Level 3 asteroid is blue and size is 60,60 // Level 2 asteroid is yellow and size is 40,40 // Level 1 asteroid is green and size is 20,20 // TODO: Draw the asteroid use the Ellipse2D.Double class // You just need to set the correct color for g2. Ellipse2D.Double circle = new Ellipse2D.Double(x, y, maxh, maxw); g2.fill(circle); } }