📄 airplanemanagergui.java.svn-base
字号:
package gui;import java.util.*;import javax.swing.*;import agents.PilotAgent.PilotState;import agents.PilotAgent;import agents.*;import java.awt.*;import java.awt.event.*;import java.util.Timer;import java.util.TimerTask;import world.*;public class AirplaneManagerGUI extends JPanel implements ActionListener { StartGuiNoRMI gui; JPanel tabPane2, listPanel; JButton add_button5; JScrollPane scroll5; JPanel testPanel; JTextField radioNum; JLabel airplaneLabel; JLabel scenario1, scenario2, scenario3, scenario4, scenario5; JPanel airplaneControl, airplaneDisplay, radioInfo, gateInfo, runwayInfo, destinationPanel, destinationInfo; JLabel radioLabel, gatesLabel, runwaysLabel, destinationsLabel; JComboBox gatesBox, runwaysBox, destinationBox; // New gui JComboBox scenarioBox; JButton btn_runScenario; JRadioButton arriving, departing, hiddenRadio; ButtonGroup group = new ButtonGroup(); ArrayList<Runway> runways; ArrayList<Gate> gates; public AirplaneManagerGUI(StartGuiNoRMI gui) { testPanel = new JPanel(); testPanel.setLayout(new GridLayout(1,1)); this.gui = gui; this.runways = (ArrayList<Runway>) gui.airport.runways; this.gates = (ArrayList<Gate>) gui.airport.gates; createAirplanesTab(); }//*** Creates the Airplanes tab. Panel belongs to Thomas*** public void createAirplanesTab(){ if(gui.userControlled) { String[] gateNames = new String[gates.size()]; String[] runwayNames = new String[runways.size()]; for(int i = 0; i < gates.size(); i++) { gateNames[i] = gates.get(i).getName(); } for(int i = 0; i < runways.size(); i++) { runwayNames[i] = runways.get(i).getName(); } tabPane2 = new JPanel(); tabPane2.setLayout(new BoxLayout(tabPane2, BoxLayout.X_AXIS)); airplaneControl = new JPanel(); airplaneDisplay = new JPanel(); radioInfo = new JPanel(); destinationPanel = new JPanel(); destinationInfo = new JPanel(); gateInfo = new JPanel(); runwayInfo = new JPanel(); airplaneLabel = new JLabel("Airplanes"); radioLabel = new JLabel("Call Number:"); gatesLabel = new JLabel("Choose a Gate:"); runwaysLabel = new JLabel("Choose a Runway:"); destinationsLabel = new JLabel("Choose a Destination:"); add_button5 = new JButton("Add"); gatesBox = new JComboBox(gateNames); runwaysBox = new JComboBox(runwayNames); destinationBox = new JComboBox(); destinationBox.setEnabled(false); radioNum = new JTextField(20); scroll5 = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); listPanel = new JPanel(); listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.Y_AXIS)); scroll5.add(listPanel); arriving = new JRadioButton("Arriving"); arriving.setSelected(true); arriving.addActionListener(this); departing = new JRadioButton("Departing"); departing.addActionListener(this); hiddenRadio = new JRadioButton(); hiddenRadio.setVisible(false); group.add(arriving); group.add(departing); group.add(hiddenRadio); airplaneControl.setLayout(new BoxLayout(airplaneControl, BoxLayout.Y_AXIS)); airplaneControl.add(airplaneLabel); airplaneControl.add(scroll5); airplaneDisplay.setLayout(new GridLayout(6,1)); airplaneDisplay.add(radioInfo); airplaneDisplay.add(destinationPanel); airplaneDisplay.add(gateInfo); airplaneDisplay.add(runwayInfo); airplaneDisplay.add(destinationInfo); airplaneDisplay.add(add_button5); //***Creates a new Airplane object*** add_button5.addActionListener(this); radioInfo.add(radioLabel); radioInfo.add(radioNum); gateInfo.add(gatesLabel); gateInfo.add(gatesBox); runwayInfo.add(runwaysLabel); runwayInfo.add(runwaysBox); destinationPanel.add(arriving); destinationPanel.add(departing); destinationInfo.add(destinationsLabel); destinationInfo.add(destinationBox); tabPane2.add(airplaneControl); tabPane2.add(airplaneDisplay); testPanel.add(tabPane2); setLayout(new GridLayout(1,1)); add(testPanel); } else { tabPane2 = new JPanel(); tabPane2.setLayout(new BoxLayout(tabPane2, BoxLayout.X_AXIS)); airplaneDisplay = new JPanel(); btn_runScenario = new JButton("Run scenario"); scenarioBox = new JComboBox(); scenario1 = new JLabel("Scenario 1: 1 airplane landing and taking off"); scenario2 = new JLabel("Scenario 2: 2 airplanes landing and taking off"); scenario3 = new JLabel("Scenario 3: 1 airplane, pilot dies"); scenario4 = new JLabel("Scenario 4: 2 airplanes land at the same gate and form a queue"); scenario5 = new JLabel("Scenario 5: 1 airplane landing 1 taking off"); for(int i = 1; i < 6; i++) { scenarioBox.addItem("Scenario " + i); } btn_runScenario.addActionListener(this); airplaneDisplay.setLayout(new GridLayout(0,2)); airplaneDisplay.add(scenarioBox); airplaneDisplay.add(btn_runScenario); airplaneDisplay.add(scenario1); airplaneDisplay.add(scenario2); airplaneDisplay.add(scenario3); airplaneDisplay.add(scenario4); airplaneDisplay.add(scenario5); tabPane2.add(airplaneDisplay); testPanel.add(tabPane2); setLayout(new GridLayout(1,1)); add(testPanel); } } public void resetForm() { radioNum.setText(""); arriving.setSelected(true); departing.setSelected(false); destinationBox.setEnabled(false); } public void setDestinations(String[] destinations) { //destinationBox.removeAll(); if(gui.userControlled) { for(int i = 0; i < destinations.length; i++) { destinationBox.addItem(destinations[i]); } } } /* Scenarios * 1. 1 plane landing and taking off * 2. 2 planes landing and taking off going to diff gates * 3. 1 plane landing and taking off with pilot dying * 4. 2 planes landing and going to same gates * 5. */ public void runScenario(int scenario) { switch(scenario) { case 0: System.out.println("Running scenario 1"); final String callNum0 = callNum(false); gui.createPlane(runways.get(0), gates.get(0),true, callNum0, "SFO", false); Timer timer0 = new Timer(); timer0.schedule(new TimerTask() { public void run() { PilotAgent pilot = (PilotAgent)gui.getPlane(callNum0).msgPilot(); pilot.plane = gui.getPlane(callNum0); pilot.pilotState = PilotState.NEW_DEPARTURE; pilot.departing = runways.get(0); pilot.destinationGate = gates.get(0); pilot.destination = "NYC"; pilot.clearanceDelivery = gui.clearanceDelivery; pilot.changeState(); // gui.clearanceDelivery.msgNewDepartingFlight(gui.getPlane(callNum0), callNum0, runways.get(0), gates.get(0), "NYC", gui.getPlane(callNum0).msgPilot()); } }, 25000); activateBtn(35); break; case 1: System.out.println("Running scenario 2"); final String callnum1 = callNum(false); final String callnum2 = callNum(false); gui.createPlane(runways.get(0), gates.get(0),true, callnum1, "SFO", false); // Need to create a timer to simulate two planes coming in one after another // Otherwise, they come in at exactly the same time, which is not plausible Timer timer1 = new Timer(); timer1.schedule(new TimerTask() { public void run() { gui.createPlane(runways.get(0), gates.get(1),true, callnum2, "SFO", false); } }, 3000); // Telling the first plane to take off Timer timertakeoff = new Timer(); timertakeoff.schedule(new TimerTask() { public void run() { PilotAgent pilot = (PilotAgent)gui.getPlane(callnum1).msgPilot(); pilot.plane = gui.getPlane(callnum1); pilot.pilotState = PilotState.NEW_DEPARTURE; pilot.departing = runways.get(0); pilot.destinationGate = gates.get(0); pilot.destination = "NYC"; pilot.clearanceDelivery = gui.clearanceDelivery; pilot.changeState(); } }, 45000); Timer timertakeoff2 = new Timer(); timertakeoff2.schedule(new TimerTask() { public void run() { PilotAgent pilot = (PilotAgent)gui.getPlane(callnum2).msgPilot(); pilot.plane = gui.getPlane(callnum2); pilot.pilotState = PilotState.NEW_DEPARTURE; pilot.departing = runways.get(0); pilot.destinationGate = gates.get(1); pilot.destination = "NYC"; pilot.clearanceDelivery = gui.clearanceDelivery; pilot.changeState(); } }, 50000); activateBtn(60); break; case 2: System.out.println("Running scenario 3"); final String callNum3 = callNum(false); gui.createPlane(runways.get(0), gates.get(0),true, callNum3, "SFO", false); /* Timer timerkillpilot = new Timer(); timerkillpilot.schedule(new TimerTask() { public void run() { gui.getPlane(callNum3).promoteCoPilot(); // gui.clearanceDelivery.msgNewDepartingFlight(gui.getPlane(callNum0), callNum0, runways.get(0), gates.get(0), "NYC", gui.getPlane(callNum0).msgPilot()); } }, 10000); */ Timer timer3 = new Timer(); timer3.schedule(new TimerTask() { public void run() { gui.getPlane(callNum3).promoteCoPilot(); CoPilotAgent pilot = (CoPilotAgent)gui.getPlane(callNum3).radio(); pilot.plane = gui.getPlane(callNum3); pilot.setDepartureState(); pilot.departing = runways.get(0); pilot.destinationGate = gates.get(0); pilot.destination = "NYC"; pilot.groundController = gui.groundControl; pilot.clearanceDelivery = gui.clearanceDelivery; pilot.changeState(); // gui.clearanceDelivery.msgNewDepartingFlight(gui.getPlane(callNum0), callNum0, runways.get(0), gates.get(0), "NYC", gui.getPlane(callNum0).msgPilot()); } }, 25000); activateBtn(35); break; case 3: System.out.println("Running scenario 4"); final String callnum4 = callNum(false); final String callnum5 = callNum(false); gui.createPlane(runways.get(0), gates.get(0),true, callnum4, "SFO", false); // Need to create a timer to simulate two planes coming in one after another // Otherwise, they come in at exactly the same time, which is not plausible Timer timer4 = new Timer(); timer4.schedule(new TimerTask() { public void run() { gui.createPlane(runways.get(0), gates.get(0),true, callnum5, "SFO", false); } }, 3000); activateBtn(35); break; case 4: System.out.println("Running scenario 5"); final String callnum6 = callNum(false); final String callnum7 = callNum(false); gui.createPlane(runways.get(0), gates.get(0),true, callnum6, "SFO", false); // Need to create a timer to simulate two planes coming in one after another // Otherwise, they come in at exactly the same time, which is not plausible Timer timer5 = new Timer(); timer5.schedule(new TimerTask() { public void run() { gui.createPlane(runways.get(0), gates.get(1),false, callnum7, "SFO", false); } }, 2000); activateBtn(35); break; } } public String callNum(boolean haveairplane) { String callnum = new String(); if(!haveairplane) { if(Math.random() > 0.5) { callnum = "SWA"; } else { callnum = "MEX"; } callnum = callnum + callNum(true) + callNum(true) + callNum(true) + callNum(true) + callNum(true); } if(haveairplane) { Integer num = (int)(100*Math.random())%10; callnum = num.toString(); } return callnum; } public void activateBtn(int seconds) { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { btn_runScenario.setEnabled(true); } }, seconds*1000); } //***Listens to the Add button*** public void actionPerformed(ActionEvent e) { if(e.getSource() == btn_runScenario) { System.out.println("Run scenario " + scenarioBox.getSelectedIndex()); if(gui.reset()) runScenario(scenarioBox.getSelectedIndex()); btn_runScenario.setEnabled(false); } else if(e.getSource() == add_button5) { if(radioNum.getText().trim().intern() != "") { listPanel.add(new JLabel(radioNum.getText())); scroll5.setViewportView(listPanel); gui.createPlane(runways.get(runwaysBox.getSelectedIndex()), gates.get(gatesBox.getSelectedIndex()), arriving.isSelected(), radioNum.getText(), (String) (destinationBox.getSelectedItem()), false); resetForm(); } } else if(e.getSource() == arriving) { destinationBox.setEnabled(false); } else if(e.getSource() == departing) { destinationBox.setEnabled(true); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -