package awb; import java.awt.*; import java.util.*; public class Being extends GraphicsObject implements Animated { private Color[] colors = {Color.red, Color.green, Color.blue, Color.yellow, Color.black}; private Color[] eyeColors = {Color.green, Color.blue, Color.black}; private String[] moods = {"Happy", "Sad", "Angry", "Surprised"}; private String[] Deaths = { " died in a tragic zamboni accident.", " drowned in foam after a basketball game.", " went postal and was killed in a police standoff.", " was electrocuted trying to turn on a computer.", " thought the taxi driver would stop in time, but...no.", " died happy.", " had one eclair too many and just burst.", " starved to death waiting for Papa John's.", " bought the farm.", " pissed off Don Corleone.", " is chillin' with Jimmy Hoffa.", " sleeps with the fishes.", "... R.I.P."}; private Random number; protected String myName = "Unknown"; protected String myMood = "Happy"; protected int myAge = 0; protected Color myEyeColor = Color.blue; private int myDeathAge = 100; private String myDeathMessage = ""; private boolean mySelected = false; public Being(String n) { myX = Utilities.getRandomInt(0,BeingCanvas.getWidth()); myY = Utilities.getRandomInt(0,BeingCanvas.getHeight()); myWidth = 10; myHeight = 10; myName = n; myColor = colors[Utilities.getRandomInt(0,5)]; myEyeColor = eyeColors[Utilities.getRandomInt(0,3)]; myMood = moods[Utilities.getRandomInt(0,4)]; number = new Random(); myDeathAge = 30 + (number.nextInt() % 10); } public Being(Being parent) { myWidth = 10; myHeight = 10; myName = parent.getName() + " Jr."; myColor = colors[Utilities.getRandomInt(0,5)]; myEyeColor = eyeColors[Utilities.getRandomInt(0,3)]; myMood = moods[Utilities.getRandomInt(0,4)]; number = new Random(); myDeathAge = 30 + (number.nextInt() % 10); myX = parent.getX() + (number.nextInt() % 20); myY = parent.getY() + (number.nextInt() % 20); } public int getX() { return myX; } public int getY() { return myY; } public void draw(Graphics g) { // draw head shape g.setColor(myColor); g.drawOval(myX,myY,myWidth,myHeight); // set up some font stuff int fontSize = ((7 + (myWidth/10))/2)*2; g.setFont(new Font("Helvetica", Font.PLAIN, fontSize)); FontMetrics fm = g.getFontMetrics(); drawName(g,fm); drawEyes(g); drawMouth(g); // outline if selected if (mySelected) { g.setColor(Color.black); int sw = fm.stringWidth(myName); int margin = 2; if (sw > myWidth) { margin = ((sw-myWidth)/2) + 2; } g.drawRect(myX-margin,myY-1,myWidth+(2*margin), myHeight+fm.getHeight()+3); } } public void update() { myAge++; if (myAge < 6) { myWidth += 3; myHeight += 3; } if (myAge > 30) { myWidth--; myHeight--; } myX = myX + number.nextInt() % 15; myY = myY + number.nextInt() % 15; int w = BeingCanvas.getWidth(); int h = BeingCanvas.getHeight(); if (myX < 0) myX = w + myX; if (myX > w) myX = myX - w; if (myY < 0) myY = h + myY; if (myY > h) myY = myY - h; myMood = moods[Utilities.getRandomInt(0,4)]; } public int getAge() { return myAge; } public boolean isDead() { if (myAge > myDeathAge) { myDeathMessage = " died of old age.\n"; return true; } int chance = Utilities.getRandomInt(0,400); try { myDeathMessage = Deaths[chance] + "\n"; return true; } catch (ArrayIndexOutOfBoundsException ex) { myDeathMessage = ""; return false; } } public boolean canReproduce() { return (((number.nextInt() % 50) > 35) && (myAge > 6) && (myAge < 20)); } public String getName() { return myName; } public void setName(String n) { myName = n; } public String getMood() { return myMood; } public String getCauseOfDeath() { return myDeathMessage; } public boolean contains(int x, int y) { if ((x < myX) || (x > myX + myWidth)) return false; if ((y < myY) || (y > myY + myHeight)) return false; return true; } public void select() { mySelected = true; } public void deselect() { mySelected = false; } private void drawName(Graphics g, FontMetrics fm) { g.setColor(Color.black); int fh = fm.getHeight(); int fw = fm.stringWidth(myName); g.drawString(myName,myX+((myWidth-fw)/2),myY+myHeight+2+fh); } private void drawEyes(Graphics g) { g.setColor(myEyeColor); g.fillOval(myX+(myWidth/3)-(myWidth/10),myY+(myWidth/4), myWidth/5,myHeight/10); g.fillOval(myX+((2*myWidth)/3)-(myWidth/10),myY+(myWidth/4), myWidth/5,myHeight/10); } private void drawMouth(Graphics g) { g.setColor(myColor); if (myMood.equals("Surprised")) { g.drawOval(myX+(myWidth/4),myY+(myHeight/2), myWidth/2,myHeight/4); } else if (myMood.equals("Sad")) { g.drawArc(myX+(myWidth/4),myY+((3*myHeight)/4),myWidth/2, myHeight/2,45,90); } else if (myMood.equals("Angry")) { g.drawLine(myX+(myWidth/4),myY+((3*myHeight)/4), myX+((3*myWidth)/4),myY+((3*myHeight)/4)); } else // assume Happy { g.drawArc(myX+(myWidth/4),myY+(myHeight/4),myWidth/2, myHeight/2,225,90); } } }