/** * Sprite.java * @author Jeff Ondich, 5 November 2010 * * The abstract parent class for Sprites in the Playground example. */ import java.awt.*; public abstract class Sprite { protected int positionX; protected int positionY; private int velocityX; private int velocityY; public static Sprite createSprite(String spriteType, int x, int y, int vx, int vy, Color color) { if (spriteType.equals("square")) { return new SquareSprite(x, y, vx, vy, color); } else if (spriteType.equals("circle")) { return new CircleSprite(x, y, vx, vy, color); } else if (spriteType.equals("moose")) { return new MooseSprite(x, y, vx, vy, color); } else { return new SquareSprite(x, y, vx, vy, color); } } public Sprite(int x, int y, int vx, int vy) { this.positionX = x; this.positionY = y; this.velocityX = vx; this.velocityY = vy; } /** * Moves the Sprite one step in the direction and magnitude * of its velocity. Subclasses may want to supplement this * behavior (or not) by calling super.step() followed by * any desired additional stepping actions. */ public void step() { this.positionX += this.velocityX; this.positionY += this.velocityY; } /** * Draws the Sprite at its current position in the specified * Graphics environment. */ public abstract void draw(Graphics g); }