import java.util.Vector; import java.awt.*; public class producer extends Thread { static final int MAXQUEUE = 5; private Vector messages=new Vector(); private int nextloc=1; prodcon parent; Graphics g; Frame info; TextArea infoarea; public producer(prodcon initparent) { parent=initparent; g=parent.getGraphics(); info=new Frame("Watch Production"); info.resize(150, 50); // Create and use a BorderLayout manager with margins info.setLayout(new BorderLayout(15, 15)); infoarea = new TextArea(); // No typing in output field... infoarea.setEditable(false); info.add("Center", infoarea); info.pack(); info.show(); } public void run() { try { while (true) { putMessage(); sleep(650); } } catch(InterruptedException e){} } private synchronized void putMessage() throws InterruptedException { while (messages.size() == MAXQUEUE) wait(); int loc=messages.size(); messages.addElement("Vader "+loc); System.out.println("added "+loc); infoarea.appendText("Produced vader "+(loc+1)+"\n"); putimage(loc,parent.fighter); // put this if here to make simulation run nicer if ((loc+1)==5) {sleep(1500);} notify(); } // called by consumer public synchronized String getMessage() throws InterruptedException { while (messages.size()==0) wait(); int loc=messages.size(); String message=(String)messages.firstElement(); infoarea.appendText("Destroyed vader "+loc+"\n"); messages.removeElement(message); putimage(loc-1,parent.poof); notify(); return message; } public synchronized void putimage(int where,Image whichimage) { if (where==0) { g.drawImage(whichimage,0,0,parent); } else { if (where==1) { g.drawImage(whichimage,150,0,parent); } else { if (where==2) { g.drawImage(whichimage,0,120,parent); } else { if (where==3) { g.drawImage(whichimage,150,120,parent); } else { g.drawImage(whichimage,60,240,parent); } } } } } }