1. Project #11: My Living Animal
CS 110 Introduction to Computing
Due: Thursday, Dec 22, 2005, 5pm
Your mission is to build on your previous animal. Your animal will be an extension of class Animal2:
1. As before, copy the files ~dblank/java/World2.java and ~dblank/java/Animal2.java to your directory.
Here are the two programs:
// Lab #11
// D.S. Blank
// World2.java
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class World2 extends Applet implements ActionListener {
// Define class properties:
Animal2 animals[];
// Define constructor:
public void init() {
// Make the GUI:
Button moveButton = new Button("Move");
add(moveButton);
moveButton.addActionListener(this);
// Make space to hold X animals:
animals = new Animal2[1];
// Now, create them:
animals[0] = new Triangle2(0, 0, 0.5, // startx, starty, scale
0, 0, 300, 300); // bounding box
}
public void actionPerformed(ActionEvent e) {
System.out.println("Moved!");
for (int i = 0; i < animals.length; i++) {
animals[i].move();
}
repaint();
}
// Have paint method paint background, and draw animals:
public void paint( Graphics g ) {
setBackground( Color.blue );
for (int i = 0; i < animals.length; i++) {
animals[i].draw(g);
}
}
}
// An Advanced Animal
// D.S. Blank
// CS110 Fall 2005
// Animal2.java
import java.awt.*;
public class Animal2 {
int x, y;
int x1, y1, x2, y2;
int xAmount, yAmount;
double scale;
int [][][] body;
Color []color;
public void draw(Graphics g) {
for (int b = 0; b < body.length; b++) {
int [] bodyX = new int [body[b].length];
int [] bodyY = new int [body[b].length];
for (int i = 0; i < body[b].length; i++) {
bodyX[i] = x + (int)(body[b][i][0] * scale);
bodyY[i] = y + (int)(body[b][i][1] * scale);
}
g.setColor(color[b]);
g.fillPolygon(bodyX, bodyY, body[b].length);
}
}
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
public void setScale(double scale) {
this.scale = scale;
}
public void setBounds(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void move() {
// Doesn't move!
}
}
2. Modify the program Triangle2 to include your previous shape, and additional shapes:
// A Triangle Animal2
// D.S. Blank
// CS110 Fall 2005
// Triangle2.java
import java.awt.*;
public class Triangle2 extends Animal2 {
public Triangle2(int x, int y, double scale,
int x1, int y1, int x2, int y2) {
// Set the position, scale, bounds:
setPosition(x, y);
setScale(scale);
setBounds(x1, y1, x2, y2);
// Set the initial movement amounts:
xAmount = 10;
yAmount = 10;
// Define the body:
int [][] mybody1 = {{0,0}, {100, 0}, {50, 100}};
int [][] eyes = {{50,0}, {50, 50}, {0, 0}};
Color [] colors = {Color.black, Color.yellow};
// body parts:
body = new int[2][][];
// set each body part:
body[0] = mybody1;
body[1] = eyes;
// and set the color array:
color = colors;
// Note: the number of body parts and colors must match!
}
public void move() {
x += xAmount;
y += yAmount;
}
}
3. Change the move method so that the animal does not go out of bounds. (x1, y1) is the upper, most left point, and (x2, y2) is the lower, most right point. Currently the move method just blindly adds xAmount to x and yAmount to y without checking to see if it is in bounds.
Please turn in your animal code, and your testing program. Also, email me the location of your animal class file. That's it!
I'll put a link here to the entire screen saver, and have it also running in the hallway monitor by the lab.
