For many applications, particularly games, action is displayed by painting and repainting a Canvas.
If you're very much into games, this SpaceInvaders tutorial will teach you a lot. Our use of the canvas will be more modest.
Remember that answers to exercises in these in-class manuals are to be entered into your electronic lab workbook.
There are many problems with the code you downloaded. Nevertheless, you should study it and understand it thoroughly.
First of all, note that the file contains three class definitions. One is the public class whose name, as per the Java rules, matches the file name. The others are Segment and ScribbleCanvas.
We have not discussed Java packages yet, but if you know about them, feel free to mention them in your answers to the previous exercise.
Look at the diagram showing class associations:
You will observe that Ex2 has three anonymous inner classes. These are the listeners. Because they are inner classes, they can directly refer to instance variables in Ex2. So, foir example, the MouseListener can directly modify lastX and lastY.
But the ActionListener on the clear button needs access to methods of the ScribbleCanvas. The MouseMotionListener needs access to the ScribbleCanvas and also needs to create Segment objects.
Bearing in mind what you have learned from the above exercises, can you do better? The kind of thing you can do include:
Try to sketch a design for a better solution on paper or in a drawing program. A better solution will have simpler associations and will minimize information transfer at runtime. When you are ready:
And after you've designed, you can code. There is actually very little code you need to generate from scratch for: