next up previous
Next: Resize the window Up: Programming assignment Previous: Ode to Joy

Make life more interesting

If you look at your program from the previous section, you will probably see that you have a lot of ``hard-coded'' constants, that is, coordinates that you have calculated on paper and then typed in to your program.

For some programs, that's ok, but it makes it very difficult to modify the program later. For example, if you wanted to make the rings bigger, you would have to go back and change all the numbers.

An alternative is to define a bounding box for the rings using variables, and calculate all the coordinates as a function of those variables.

1.
Create four integer variables named x, y, width and height, and set them to 0, 0, 400 and 250 (the size of the Applet canvas).
2.
Rewrite your drawOval statements so that all coordinates are expressed in terms of x, y, width and height.

3.
Test your program by changing the size and position of the bounding box and seeing if the rings get drawn correctly. You might want to use drawRect to draw the bounding box so you can confirm that the rings are always inside it.

4.
Make sure your program still works even if the bounding box is tall and narrow or short and wide. Of course, the rings won't be round, but they should still overlap correctly and be in the right places relative to each other.


next up previous
Next: Resize the window Up: Programming assignment Previous: Ode to Joy
Allen B. Downey
2/11/1998