next up previous
Next: Clean up and turn Up: Programming assignment Previous: Make life more interesting

Resize the window

Java allows you to change the size of the window while the program is running. Each time this happens, the Applet runner will invoke your paint method, letting you know that the size of the window has changed. Your program can determine the size of the window by invoking the bounds method. The following code will do the trick:

     // get the width and height of the window
     Rectangle boundingBox = bounds();
     width = boundingBox.width;
     height = boundingBox.height;

1.
Add this code to your program, at the beginning of paint, which is where I assume you are setting x, y, width and height. You can set x and y to 0.
2.
Now run the program again, and when the window appears, grab the resize handle in the lower right-hand corner and resize the window. The rings should get redrawn at the new size and aspect ratio.



Allen B. Downey
2/11/1998