Graphic by Keith Ohlfs

CS111, Wellesley College, Fall 1999

Animation Contest

[CS111 Home Page] [Syllabus] [Lecture Notes] [Assignments] [Programs] [Documentation] [Software Installation] [FAQ] [CS Dept.] [CWIS]

Contest Entries


We've now entered the world of being real Java programmers because we know how to create our own objects. This contest is an opportunity for us to explore Java graphics and gain practice creating more objects. AnimationWorld is a framework which allows us to create animations and test them out. A complete work of animation includes a set of animated objects called sprites. The sprites are drawn in a specific order for each animation frame and then updated to a new state between each frame. An animation proceeds as a series of frames. The speed of the animation is the number of frames per second (fps). We can also set the number of frames in the complete animated work.

AnimationWorld

Try out the following two animations: EasyAnimation1 and EasyAnimation2. A screenshot of AnimationWorld is below:

EasyAnimation2

The basic control buttons are on the right button panel.

The following properties of the animation can be changed in AnimationWorld: The following properties of the Sprites can be changed in AnimationWorld:

Basic Animation Structure

Creating an animation involves extending the AnimationWorld class and defining one method called initializeAnimation. Here's the code for EasyAnimation1:
public class EasyAnimation1 extends AnimationWorld {
  public void initializeAnimation () {
    MovingText hello = new MovingText("Hello!", 20, 150, 1, 0, Color.red);
    addSprite("Hello!", hello);
  }
}
To add Sprites to the animation, we use the addSprite method.
public void addSprite (String name, Sprite s)
Adds the Sprite to the animation and allows it to be referenced by the name given.
In the animation above, there is one Sprite. The MovingText Sprite moves text starting from the specified starting position (x,y) by the increment given (dx,dy) between frames and renders text in the color given. Defaults are used for everything else in this animation. The default background is white. The default frame size fills the entire space even if we resize the window. The default frames per second is 12. The default number of frames in the animation is 60. Note that the defaults can all be changed via the AnimationWorld interface. The defaults are set slow and small to values which are likely the minimum that you would want to use.

Here is the code for EasyAnimation2:

public class EasyAnimation2 extends AnimationWorld {

  public void initializeAnimation () {
    // set animation properties first
    setFrameSize(500,100);
    setFps(24);
    setMaxFrameNumber(180);

    // create Sprites and set Sprite properties
    ColorBackground background1 = new ColorBackground(Color.red);
    background1.setLastFrameNumber(160);

    ColorBackground background2 = new ColorBackground(Color.black);
    background2.setFirstFrameNumber(161);

    MovingText hello = new MovingText("Hello!", 20, 40, 5, 0, Color.green);

    MovingText bye = new MovingText("When will I see you again?", 300, 80, -1, 0, Color.cyan);
    bye.setFirstFrameNumber(25);

    FlashingText smile = new FlashingText("Smile!", 400, 40, Color.blue, Color.white, 10);
    smile.setFont(new Font("TimesRoman",Font.ITALIC,36));
    smile.setActiveFrameNumbers(50,120);

    // add Sprites to animation in the order in which they should be drawn
    // Sprites added before other Sprites appear behind those Sprites
    addSprite("red background", background1);
    addSprite("orange", background2);
    addSprite("Hello!", hello);
    addSprite("see ya", bye);
    addSprite("=)", smile);
  }

}
In this example, we are now using all of the possible methods we can use to set animation properties. They are explained below:
public void setFrameSize(int width, int height)
This method limits the animation to the specified width and height regardless of the size of the window. Centers the animation frame in the space available. Should only be used if you want a smaller animation space than normal.

public void setFps(int newFps)
This method sets the default frames per second for the animation to the number given. The default frames per second shows initially in the AnimationWorld interface and can be accessed again via the Reset All button.

public void setMaxFrameNumber(int newMaxFrameNumber)
This method sets the default number of frames in the animation. The default number of frames shows initially in the AnimationWorld interface and can be accessed again via the Reset All button. When we run an animation, it stops after rendering the frame numbered with the maximum number of frames.

In EasyAnimation2 we have now introduced backgrounds and the concept that Sprites can be active in only parts of the animation. Backgrounds are Sprites, too. The ColorBackground class fills the animation frame with the color given. Note that you should add backgrounds first to your animation so they will appear behind all the other Sprites. Sprites are special objects which understand a number of methods which will be explained later. The three methods that restrict when they show up in an animation will be used most often in the initializeAnimation method. They are explained below:
public void setFirstFrameNumber (int newFirstFrameNumber)
The Sprite becomes active starting from the frame number given.

public void setLastFrameNumber (int newLastFrameNumber)
The Sprite stops being active after the frame number given.

public void setActiveFrameNumbers (int newFirstFrameNumber, int newLastFrameNumber)
The Sprite is active between the frame numbers given, inclusive.

An active Sprite is drawn on the screen and can print out debugging messages. Notice that since backgrounds are Sprites, we can change the background in our animation.


Steps for creating a new animation:
  1. Start a new file.
  2. Add the following import statements:
    import java.applet.*;
    import java.awt.*;
    
  3. Create a class which extends the AnimationWorld class.
    public class MyAnimation extends AnimationWorld {
      // add code here
    }
    
  4. Save it in a file which is named the same name as the class name with a .java extension. In the example above, the file name must be MyAnimation.java.
  5. Add the file to your project.
  6. Define an initializeAnimation method.
  7. Set any animation properties you want.
  8. Define sprites.
  9. Add sprites to animation.
  10. Build, bring it up to date.
  11. Create an .html file for your applet and name it after your class. The standard file for this example would be named MyAnimation.html and would include
    <HTML>
    <HEAD><TITLE>MyAnimation</TITLE></HEAD>
    <BODY>
    <APPLET CODE="MyAnimation.class" WIDTH=600 HEIGHT=400></APPLET>
    </BODY></HTML>
    
  12. Test out the animation in AppletViewer and have fun!

Sprites

Sprites are the objects which live and play in the animation. The bulk of the work is creating your own Sprites. However, creating Sprites is pretty easy. To make our own Sprite, we just need to do the following steps: Sometimes the Sprite will want to behave differently depending on its environment. A Sprite can get the following information about its environment: Note that if you start the Sprite after the first frame, the currentStateNumber is not the same as the currentFrameNumber. For example, if we have a Sprite which lives from frames 3-6 inclusive, the following table illustrates the difference:
Frame #State #
10
20
31
42
53
64
74
84
Three sample classes have been provided for you to study. More samples may be available later.

Timeline

Logistics/Rules

Entry Registration/Submission

Programming Guidelines

Code

Sample Code for this contest can be found in the folder AnimationContest available in the CS111 download directory. The samples above are in the folder SampleAnimations. A blank folder with the minimal number of files necessary and the template files are available in the ContestEntry folder.