/*-------------------------------------------------------------------* * NTU Student: Java With Engineering Applications, Spring 1999 * Final Project - Java How To Program 2nd edition, Deitel & Deitel * Problem - Appendix F page 1008 - CLASS Simulator: application created to * simulate an elevator within a two story building. * * Michael Kusmierz * Xerox Corporation * Rev 1.0 July 14, 1999 * * This code represents a students work in progress * and is not to be used beyond that purpose. *-------------------------------------------------------------------*/ /******************************************************************************* * class Simulator */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Simulator extends Applet implements ActionListener { private Floor floor1; private Floor floor2; private Elevator elevator; private Person person; //--- Simulator / Building attributes private Floor arrivalFloor,departureFloor; //Persons arrive and depart // passed to Person() constructor private int intPeople; //Total Number of people //--- Sound public AudioClip elevatorButtonSound; public TextArea ivjTextArea1 = null; public final int MAX_PERSONS_ON_FLOOR = 1; public boolean bolMusicAndStepsOn = true; public boolean bolSpecialEffects = true; public Button ivjBtnNewPerson1 = null; public Button ivjBtnNewPerson2 = null; //--- Images for control board public Image bellImage; //--- Images for floor public Image imgStart, imgStop; public Button ivjBtnStopMusicSteps = null; public Button ivjBtnEffects = null; public Label ivjLabel1 = null; /***************************************************************************************** * actionPerformed * @param e java.awt.event.ActionEvent */ public void actionPerformed(ActionEvent e) { if (e.getSource() == ivjBtnStopMusicSteps) { //elevator.stop(); //person.stopMessages(); if (bolMusicAndStepsOn == true) { bolMusicAndStepsOn = false; ivjBtnStopMusicSteps.setLabel("Music is Off"); ivjTextArea1.append("Simulator: Elevator music and person step sounds off." + ".\n"); } else { ivjBtnStopMusicSteps.setLabel("Music is On"); bolMusicAndStepsOn = true; ivjTextArea1.append("Simulator: Elevator music and person step sounds resumed. " + ".\n"); } } if (e.getSource() == ivjBtnEffects) { if (bolSpecialEffects == true) { bolSpecialEffects = false; ivjBtnEffects.setLabel("Effects are Off"); ivjTextArea1.append("Simulator: Crash effects off." + ".\n"); } else { ivjBtnEffects.setLabel("Effects are On"); bolSpecialEffects = true; ivjTextArea1.append("Simulator: Crash effects on." + ".\n"); } } if (e.getSource() == ivjBtnNewPerson1) { arrivalFloor = floor1; departureFloor = floor2; if (arrivalFloor.getPersonsOnFloor() < MAX_PERSONS_ON_FLOOR) { newSimulationPerson(); elevatorButtonSound.play(); } else ivjTextArea1.append("Simulator:Action denied: Too many people on the floor" + ".\n"); } if (e.getSource() == ivjBtnNewPerson2) { arrivalFloor = floor2; departureFloor = floor1; if (arrivalFloor.getPersonsOnFloor() < MAX_PERSONS_ON_FLOOR) { newSimulationPerson(); elevatorButtonSound.play(); } else ivjTextArea1.append("Simulator:Action denied: Too many people on the floor" + ".\n"); }} /** * Gets the applet information. * @return java.lang.String */ public String getAppletInfo() { return "Simulator created using VisualAge for Java."; } /** * Return the BtnEffects property value. * @return java.awt.Button */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Button getBtnEffects() { if (ivjBtnEffects == null) { try { ivjBtnEffects = new java.awt.Button(); ivjBtnEffects.setName("BtnEffects"); ivjBtnEffects.setBounds(355, 322, 109, 23); ivjBtnEffects.setLabel("Special Effects"); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjBtnEffects; } /** * Return the BtnNewPerson1 property value. * @return java.awt.Button */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Button getBtnNewPerson1() { if (ivjBtnNewPerson1 == null) { try { ivjBtnNewPerson1 = new java.awt.Button(); ivjBtnNewPerson1.setName("BtnNewPerson1"); ivjBtnNewPerson1.setBounds(87, 322, 124, 23); ivjBtnNewPerson1.setLabel("New person - Floor 1"); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjBtnNewPerson1; } /** * Return the BtnNewPerson2 property value. * @return java.awt.Button */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Button getBtnNewPerson2() { if (ivjBtnNewPerson2 == null) { try { ivjBtnNewPerson2 = new java.awt.Button(); ivjBtnNewPerson2.setName("BtnNewPerson2"); ivjBtnNewPerson2.setBounds(221, 322, 129, 23); ivjBtnNewPerson2.setLabel("New person - Floor 2"); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjBtnNewPerson2; } /** * Return the BtnStopThreads property value. * @return java.awt.Button */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Button getBtnStopMusicSteps() { if (ivjBtnStopMusicSteps == null) { try { ivjBtnStopMusicSteps = new java.awt.Button(); ivjBtnStopMusicSteps.setName("BtnStopMusicSteps"); ivjBtnStopMusicSteps.setBounds(468, 322, 125, 23); ivjBtnStopMusicSteps.setActionCommand("Stop Music&Steps"); ivjBtnStopMusicSteps.setLabel("Stop Music&Steps"); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjBtnStopMusicSteps; } /** * This method was created in VisualAge. * @return Elevator */ public Elevator getElevatorRef() { return elevator; } /** * Return the Label1 property value. * @return java.awt.Label */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private Label getLabel1() { if (ivjLabel1 == null) { try { ivjLabel1 = new java.awt.Label(); ivjLabel1.setName("Label1"); ivjLabel1.setAlignment(java.awt.Label.CENTER); ivjLabel1.setFont(new java.awt.Font("dialog", 3, 18)); ivjLabel1.setText(" Elevator Simulation for a Two Story Building by Michael Kusmierz"); ivjLabel1.setBounds(37, 6, 788, 23); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjLabel1; } /** * Return the TextArea1 property value. * @return java.awt.TextArea */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private TextArea getTextArea1() { if (ivjTextArea1 == null) { try { ivjTextArea1 = new java.awt.TextArea(); ivjTextArea1.setName("TextArea1"); ivjTextArea1.setBounds(91, 350, 504, 138); // user code begin {1} // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } }; return ivjTextArea1; } /** * Called whenever the part throws an exception. * @param exception java.lang.Throwable */ private void handleException(Throwable exception) { /* Uncomment the following lines to print uncaught exceptions to stdout */ // System.out.println("--------- UNCAUGHT EXCEPTION ---------"); // exception.printStackTrace(System.out); } /** * Handle the Applet init method. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ public void init() { super.init(); try { setName("Simulator"); setFont(new java.awt.Font("dialog", 0, 12)); setLayout(null); setSize(940, 505); add(getTextArea1(), getTextArea1().getName()); add(getBtnNewPerson1(), getBtnNewPerson1().getName()); add(getBtnNewPerson2(), getBtnNewPerson2().getName()); add(getBtnStopMusicSteps(), getBtnStopMusicSteps().getName()); add(getBtnEffects(), getBtnEffects().getName()); add(getLabel1(), getLabel1().getName()); // user code begin {1} ivjBtnEffects.setEnabled(false); ivjBtnNewPerson1.setEnabled(false); ivjBtnNewPerson2.setEnabled(false); ivjBtnStopMusicSteps.setEnabled(false); bolMusicAndStepsOn = true; bolSpecialEffects = false; floor2 = new Floor(2,this); add(floor2); floor1 = new Floor(1,this); add(floor1); elevator = new Elevator(this, floor1, floor2); add(elevator); elevator.start(); ivjBtnNewPerson1.addActionListener(this); ivjBtnNewPerson2.addActionListener(this); ivjBtnStopMusicSteps.addActionListener(this); ivjBtnEffects.addActionListener(this); elevatorButtonSound = getAudioClip(this.getDocumentBase(), "images\\0.au"); bellImage = getImage( this.getDocumentBase(), "images/bell.jpg"); imgStart = getImage(this.getDocumentBase(), "images/start.gif"); imgStop = getImage(this.getDocumentBase(), "images/stop.gif"); ivjBtnEffects.setEnabled(true); ivjBtnNewPerson1.setEnabled(true); ivjBtnNewPerson2.setEnabled(true); ivjBtnStopMusicSteps.setEnabled(true); // user code end } catch (java.lang.Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } /** * main entrypoint - starts the part when it is run as an application * @param args java.lang.String[] */ public static void main(java.lang.String[] args) { try { Frame frame; try { Class aFrameClass = Class.forName("com.ibm.uvm.abt.edit.TestFrame"); frame = (Frame)aFrameClass.newInstance(); } catch (java.lang.Throwable ivjExc) { frame = new Frame(); } Simulator aSimulator; Class iiCls = Class.forName("Simulator"); ClassLoader iiClsLoader = iiCls.getClassLoader(); aSimulator = (Simulator)java.beans.Beans.instantiate(iiClsLoader,"Simulator"); frame.add("Center", aSimulator); frame.setSize(aSimulator.getSize()); frame.setVisible(true); } catch (Throwable exception) { System.err.println("Exception occurred in main() of java.applet.Applet"); exception.printStackTrace(System.out); } } /*************************************************************************** * newSimulationPerson * Create a new person in the Simulation Building */ private void newSimulationPerson() { String personName[] = {"OPUS","FELIX","BUGS","DAFFY","5","6","7","8","9"}; //--- watcher for the number of different people intPeople++; if (intPeople > 4) intPeople = 1; //recycle the images //--- get the cartoonX.gif for the persons image. Image personImage = getImage(getDocumentBase(), "images/cartoon" + intPeople + ".gif"); person = new Person(arrivalFloor, departureFloor, personImage, personName[intPeople - 1], this, elevator); //--- start the person thread walking person.start(); } /****************************************************************************** * Eliminate an obsolete person from the Simulation Building */ private void obsoletePerson() { person = null; } /** * This method was created in VisualAge. * @param g java.awt.Graphics */ public void paint(Graphics g) { } }