Canvas

Chapter: Canvas

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.


Exercise 2

Obtain this code either by clicking and copying and pasting or by locating it on Titan. Compile and run it. Study it carefully. The next few exercises will ask you about its design and challenge you to improve it.

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.


Exercise 3

Many practitioners do not like to see more than one class in a file. How do you feel about this? Feel free to google for others' opinions. Try to come up with specific

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.


Exercise 4

Separate the classes into three files. Keep them in the same directory (or folder).

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.


Exercise 5

Is it good to have so many associations? Is it bad? Discuss.


Exercise 6

Draw a similar diagram indicating parameter passing and communication between methods in the various classes. As you draw links indicating parameter passing, try to annotate your links with a number -- indicating how often you think information needs to pass along that particular pipeline. Try to base your numbers on a user drawing one scribble in about 10 seconds. You may discuss the source of these numbers with your classmates.

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:


Exercise 7

Draw a better design for the Scribble application. Draw it in a drawing program, and put a copy in your notebook.

And after you've designed, you can code. There is actually very little code you need to generate from scratch for:


Exercise 8

Implement your design from the previous exercise. It's ok to cut and paste code from my application.


rhyspj@gwu.edu