// Copyright 1999 Regents of The University of Michigan import umich.coabs.simulator.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; /** * This type was created in VisualAge. */ class SimulatorStartFrame extends JFrame implements DocumentListener, ActionListener, Runnable { int numLocations = 11, numTransports = 9, numEvacuees = 20; int numSafetyLocations = 4; JTextField numLocField; JTextField numSafeLocField; JTextField numTransField; JTextField numEvacField; JButton startButton; static Control ctrl; static boolean runningAsApplet; /** * SimulatorStartFrame constructor comment. */ public SimulatorStartFrame() { super(); setTitle("Randomly Moving Transports"); } /** * SimulatorStartFrame constructor comment. * @param title java.lang.String */ public SimulatorStartFrame(String title, Control c) { super(title); ctrl = c; initialize(); } /** * SimulatorStartFrame constructor comment. */ public SimulatorStartFrame(Control c) { super(); setTitle("Randomly Moving Transports"); ctrl = c; initialize(); Thread t = new Thread(ctrl); t.start(); } /** * This method was created in VisualAge. */ public void actionPerformed(ActionEvent evt) { if (evt.getSource() == startButton) { startButton.setText("Restart"); //ctrl.pause(); ctrl.reset(); updateForInput(); double routeTripTime = 10.0; double routeDestroyTime = 3.0; int dim = (int) java.lang.Math.sqrt(numLocations) + 1; Location[][] locations = new Location[dim][dim]; LinkedList routes = new LinkedList(); LinkedList transports = new LinkedList(); LinkedList evacuees = new LinkedList(); LinkedList safeLocations = new LinkedList(); Location l1, l2; Route r1; Transport t1; Evacuee e1; DecisionMakerForEvacuees dmfe; TransportDecisionMaker tdm; int i, j, k, n, r; boolean madeSafe; Color[] colorArray = new Color[] {Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.magenta, Color.cyan, Color.blue}; String[] colorStringArray = new String[] {"red", "pink", "orange", "yellow", "green", "magenta", "cyan", "blue"}; String id; Color c; int x = 50, y = 50, dx = 80, dy = 80, tx, ty; //Control ctrl = new Control(2*x+dx*(dim-1), 2*y+dy*(dim-1)); //Control ctrl = new Control(); // Create grid of locations k = 0; for (i = 0; i < dim; i++) { ty = y + dy * i; for (j = 0; j < dim; j++) { id = "l" + i + "-" + j; tx = x + dx * j; if (k < numLocations) locations[i][j] = ctrl.CreateLocation(id, null, null, 1000, 1000, tx, ty, Color.white); k++; } } // Make safety locations for (n = 0; n < numSafetyLocations; n++) { madeSafe = false; while (!madeSafe) { k = (int) (numLocations * java.lang.Math.random()); i = k / dim; j = k % dim; l1 = locations[i][j]; if (safeLocations.Find(l1) == null) { madeSafe = true; l1.setColor(Color.green); safeLocations.Add(l1); } } } // Create routes k = 0; for (i = 0; i < dim; i++) { for (j = 0; j < dim; j++) { if (k < numLocations) { l1 = locations[i][j]; if (j < dim - 1) { id = "r" + i + "-" + j + "h"; l2 = locations[i][j + 1]; if ((l1 != null) && (l2 != null)) routes.Add(ctrl.CreateRoute(id, null, null, l1, l2, routeTripTime, routeDestroyTime, Route.TWOWAY, true, 0.0, Color.black)); } if (i < dim - 1) { l2 = locations[i + 1][j]; id = "r" + i + "-" + j + "v"; if ((l1 != null) && (l2 != null)) routes.Add(ctrl.CreateRoute(id, null, null, l1, l2, routeTripTime, routeDestroyTime, Route.TWOWAY, true, 0.0, Color.black)); } if (java.lang.Math.random() < 0.20) if ((j < dim - 1) && (i < dim - 1)) { l2 = locations[i + 1][j + 1]; id = "r" + i + "-" + j + "dr"; if ((l1 != null) && (l2 != null)) routes.Add(ctrl.CreateRoute(id, null, null, l1, l2, routeTripTime, routeDestroyTime, Route.TWOWAY, true, 0.0, Color.black)); } if (java.lang.Math.random() > 0.80) if ((j > 0) && (i < dim - 1)) { l2 = locations[i + 1][j - 1]; id = "r" + i + "-" + j + "dl"; if ((l1 != null) && (l2 != null)) routes.Add(ctrl.CreateRoute(id, null, null, l1, l2, routeTripTime, routeDestroyTime, Route.TWOWAY, true, 0.0, Color.black)); } } k++; } } // Create transports for (n = 0; n < numTransports; n++) { k = (int) (numLocations * java.lang.Math.random()); i = k / dim; j = k % dim; l1 = locations[i][j]; id = "dmt" + n; tdm = new TransportDecisionMaker(id, null, ctrl); id = "t" + n; r = (int) (colorArray.length * java.lang.Math.random()); c = colorArray[r]; t1 = ctrl.CreateTransport(id, tdm, null, l1, null, 100, 100, true, 0.0, l1.getX(), l1.getY(), c); transports.Add(t1); tdm.Possess(t1); ctrl.connect(tdm); } // Create evacuees for (n = 0; n < numEvacuees; n++) { k = (int) (numLocations * java.lang.Math.random()); i = k / dim; j = k % dim; l1 = locations[i][j]; //k = (int) (numLocations * java.lang.Math.random()); //i = k / dim; //j = k % dim; //l2 = locations[i][j]; id = "e" + n; r = (int) (colorArray.length * java.lang.Math.random()); c = colorArray[r]; evacuees.Add(ctrl.CreateEvacuee(id, null, null, 1, safeLocations, null, l1, null, false, 0.0, c)); } // Create decision maker for evacuees dmfe = new DecisionMakerForEvacuees("m", evacuees, ctrl); ctrl.connect(dmfe); //ctrl.StartSimulator(); ctrl.restart(); //ctrl.unpause(); } } /** * This method was created in VisualAge. */ public void changedUpdate(DocumentEvent e) { } /** * This method was created in VisualAge. */ private void initialize() { setSize(300, 200); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (!runningAsApplet) System.exit(0); } } ); Container contentPane = getContentPane(); contentPane.setLayout(new GridLayout(5,1)); JPanel numLocPanel = new JPanel(); JPanel numSafeLocPanel = new JPanel(); JPanel numTransPanel = new JPanel(); JPanel numEvacPanel = new JPanel(); //JPanel buttonPanel = new JPanel(); numLocField = new JTextField(""+numLocations, 6); numSafeLocField = new JTextField(""+numSafetyLocations, 6); numTransField = new JTextField(""+numTransports, 6); numEvacField = new JTextField(""+numEvacuees, 6); numLocField.getDocument().addDocumentListener(this); numSafeLocField.getDocument().addDocumentListener(this); numTransField.getDocument().addDocumentListener(this); numEvacField.getDocument().addDocumentListener(this); JLabel numLocLabel = new JLabel("Number of locations"); JLabel numSafeLocLabel = new JLabel("Number of safety locations"); JLabel numTransLabel = new JLabel("Number of transports"); JLabel numEvacLabel = new JLabel("Number of evacuees"); startButton = new JButton("Start"); startButton.addActionListener(this); numLocPanel.add(numLocLabel); numLocPanel.add(numLocField); numSafeLocPanel.add(numSafeLocLabel); numSafeLocPanel.add(numSafeLocField); numTransPanel.add(numTransLabel); numTransPanel.add(numTransField); numEvacPanel.add(numEvacLabel); numEvacPanel.add(numEvacField); //buttonPanel.add(startButton); contentPane.add(numLocPanel); contentPane.add(numSafeLocPanel); contentPane.add(numTransPanel); contentPane.add(numEvacPanel); contentPane.add(startButton); } /** * This method was created in VisualAge. */ public void insertUpdate(DocumentEvent e) { } /** * This method was created in VisualAge. */ public static void main(String[] args) { runningAsApplet = false; ctrl = new Control(runningAsApplet); SimulatorStartFrame f = new SimulatorStartFrame(ctrl); f.show(); } /** * This method was created in VisualAge. */ public void removeUpdate(DocumentEvent e) { } /** * This method was created in VisualAge. */ public void run() { runningAsApplet = true; ctrl = new Control(runningAsApplet); initialize(); Thread t = new Thread(ctrl); t.setPriority(Thread. MAX_PRIORITY); t.start(); show(); while (true) { try { Thread.sleep(100); } catch(Exception e) {} } } /** * This method was created in VisualAge. */ private void updateForInput() { try { numLocations = Integer.parseInt(numLocField.getText().trim()); numSafetyLocations = Integer.parseInt(numSafeLocField.getText().trim()); numTransports = Integer.parseInt(numTransField.getText().trim()); numEvacuees = Integer.parseInt(numEvacField.getText().trim()); if (numSafetyLocations > numLocations) numSafetyLocations = numLocations; } catch(Exception e) { System.out.println("Bad input exception: " + e); } } }