Assignment 6: An animation of alien activity
In this assignment, you create an applet that behaves the same as the one
below.
It comprises the following principal objects:
-
A button for creating new aliens.
-
A "board" that is the aliens’ universe
-
An abstract class called Alien that looks like the following:
import java.awt.*;
public abstract class Alien {
int x, y,
// location of alien
size; // size of alien
Color color;
// color of alien
final static int boardSize
= 200,
xBoard = 50,
yBoard = 50;
// Constructor: initialize x and y to a
random position inside the board, so that the even // the biggest alien
gets drawn completely inside the board.
public Alien()
{ }
// compute coordinates
of next position of alien
abstract void move();
// draw alien at his
location within the universe
abstract void draw(Graphics
g);
}
-
The applet skeleton that accomplishes the animation follows:
// Basic Assignment 6
import java.awt.*;
import java.applet.*;
public class animations extends Applet
{
int sleepTime = 80,
// time step between repaints
// insert the other
applet variables here
// initialize applet
public void init()
{}
// paint accomplishes the animation
public void paint(Graphics
g) {
//
draw the bounding box of the universe and all the aliens
try {
Thread.sleep(sleepTime);
}
catch (InterruptedException e) {
showStatus(e.toString());
}
repaint();
}
public boolean action(Event
event, Object o) {
// process the button being clicked, then ....
repaint();
return true;
}
}
-
Your abstract class Alien will have at least the 3 subclasses described
below:
-
Martian:
-
They are red squares, 10 pixels on a side.
-
When created, they are placed randomly on the board.
-
At each time step, they move randomly 5 pixels either left or right
and randomly 5 pixels either up or down.
-
They stay fully within the universe at all times. If a move would
put them out of the universe, they wrap around (like a modulus operation).
-
Venutians:
-
They are green ovals, 20 pixels wide and 40 pixels high.
-
When created, they are placed randomly on the board, and are given a movement
vector (x, y), where x is a random number of pixels between 0 and 9, inclusive;
y is another number in the same interval.
-
At each time step, they move according to this vector, except that when
the reach a boundary, the reflect, like a billiard ball.
-
They stay fully within the universe at all times.
-
Earthlings:
-
They are blue circles, 6 pixels in diameter that move in a circle of radius
30 pixels.
-
When they are created, the center of their circle is randomly selected
within the universe.
-
At each time step, they move around the circle 0.2 radians.
-
If any portion of their orbit falls outside the universe, then when they
are on that portion of their orbit, they are not visible.
-
When the create button is clicked, you create a randomly selected alien.
Your applet should be able to hold a maximum of 20 such aliens.
-
You must use polymorphism in your paint method to update the positions
of the aliens and to draw them. This is illustrated in the solar
system applet presented in class. A link to its source code appears
on the course’s Resources page.
-
You optionally can create other kinds of Aliens. For example, you
might create a subclass of Alien, called Jovians, who are pulled by gravity
to the bottom of the universe, and bounce half as high. Their horizontal
direction is a random number of pixels between 0 and 15. You can
fill in other details.
-
Here is the html file you should use:
<HTML>
<HEAD>
<TITLE> A simple program </TITLE>
</HEAD>
<BODY>
<APPLET CODE="animations.class" WIDTH=400
HEIGHT=400></APPLET>
</BODY>
</HTML>
Turn In Procedure
Use email to turn in your code to cs10@cs.ucsb.edu.
Each class is a separate attachment within the same message. Submit
a hard copy to the CS10 homework box.