Java TOPIC
21 – LOOP APPLICATION: ANIMATION LESSON NOTE ANIMATION BASICS An
animation is simply the display of a graphical object that gets moved
locations (or warped shapes) every so often.
If this occurs regularly and fast enough, it fools the viewer's brain
into thinking that the graphical shape is an object that is moving. We
will look at the basic setup of doing animation using NOOPDraw. We will use a while loop where every pass
in the loop will represent a frame in the animation. DELAY For
an animation to be viewable, we need to slow things down a little so that we
don't have too much happening in a small amount of time. We can slow Java statement execution down
by using: Thread.sleep(milliseconds); Essentially
the above line of code stops all execution of statements for the amount of
milliseconds specified. One
note, to use this statement, we need to add a "throws exception" at
the end of the function prototype that contains it. For
example, if we want to use it in the main function, our prototype will have
to look like this: public static void main(String[] args) throws
InterruptedException
... } while(true) //loop forever { Update
the current state of the animation. Draw
the current state of the animation. Pause
for a small amount of time. } Of
course, if we want the animation to stop on its own, we could add a way to
stopping the loop. EXAMPLE 1 – APPEARING
DOTS public class Animation01 { public static void main(String[] args) throws InterruptedException { NOOPDraw.createWindow(500,500,"Our First Animation"); while(true) { int randomX = (int)(Math.random() * 450); int randomY = (int)(Math.random() * 450); NOOPDraw.drawPoint(randomX,
randomY); Thread.sleep(1000); } } } The
result (click image to view an SWF video): EXAMPLE 1B – ADDING COLOUR { public static void main(String[] args) throws
InterruptedException { NOOPDraw.createWindow(500,500,"Our First Animation"); while(true) { NOOPDraw.setRandomColor(); int randomX = (int)(Math.random() * 450); int randomY = (int)(Math.random() * 450); NOOPDraw.drawPoint(randomX,
randomY); Thread.sleep(500); } } } The
result (click image to view an SWF video): EXAMPLE 2 – JUMPING CIRCLE public class Animation02 { public static void main(String[] args) throws
InterruptedException { NOOPDraw.createWindow(500, 500, "Jumping Circle"); while(true) { NOOPDraw.clearScreen(); int randomX = (int)(Math.random() * 450); //0 to 449 int randomY = (int)(Math.random() * 450); //0 to 449 int size = (int)(Math.random() * 50) + 10; //10 to 59 NOOPDraw.fillEllipse(randomX,
randomY, size, size); Thread.sleep(1000); } } } The
result (click image to view an SWF video):
{ public static void
main(String[] args) throws InterruptedException { NOOPDraw.createWindow(500, 500, "Horizontal Moving Block"); int squareX = 0; int squareY =
200; //won't change while(true) { NOOPDraw.clearScreen(); squareX=squareX+2; NOOPDraw.fillRectangle(squareX,
squareY, 40, 40); Thread.sleep(50); } } }
EXAMPLE 3B – RETURNING
BLOCK This
example is very similar to the previous one.
However, when the block gets off the screen (when its x-coordinate
reaches 490), then it is teleported to an x-coordinate of 0. public class Animation03B { public static void main(String[] args) throws
InterruptedException { NOOPDraw.createWindow(500, 500, "Horizontal Moving Square"); int squareX = 0; int squareY =
200; //won't change while(true) { NOOPDraw.clearScreen(); squareX=squareX+2; if (squareX
> 490) { squareX = 0; } NOOPDraw.fillRectangle(squareX,
squareY, 40, 40); Thread.sleep(25); } } }
USING OBJECTS TO GROUP
DATA As
our graphical applications get more advanced, we will start having a lot of
data about each shape. It is
beneficial to use object oriented programming to group data together. Let's
consider all of these details that we want for an object that will move on
the screen:
We
create a class called MovingObject that looks like this: public class MovingObject { public int red; public int green; public int blue; public int size; public int x; public int y; public int xDirection; public int yDirection; public String name; } We
can now create an animation with the above object. EXAMPLE 4 – USING OOP In
this example, we use a MovingObject object to store all the data of a
graphical object moving on the screen. public class Animation04 { public static void main(String[] args) throws
InterruptedException { NOOPDraw.createWindow(500, 500, "Using OOP"); //Create object MovingObject mo = new MovingObject(); mo.red=255; mo.green=128; mo.blue=128; mo.size=50; mo.x=250; mo.y=250; mo.xDirection = 5; mo.yDirection = 4; mo.name="UFO"; while(true) { //Clear screen NOOPDraw.clearScreen(); //Update location. mo.x = mo.x + mo.xDirection; mo.y = mo.y + mo.yDirection; //Moving off window? if (mo.x > 490) { mo.x = -mo.size; } if (mo.y > 470) { mo.y = -mo.size; } //Draw object and text NOOPDraw.setColor(mo.red, mo.green, mo.blue); NOOPDraw.fillRectangle(mo.x, mo.y, mo.size, mo.size); NOOPDraw.drawString(mo.name, mo.x, mo.y-10); //Delay Thread.sleep(50); } } }
|
|
|