import java.applet.*; import java.awt.*; import java.util.*; public class GodSim extends Applet implements Runnable { // Set up an array of humanity public Person people[]; // Double buffering initialization private Dimension appletSize; private Image offscreenBuffer; public Graphics gOff, g; public static int width = 450; public static int height = 250; // Set up Heaven + Hell (Dynamically resizable) private MyLinkedList heaven = new MyLinkedList(); private MyLinkedList hell = new MyLinkedList(); private final int perRow = 30; // Set up space for the person icon private Image personImage; // Set up space for the thread that will run to move the car private Thread timeLoop; public void init() { // Set up the double buffering. g = getGraphics(); appletSize = getSize(); offscreenBuffer = createImage(appletSize.width, appletSize.height); gOff = offscreenBuffer.getGraphics(); // How many people? people = new Person[(int)(Math.random()*100)+20]; // Set up the people. peopleGen(); // Start up the thread timeLoop = new Thread(this); timeLoop.start(); } public void peopleGen() { // Set up the people for (int i=0; i < people.length; i++) people[i] = new Person(getImage(getDocumentBase(), "img_person.gif")); } public void paint(Graphics g) { g.drawImage(offscreenBuffer, 0, 0, null); } public void run() { // Keep thread looping while (true) { // Clear our display area gOff.setColor(Color.white); gOff.fillRect(0, 0, GodSim.width, GodSim.height+100); for (int i=0; i < people.length; i++) { // Russian roulette is played on each display call // If you die, it returns false and you're sent to the pearly gates if (!people[i].displayLive(gOff)) { // Run a quick check to see if you're in the book of life // and add you to the appropriate list for etenity if (people[i].saved()) heaven.add(people[i]); else hell.add(people[i]); // Reincaration, for the sake of array conservation people[i] = new Person(getImage(getDocumentBase(), "img_person.gif")); } } // We could traverse the lists, but dealing with array representations are easier. Object heavenList[] = heaven.toArray(); Object hellList[] = hell.toArray(); // If heaven or hell get too full, clear them and start over. if (heavenList.length > 300 || hellList.length > 300) { heaven.clear(); hell.clear(); peopleGen(); } // Print the labels gOff.setColor(Color.black); gOff.drawString("Heaven", 0, GodSim.height); gOff.drawString("Hell", (int)GodSim.width/2, GodSim.height); // Draw the blessed with rows that have at most perRow people for (int i=0; i < heavenList.length; i++) { Person temp = (Person)(heavenList[i]); temp.displayDead(gOff, 0, GodSim.height+20+((int)i/perRow*5), (i%perRow)*5); } // Draw the damned with rows that have at most perRow people for (int i=0; i < hellList.length; i++) { Person temp = (Person)(hellList[i]); temp.displayDead(gOff, (int)GodSim.width/2, GodSim.height+20+((int)i/perRow*5), (i%perRow)*5); } // Paint the world paint(g); // Sleep try {timeLoop.sleep(30);} catch (InterruptedException e) {} } } // We don't need the graphics resources anymore. public void stop() { g.dispose(); gOff.dispose(); } }