LinkedLists: what targets, scribbles, and chain
reactions have in commonTopChain Reaction

Chain Reaction

Try out this DEMO:

ChainReaction demo

With each click of the mouse, a ball is drawn on the canvas. When the mouse exits the canvas, the balls move, resulting in an animation that looks like a chain reaction. When the mouse enters the canvas, the canvas is cleared and we can start all over.

A set of classes similar to those in the scribble example above will give us that behavior. We will call the interface BallListIfc, while the classes representing empty and non-empty lists will be named EmptyBallList and BallList. The only changes we must make are to change some of the types. For example, the instance variable first must have type FilledOval rather than Line. Similarly instance variable rest will have type BallListIfc. The other minor changes to method types follow from these.

Because we want this "chain reaction" to be animated, we will have to set up an Active Object to manage the animation. We call the class that manages the animation, AnimateBalls:

If you examine the code in AnimateBalls, you will see that the run method calls a method moveThemBalls that is recursive in the sense that it calls itself on a smaller collection of balls. We will see more examples of this below.


LinkedLists: what targets, scribbles, and chain
reactions have in commonTopChain Reaction