Snake

Write a FrameWindowController class named Snake that behaves line the following applet:

Dragging the mouse draws a "snake" consisting of a sequence of line segments connecting the mouse drag points that are detected (and beginning with the mouse press point), but limited to the last 50 such line segments. The color of the line segments changes every 10 segments to a new randomly chosen color. On each mouse press, the previous snake is erased, if there was one.

Hints:

  1. Save the lines drawn in an array so the ones at the tail of the snake can be removed when the snake gets too long. Use the removeFromCanvas method.

  2. As always, try to develop the program in stages. For example, start by drawing snakes of arbitrary length, without changing colors or limiting the snake length, or deleting old snakes. That's pretty easy. Then add color changes, which is also pretty easy. After that, limit snake length (this is the core of the assignment). Finally, erase previous snakes when you start a new one.

  3. Since you only need to store the last 50 line segments (in order to be able to remove them when their time comes), you only need an array of 50 elements. By wrapping around from the end of the array to the beginning, you can avoid having to copy all the elements each time a line is removed. This is similar to the widely-used circular buffer data structure.