Turtle | ---------+--------- | | Simple Animated Turtle Turtle Turtle ------------------------ (components/ivars) shape (behaviors/methods) moveBy* scale* setColor* setStyle* turn forward SimpleTurtle ------------------------ drawOn* AnimatedTurtle ------------------------ drawAnimationOn *delegated method Component "shape" is object type "Shape"
public class Turtle { //... } public class SimpleTurtle extends Turtle { //... } public class AnimatedTurtle extends Turtle { //... }
public class Turtle { //--------------------------------------------------- // Instance Vars private Shape shape; //... }
//--------------------------------------------------- // Instance Methods (Delegated) public void moveBy(Point vector) { // Delegate to component "shape" this.shape.moveBy(vector); } public void scale(double factor) { // Delegate to component "shape" this.shape.scale(factor); } public void setColor(Color color) { // Delegate to component "shape" this.shape.setColor(color); } public void setStyle(String style) { // Delegate to component "shape" this.shape.setStyle(style); }
//--------------------------------------------------- // Instance Methods (Coded) public void turn(int angleAsDegrees) { this.direction = new Direction(angleAsDegrees); } public void forward(int aDistance) { Point p1, p2; p1 = this.location; p2 = this.nextPointFor(aDistance); p1.drawTo(p2, this.pen, this.getColor()); this.location = p2; } boolean isAnimated() { return this.rest > 0; }
package model.turtle; import java.awt.Graphics2D; public class SimpleTurtle extends Turtle { public void drawOn(Graphics2D canvas) { this.getShape().drawOn(canvas); } }
package model.turtle; import java.awt.Graphics2D; public class AnimatedTurtle extends Turtle { public void drawAnimationOn(Graphics2D canvas) { this.getShape().drawAnimationOn(canvas); } @Override public void forward(int aDistance) { //We are animated, so we want to pause between steps to animate sleep(this.getRest()); super.forward(aDistance); } }
/** Set shape to circle with provided center (x, y) and provided radius */ public void setAsCircular(int x, int y, int radius) { this.shape = Circle.fromXYRadius(x, y, radius); } /** Set shape to rectangle with provided leftX, topY, width and height */ public void setAsRectangular(int leftX, int topY, int width, int height) { this.shape = Rectangle.fromLeftTopWidthHeight(leftX, topY, width, height); } /** Set shape to ellipse with provided center (centerX, centerY) and major axis and minor axis (a and b) */ public void setAsElliptical(int centerX, int centerY, int a, int b) { this.shape = Ellipse.fromCenterAB(400, 400, 100, 50); }
SimpleTurtle simpleTurtle = new SimpleTurtle(); simpleTurtle.setAsCircular(200, 100, 5); simpleTurtle.drawOn((Graphics2D)g); simpleTurtle = new SimpleTurtle(); simpleTurtle.setAsRectangular(100, 50, 300, 200); simpleTurtle.drawOn((Graphics2D)g); AnimatedTurtle animatedTurtle = new AnimatedTurtle(); animatedTurtle.setAsElliptical(500, 300, 20, 10); animatedTurtle.drawAnimationOn((Graphics2D)g);