|
|
|
|
||
|
|
Introduction to Animation Programming Introduction |
|
||
|
|
In the first two exercises in this chapter we introduced the concept of classes and object oriented programming. With this understanding and familiarity we are now able to start doing some more interesting things with our programs, one of which would be to start with some basic animation programming. Animation often requires executing different programs in a fashion where the programs seem to be running simultaneously. To do this Java provides a special class called Thread. The Thread class has several methods including "start", "suspend", "resume", "sleep", and "stop". The program listing below places some red disks in a window and moves them around. |
|
||
|
|
/** * Animator02.java * * W. Dirk Sikkema * Version 1.00 */ import java.awt.*; import java.applet.*; public class Animator02 extends Applet implements Runnable { Thread typo; private int index; final int len = 200; public void
start() { if (typo == null) { typo = new Thread(this); typo.start(); }//if }//start() public void stop
() { typo = null; }//stop() public void run
() { Thread current = Thread.currentThread
(); while (typo == current) { try { //the smaller the parameter number
the //faster the objrct
moves Thread.sleep
(200); }//try catch (InterruptedException
e) { }//catch repaint
(); index++; if (index >= len) { index = 0; }//if }//while }//run() public void
paint (Graphics g) { //coordinates for position on circle int x = 100; int y = 100; //int radius = 50; g.setColor (Color.black); g.fillOval(70, 70,
25, 25); g.setColor (Color.red); g.fillOval( (int)(Math.cos(x + index) * 45)
+ 75 , (int)(Math.sin(y +
index) * 45) + 75, 12, 12); g.fillOval( (int)(Math.sin(x + index) * 55)
+ 75 , (int)(Math.cos(y +
index) * 55) + 75, 12, 12); g.fillOval( (int)(Math.cos(x + index) * 45)
+ 100 , (int)(Math.sin(y
+ index) * 45) + 100, 12, 12); g.fillOval( (int)(Math.sin(x + index) * 55)
+ 100 , (int)(Math.cos(y
+ index) * 55) + 100, 12, 12); }//paint() }//Animator02 |
|
||
|
|
Note that this program is implemented as an applet. So we need the appropriate HTML code which is listed below. |
|
||
|
|
<HTML> <HEAD> </HEAD> <BODY BGCOLOR="000000"> <CENTER> <APPLET code = "Animator02.class" width = "500" height = "300" > </APPLET> </CENTER> </BODY> </HTML> |
|
||
|
|
|
|
||
|
|
Programming Assignment Modify the above program so as to change the shape , number, color and type of items Experiment with the settings of the various variables. Turn in a program listing your source code. Source code should use proper formatting. For this assignment you will also need to show me your program running. |
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|