import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class JavaPsychiatrist extends Applet implements Runnable, MouseListener { // Double buffering initialization private Dimension appletSize; private Image offscreenBuffer; public Graphics gOff, g; public static int width = 450; public static int height = 250; public static int numProzac = (int)(Math.random() * 10)+4; public static int numPaxil = (int)(Math.random() * 10)+4; public static int numValium = (int)(Math.random() * 10)+4; public static int numRitalin = (int)(Math.random() * 10)+4; public static int[] num = {numProzac, numPaxil, numValium, numRitalin}; private boolean patientDied = false; private boolean running = true; private MyLinkedList pills = new MyLinkedList(); private MedicationBar[] bars = new MedicationBar[4]; // 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(); // Set up the images Pill.imageInit(this); this.addMouseListener(this); setupPills(); // Start up the thread timeLoop = new Thread(this); timeLoop.start(); } public void setupPills() { // Set up all the pills for (int i=0; i < numProzac; i++) pills.add(new Pill(Pill.PROZAC)); bars[0] = new MedicationBar("Prozac", 20, 300, (numProzac*8)/2, numProzac*8); for (int i=0; i < numPaxil; i++) pills.add(new Pill(Pill.PAXIL)); bars[1] = new MedicationBar("Paxil", 60, 300, (numPaxil*8)/2, numPaxil*8); for (int i=0; i < numValium; i++) pills.add(new Pill(Pill.VALIUM)); bars[2] = new MedicationBar("Valium", 100, 300, (numValium*8)/2, numValium*8); for (int i=0; i < numRitalin; i++) pills.add(new Pill(Pill.RITALIN)); bars[3] = new MedicationBar("Ritalin", 140, 300, (numRitalin*8)/2, numRitalin*8); } public void paint(Graphics g) { g.drawImage(offscreenBuffer, 0, 0, null); } public void run() { String deathString = ""; // Keep thread looping while (running) { // Clear our display area gOff.setColor(Color.white); gOff.fillRect(0, 0, 450, 350); if (patientDied) { gOff.setColor(Color.black); gOff.drawString(deathString + " Click to restart", (appletSize.width/2)-80, (appletSize.width/2)-15); } else { for (int i=0; i < bars.length; i++) { deathString = bars[i].display(gOff); if (!deathString.equals("")) { patientDied = true; i = bars.length; } } Object pillList[] = pills.toArray(); if (pillList.length==0) { // I love the irony in these two deathString = "Congratulations! You lived!"; patientDied = true; } else { for (int i = 0; i < pillList.length; i++) { Pill tempPill = (Pill)(pillList[i]); tempPill.display(gOff); } } } // 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(); } public void mouseClicked(MouseEvent e) { if (patientDied) { pills.clear(); setupPills(); patientDied=false; } else { Object pillList[] = pills.toArray(); double closestDistance = 999; Pill closest = null; Pill tempPill = null; for (int i = 0; i < pillList.length; i++) { tempPill = (Pill)(pillList[i]); double tempDist = tempPill.getDistance(e.getX(), e.getY()); if (tempDist < closestDistance) { closestDistance = tempDist; closest = tempPill; } } if (closestDistance < 40) { pills.remove(closest); bars[closest.getType()-1].addMedication(num[closest.getType()-1]); } } } // Methods for interface compatibility public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} }