Let's assume you've got Ball World working. You know you'll be asked to change its performance. Maybe your boss is going to ask for balls of selected color. Maybe your customers want to change the shapes of the balls. Maybe somebody wants to replace images of balls bouncing around by real images -- maybe a world where a lot of heads are moving around bouncing off of the walls. Maybe your company is approached by another company who would like to see your balls behaving more as planetary objects, perhaps going into orbits around some suns? Perhaps you want the balls to behave like proper balls following parabolas because of a uniform gravitational field. Perhaps at some stage you'll need to detect collisions and have balls bounce off of each other.
The code provided for the CSci 53 lab is not well suited to this kind of modification. In this lab I want you to rewrite the classes so that modifications to appearance (like color, size, shape, image) would be easy to makel and so that modifications to the laws of motion can be easily made.
As discussed in lecture there are good reasons to encapsulate the motion behavior in a separate class that implements a MotionBehavior interface; and to encapsulate the appearance of the "balls" in a separate class that implements an AppearanceBehavior interface. Here is my code for those two java interfaces:
public interface AppearanceBehavior { void update(Ball b); void draw(Ball ball, Graphics g); } public interface MotionBehavior { void update(Ball b); }You are welcome, as always, to use my code or to create your own.
private AppearanceBehavior appearance; private MotionBehavior motion;
Your constructor for the Ball class needs to set quite a lot of attributes. Here is the header for my constructor. Yours may vary:
public Ball(int x, int y, int xv, int yv, int d, AppearanceBehavior look, MotionBehavior move, Screen world)
Here are some suggestions that you may use or ignore as you wish: