Your Names:

6.831 • User Interface Design and Implementation

Massachusetts Institute of Technology
Department of Electrical Engineering and Computer Science
Fall Semester, 2006

AC13: Animation

The purpose of this activity is to learn more about programming animations in Swing. You will need to download http://courses.csail.mit.edu/6.831/handouts/ac13/ac13-code.zip, which contains the Java code you'll be using. If you use Eclipse, you can create a project directly from the ZIP file using File / Import / Existing Projects into Workspace / Select archive file.

1 How Not to Animate

Open Problem1.java and run it. Clicking on the window should move the rectangle through an animation from one side of the window to the other. What's wrong with the way the animation was implemented? (Hint: try to use the window's menubar while the animation is in progress. Why is the menubar broken?)

Look at the animate() method carefully. Notice that it calls paintImmediately() to paint each frame of the animation. Why does it also call repaint() at the end of the method?

2 Tight Loop Animation

Open Problem2.java. Problem2 is a subclass of Problem1, and it overrides the animate() method with a different implementation that uses a tight loop to do the animation as smoothly as possible. Finish the code by filling in the part marked YOUR CODE HERE. The rectangle should follow the same path as in Problem1. Write the code you added in the space below:

Does Problem2 suffer from the same drawback you identified in Problem1? What else is undesirable about Problem2's way of implementing animation? (Hint: press the mouse button a bunch of times to get the animation to run for a while, and then switch to another application, maybe Eclipse, and try to use it. What's wrong?)

3 Timer Animation

Open Problem3.java. Problem3 implements animate() using a timer. Run it and see how it works. Why does Problem3 use repaint() instead of paintImmediately()? Does Problem3 suffer from the same drawback as Problem1 and Problem2?

But it suffers from a new problem. The animation is supposed to take 2 seconds. How long does it actually take? (Instrument the code to find out.) Why?

4 Timer Animation Redux

Open Problem4.java. Problem4 is supposed to implement animate() using a timer as well as the system clock, but it's incomplete. Finish the code by filling in the part marked YOUR CODE HERE, using what you've learned from Problem2 and Problem3.

Run your Problem4 and see how it works. Does it exhibit any of the deficiencies you found in Problems 1, 2, and 3? What's still wrong with it?