We will practice the design patterns from sections 5.4 - 5.6 of the textbook.
ch04/animation
project and the modification of it in Lab 8 that allows you to animate multiple shapes.MoveableShape
:/** * Yields the bounding rectangle of this shape. * @return the bounding rectangle */ Rectangle getBounds();
Car
and MoveableIcon
classes.BoxedShape
with a constructor
public BoxedShape(MoveableShape shape, int gap)that, when drawn, yields the original shape with a rectangle along its bounds if
gap
is zero, or with as many pixels between the bounds and the rectangle on each side as given by gap
. It's a decorator, so you should be able to apply it twice:
new BoxedShape(new BoxedShape(new CarShape(...), 0), 5)
CompoundShape
draws all of its shapes, moves each of them, and has a bounding box that is the smallest rectangle containing all individual bounding boxes. Provide a constructor
public CompoundShape(MoveableShape... shapes)Note the varargs parameter. You should be able to call
new CompoundShape(new BoxedShape(...), new CarShape(...), new MoveableIcon(...))
AnimationTester
simply moves all moveable shapes in each timer tick. Suppose we want it to do something more sophisticated, like stopping shapes that reach the boundary. That would be a different strategy. Provide an interface MoveStrategy
with an abstract method
void process(List<MoveableShape> shapes)
SimpleMoveStrategy
that does what's currently done in AnimationTester
, and a class BoundedMoveStrategy
that only moves shapes whose bounds are contained inside a Rectangle
that is given in the constructor.AnimationTester
to Animation
. Turn the main
method into a method
public static void show(List<MoveableShape> shapes, MoveStrategy strategy, int width, int height)I will call that method from my test cases. Here is an example. Make sure that it compiles with your classes with no change.
import java.awt.Rectangle; import java.util.ArrayList; import java.util.List; public class AnimationTester { public static void main(String[] args) { final int CAR_WIDTH = 100; List<MoveableShape> shapes = new ArrayList<>(); shapes.add(new BoxedShape(new CompoundShape(new CarShape(200, 20, CAR_WIDTH), new MoveableIcon("dog.png", 100, 10), new MoveableIcon("dog.png", 150, 100)), 0)); Animation.show(shapes, new BoundedMoveStrategy(new Rectangle(0, 0, 500, 200)), 600, 200); } }