Graphic by Keith Ohlfs |
|
The basic control buttons are on the right button panel.
getState
method for each
frame in the animation in which it is being displayed.
AnimationWorld
class
and defining one method called initializeAnimation
. Here's the code
for EasyAnimation1
:
To add Sprites to the animation, we use thepublic class EasyAnimation1 extends AnimationWorld { public void initializeAnimation () { MovingText hello = new MovingText("Hello!", 20, 150, 1, 0, Color.red); addSprite("Hello!", hello); } }
addSprite
method.
In the animation above, there is one Sprite. Thepublic void addSprite (String name, Sprite s)
Adds the Sprite to the animation and allows it to be referenced by the name given.
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
:
In this example, we are now using all of the possible methods we can use to set animation properties. They are explained below: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); } }
Inpublic 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 theAnimationWorld
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 theAnimationWorld
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.
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:
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.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.
import java.applet.*; import java.awt.*;
AnimationWorld
class.public class MyAnimation extends AnimationWorld { // add code here }
initializeAnimation
method.
<HTML> <HEAD><TITLE>MyAnimation</TITLE></HEAD> <BODY> <APPLET CODE="MyAnimation.class" WIDTH=600 HEIGHT=400></APPLET> </BODY></HTML>
import java.awt.*;
Sprite
class.public class MySprite extends Sprite { // add code here }
public String name ()
Returns the name of the class.
public String toString ()
Returns a String telling the properties/instance variables of this object.
public String getState ()
Returns a String telling the current state of this object.
protected void resetState ()
Resets the state of the Sprite to its initial state.
protected void drawState (Graphics g)
Draws the current state of the Sprite on the given Graphics context.
protected void updateState ()
Updates the state of the Sprite to the next state.
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 # |
---|---|
1 | 0 |
2 | 0 |
3 | 1 |
4 | 2 |
5 | 3 |
6 | 4 |
7 | 4 |
8 | 4 |