GUI PROGRAMMING 109

3.8. GUI PROGRAMMING 109

} } The “import java.awt.*;” is required to get access to graphics-related classes such as

Graphics and Color. You get to fill in any name you want for the class, and you get to fill in the statements inside the subroutine. The drawFrame() subroutine will be called by the system each time a frame needs to be drawn. All you have to do is say what happens when this subroutine is called. Of course, you have to draw a different picture for each frame, and to do that you need to know which frame you are drawing. The class SimpleAnimationApplet2 provides a function named getFrameNumber() that you can call to find out which frame to draw. This function returns an integer value that represents the frame number. If the value returned is 0, you are supposed to draw the first frame; if the value is 1, you are supposed to draw the second frame, and so on.

In the sample applet, the thing that differs from one frame to another is the distance between the edges of the applet and the outermost rectangle. Since the rectangles are 15 pixels apart, this distance increases from 0 to 14 and then jumps back to 0 when a “new” rectangle appears. The appropriate value can be computed very simply from the frame number, with the statement “inset = getFrameNumber() % 15;”. The value of the expression getFrameNumber() % 15 is between 0 and 14. When the frame number reaches 15 or any multiple of 15, the value of getFrameNumber() % 15 jumps back to 0.

Drawing one frame in the sample animated applet is very similar to drawing the single image of the StaticRects applet, as given above. The paint() method in the StaticRects applet becomes, with only minor modification, the drawFrame() method of my MovingRects animation applet. I’ve chosen to make one improvement: The StaticRects applet assumes that the applet is 300 by 160 pixels. The MovingRects applet will work for any applet size. To implement this, the drawFrame() routine has to know how big the applet is. There are two functions that can be called to get this information. The function getWidth() returns an integer value representing the width of the applet, and the function getHeight() returns the height. The width and height, together with the frame number, are used to compute the size of the first rectangle that is drawn. Here is the complete source code:

import java.awt.*; public class MovingRects extends SimpleAnimationApplet2 {

public void drawFrame(Graphics g) { // Draw one frame in the animation by filling in the background

// with a solid red and then drawing a set of nested black // rectangles. The frame number tells how much the first // rectangle is to be inset from the borders of the applet.

int width;

// Width of the applet, in pixels.

int height;

// Height of the applet, in pixels.

int inset; // Gap between borders of applet and a rectangle. //

The inset for the outermost rectangle goes

from 0 to 14 then back to 0, and so on,

as the frameNumber varies.

int rectWidth, rectHeight; // The size of one of the rectangles. width = getWidth();

// Find out the size of the drawing area.