// animation-simulation applet: hungry being and food // See program assignment for labs meeting Nov. 10, 15, 16, 20, 21. // Prepared by A. Biermann, November 2000. import java.awt.*; import awb.*; import java.awt.event.*; public class CA6 extends java.applet.Applet implements ActionListener { Graphics g; Canvas c; Button b1,b2; HuOne h1; // This declares one hungry being Foo f1 []; // This declares an array of food objects int FooMax, lifetime; int x, y, boxsize; IntField xF, yF, lifetimeF; public void init() // Initialize program { f1 = new Foo[15]; // 15 food objects boxsize = 200; c = new Canvas(); c.setSize(200,200); add(c); g = c.getGraphics(); b1 = new Button("Initialize"); b1.addActionListener(this); b2 = new Button("Go"); b2.addActionListener(this); xF = new IntField(10); xF.setLabel("x-coord"); yF = new IntField(10); yF.setLabel("y-coord"); lifetimeF = new IntField(10); lifetimeF.setLabel("lifetime"); x = xF.getInt(); y = yF.getInt(); add(b1); add(b2); add(xF); add(yF); add(lifetimeF); } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == b1) // Initialize screen { g.setColor(Color.white); g.fillRect(0,0,200,200); x = xF.getInt(); y = yF.getInt(); lifetime = 0; lifetimeF.setInt(lifetime); h1 = new HuOne(x, y, g); // Create and draw hungry being h1.draw(); f1[0] = new Foo(40,30,g); // Create and scatter 15 food objects f1[0].draw(); f1[1] = new Foo(30,70,g); f1[1].draw(); f1[2] = new Foo(50,120,g); f1[2].draw(); f1[3] = new Foo(70,170,g); f1[3].draw(); f1[4] = new Foo(90,120,g); f1[4].draw(); f1[5] = new Foo(110,70,g); f1[5].draw(); f1[6] = new Foo(130,20,g); f1[6].draw(); f1[7] = new Foo(15,70,g); f1[7].draw(); f1[8] = new Foo(170,120,g); f1[8].draw(); f1[9] = new Foo(190,170,g); f1[9].draw(); f1[10] = new Foo(20,150,g); f1[10].draw(); f1[11] = new Foo(40,90,g); f1[11].draw(); f1[12] = new Foo(60,180,g); f1[12].draw(); f1[13] = new Foo(80,60,g); f1[13].draw(); f1[14] = new Foo(120,140,g); f1[14].draw(); FooMax = 14; // Maximum number of food objects } if (cause == b2) // Advance the simulation one time interval { lifetime = lifetime + 1; lifetimeF.setInt(lifetime); h1.advance(); } } public class HuOne // Hungry being class // It goes looking for food // It eats what it finds { protected int xc,yc,diameter; protected Graphics hg; public HuOne(int x, int y, Graphics h) { diameter = 20; // The being will be a filled circle xc = x; yc = y; hg = h; } public void draw() { hg.setColor(Color.blue); hg.fillOval(xc,yc,diameter,diameter); } public void erase() { hg.setColor(Color.white); hg.fillOval(xc,yc,diameter,diameter); } public void advance() // Advance the being through // one time interval { int d, distance, mindist, closest, j; int xtarget, ytarget; erase(); diameter = diameter - 1; // Being gets thinner if (diameter > 0) // If being still exists { xc = xc + 10; // Move 10 pixels right yc = yc + 10; // Move 10 pixels down // This is a dumb strategy // You can do better than this if (xc > (boxsize-diameter)) // Bounce off right side { d = xc - (boxsize-diameter); xc = xc - 2*d; } if (xc < 0) // Bounce off left side { xc = -xc; } if (yc > (boxsize-diameter)) // Bounce off bottom { d = yc - (boxsize-diameter); yc = yc - 2*d; } if (yc < 0) // Bounce off top { yc = -yc; } // Is there food near at hand? // Find closest food j = 1; mindist = f1[0].FooDist(xc,yc); closest = 0; while(j <= FooMax) { distance = f1[j].FooDist(xc,yc); if (distance < mindist) { mindist = distance; closest = j; } j = j + 1; } xtarget = f1[closest].FooX(); // Closest food x-coord ytarget = f1[closest].FooY(); // Closest food y-coord // If food is near, eat it. if (((xc-xtarget)*(xc-xtarget) + (yc-ytarget)*(yc-ytarget)) < 200) { f1[closest].eat(); diameter = diameter + 2; // Being gets fatter } draw(); } } } public class Foo // This is the food class // We scatter these around // The being searches for them // and eats them { protected int fx, fy, foolife; // foolife is 1 until it is eaten // then foolife is 0 protected Graphics fg; public Foo(int x, int y, Graphics h) { fx = x; fy = y; foolife = 1; fg = h; } public int FooDist(int xx, int yy) // Finds the distance to food // (squared) { if (foolife==1) { return ( (xx-fx)*(xx-fx) + (yy-fy)*(yy-fy) ); } else { return 1000000; // If foolife is zero, // distance is very far } } public int FooX() // Finds the x-coord of the food { return fx; } public int FooY() // Finds the y-coord of the food { return fy; } public void draw() { fg.setColor(Color.green); fg.fillRect(fx,fy,10,10); } public void eat() { foolife = 0; fg.setColor(Color.white); fg.fillRect(fx,fy,10,10); } } }