startguinormi.java.svn-base
来自「這是一個JAVA語言寫的多代理人程式用來模擬飛機起飛或是降落的程式」· SVN-BASE 代码 · 共 333 行
SVN-BASE
333 行
package gui;import interfaces.ClearanceDeliveryInterface;import interfaces.CoPilotInterface;import interfaces.GroundControllerInterface;import interfaces.LocalControllerInterface;import interfaces.PilotInterface;import interfaces.*;import java.util.ArrayList;import java.util.Timer;import java.util.TimerTask;import javax.swing.JFrame;import world.Airport;import world.Gate;import world.Plane;import world.RealPlane;import world.Runway;import world.Taxiway;import world.Tower;import agents.ClearanceDeliveryAgent;import agents.CoPilotAgent;import agents.GroundControllerAgent;import agents.LocalControllerAgent;import agents.PilotAgent;import base.Agent;import base.World;import base.WorldObject;import base.WorldObjectState;import base.WorldVector;import java.util.ConcurrentModificationException; public class StartGuiNoRMI { public ArrayList<BuilderObject> builderObjects; public boolean cdControlled, gcControlled, lcControlled; public boolean userControlled; public Airport airport; ArrayList<PilotInterface> pilotAgents = new ArrayList<PilotInterface>(); ArrayList<CoPilotInterface> coPilotAgents = new ArrayList<CoPilotInterface>(); ClearanceDeliveryInterface clearanceDeliveryAgent; public ClearanceDeliveryInterface clearanceDelivery; public GroundControllerInterface groundControl; public LocalControllerInterface localControl; public ClearanceDeliveryAgent cdagent; public GroundControllerAgent gcagent; public LocalControllerAgent lcagent; public PilotGUI pilotGui; public CoPilotGUI coPilotGui; public AgentControllerGUI agentControllerGui; public AirplaneManagerGUI airplaneManagerGui; public MessageGUI messageGUI; MainPanel mainPanel; Tower tower; public boolean reset() { System.out.println("Adding agents"); tower = airport.towers.get(0); synchronized(World.world_objects) { try { for (WorldObject planeObj : World.world_objects) { if (planeObj instanceof Plane) { // World.world_objects.remove(World.world_objects.indexOf(planeObj)); planeObj.objectState.position.x = -100000; planeObj.objectState.position.y = -100000; planeObj.objectState.position.z = 100000; } } } catch (ConcurrentModificationException e) { // e.printStackTrace(); System.out.println("Please wait before loading another scenario"); return false; } } String cdName = "ClearanceDeliveryAgent"; clearanceDelivery = new ClearanceDeliveryAgent(cdName, tower); agentControllerGui.addClearanceDelivery(cdName); ((Agent) (clearanceDelivery)).startThread(); String gcName = "GroundControlAgent"; groundControl = new GroundControllerAgent(gcName, tower); agentControllerGui.addGroundControl(gcName); ((Agent) (groundControl)).startThread(); String lcName = "LocalControlAgent"; localControl = new LocalControllerAgent(lcName, tower); tower.localController = localControl; agentControllerGui.addLocalControl(lcName); ((Agent) (localControl)).startThread(); return true; } public StartGuiNoRMI(ArrayList<BuilderObject> builderObjects, boolean userControlled, boolean cdControlled, boolean gcControlled, boolean lcControlled) { this.builderObjects = builderObjects; this.userControlled = userControlled; this.cdControlled = cdControlled; this.gcControlled = gcControlled; this.lcControlled = lcControlled; createAirport(); JFrame tabbedFrame = new JFrame(); mainPanel = new MainPanel(this); tabbedFrame.add(mainPanel); tabbedFrame.pack(); tabbedFrame.setLocation(550, 0); tabbedFrame.setSize(900, 700); tabbedFrame.setVisible(true); tabbedFrame.setDefaultCloseOperation(tabbedFrame.EXIT_ON_CLOSE); tabbedFrame.update(tabbedFrame.getGraphics()); JFrame animationFrame = new JFrame(); AirportAnimation airportAnimation = new AirportAnimation(); animationFrame.add(airportAnimation); animationFrame.pack(); animationFrame.setBackground(airportAnimation.getBG()); animationFrame.setLocation(0, 0); animationFrame.setSize(700, 700); animationFrame.setVisible(true); animationFrame.setDefaultCloseOperation(animationFrame.EXIT_ON_CLOSE); Animation.g = animationFrame.getGraphics(); Animation.frame = animationFrame; Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { runWorld(); } }, 1000); } public void createAirport() { ArrayList<Runway> runways = new ArrayList<Runway>(); ArrayList<Taxiway> taxiways = new ArrayList<Taxiway>(); ArrayList<Gate> gates = new ArrayList<Gate>(); ArrayList<Tower> towers = new ArrayList<Tower>(); BuilderObject builderObject; String type; for(int i = 0; i < builderObjects.size(); i++) { builderObject = builderObjects.get(i); type = ((GuiObject) builderObject.getGuiObject()).type; if(type == "Runway") { runways.add(new Runway(builderObject.getName(), (WorldObjectState) builderObject.getObjectState(), (GuiObject) builderObject.getGuiObject())); } else if(type =="Taxiway") { taxiways.add(new Taxiway(builderObject.getName(), (WorldObjectState) builderObject.getObjectState(), (GuiObject) builderObject.getGuiObject())); } else if(type == "Gate") { gates.add(new Gate(builderObject.getName(), (WorldObjectState) builderObject.getObjectState(), (GuiObject) builderObject.getGuiObject())); } else if(type == "Tower") { towers.add(new Tower(builderObject.getName(), (WorldObjectState) builderObject.getObjectState(), (GuiObject) builderObject.getGuiObject())); } } airport = new Airport("LAX", runways, taxiways, gates, towers); for(int i = 0; i < airport.towers.size(); i++) { airport.towers.get(i).setAirport(airport); } tower = airport.towers.get(0); if(lcControlled) { System.out.println("Adding LC Agent"); String lcName = "LocalControllerAgent"; localControl = new LocalControllerAgent(lcName, tower); // agentControllerGui.addClearanceDelivery(cdName); ((Agent) (localControl)).startThread(); } if(gcControlled) { System.out.println("Adding GC Agent"); String gcName = "GroundControlAgent"; groundControl = new GroundControllerAgent(gcName, tower); // agentControllerGui.addGroundControl(gcName); ((Agent) (groundControl)).startThread(); } if(cdControlled) { System.out.println("Adding CD Agent"); String cdName = "ClearanceDeliveryAgent"; clearanceDelivery = new ClearanceDeliveryAgent(cdName, tower); // agentControllerGui.addClearanceDelivery(cdName); ((Agent) (clearanceDelivery)).startThread(); } } public void createAgent(String type, String name) { if(type == "ClearanceDelivery") { clearanceDeliveryAgent = new ClearanceDeliveryAgent(name, airport.towers.get(0)); ((Agent) clearanceDeliveryAgent).startThread(); } else if(type == "GroundController") { groundControl = new GroundControllerAgent(name, airport.towers.get(0)); ((Agent) groundControl).startThread(); } else { LocalControllerAgent localController = new LocalControllerAgent(name, airport.towers.get(0)); ((Agent) localController).startThread(); } } public void createAgent(String type, String name, Plane plane) { if(type == "Pilot") { PilotAgent pilotAgent = new PilotAgent(name, plane); pilotAgents.add(pilotAgent); ((Agent) pilotAgent).startThread(); mainPanel.addPilot(name); } else if(type == "CoPilot") { CoPilotAgent coPilotAgent = new CoPilotAgent(name, plane); coPilotAgents.add(coPilotAgent); ((Agent) coPilotAgent).startThread(); mainPanel.addCoPilot(name); } } public void createPlane(Runway runway, Gate gate, boolean arriving, String callNumber, String destination, boolean userControlled) { Plane plane = new RealPlane(); GuiObject planeGui = new GuiObject("Plane"); planeGui.setTexture("resources/textures/boeing777.png"); WorldVector initialPosition = new WorldVector(); WorldVector initialHeading = new WorldVector(); int textureHeight = planeGui.texture.getHeight(), textureWidth = planeGui.texture.getWidth(); ArrayList<WorldVector> vertices = new ArrayList<WorldVector>(); vertices.add(new WorldVector(-(textureWidth / 2), -(textureHeight / 2), 0)); vertices.add(new WorldVector((textureWidth / 2), -(textureHeight / 2), 0)); vertices.add(new WorldVector((textureWidth / 2), (textureHeight / 2), 0)); vertices.add(new WorldVector(-(textureWidth / 2), (textureHeight / 2), 0)); WorldObjectState runwayObjectState = runway.getObjectState(); if(arriving) { initialPosition.x = -50; initialPosition.y = runwayObjectState.position.y; initialPosition.z = 1000; mainPanel.addArrivingFlight(callNumber); } else { initialPosition.x = gate.getObjectState().position.x; initialPosition.y = gate.getObjectState().position.y; initialPosition.z = 1; if(cdControlled) { mainPanel.addDepartingFlight(callNumber, runway); } } WorldObjectState planeState = new WorldObjectState(initialPosition, initialHeading, vertices); planeState.heading = runwayObjectState.heading; plane = new RealPlane(callNumber, planeState, planeGui); airport.towers.get(0).addPlane(callNumber, plane); if(!userControlled) { createAgent("Pilot", "Pilot" + (pilotAgents.size() + 1), plane); createAgent("CoPilot", "CoPilot" + (pilotAgents.size() + 1), plane); //mainPanel.setFocus("Agent Controller"); } if(!userControlled) {}//((Agent)airport.towers.get(0).groundController).startThread(); if(arriving) { PilotInterface thisPilot = pilotAgents.get(pilotAgents.size() - 1); PilotInterface coPilot = coPilotAgents.get(coPilotAgents.size()-1); LocalControllerInterface thisLocalController = airport.towers.get(0).localController; if(!userControlled) { /* DONT NEED THIS CODE System.out.println("Starting LocalController Thread"); ((LocalControllerAgent)thisLocalController).startThread(); ((PilotAgent)thisPilot).startThread(); ((ClearanceDeliveryAgent)clearanceDelivery).startThread(); */ } // coPilot.setLocalController(thisLocalController); thisLocalController.msgNewArrivingFlight(plane, callNumber, runway, gate); thisPilot.msgNewArrivingFlight(callNumber, runway, gate, thisLocalController); coPilot.msgNewArrivingFlight(callNumber, runway, gate, thisLocalController); } else { PilotInterface thisPilot = pilotAgents.get(pilotAgents.size() - 1); PilotInterface coPilot = coPilotAgents.get(coPilotAgents.size()-1); if(!userControlled) { /* DONT NEED THIS CODE System.out.println("Starting Thread"); ((ClearanceDeliveryAgent)clearanceDelivery).startThread(); ((Agent)airport.towers.get(0).localController).startThread(); ((PilotAgent)thisPilot).startThread(); */ } clearanceDelivery.msgNewDepartingFlight(plane, callNumber, runway, gate, destination, thisPilot); thisPilot.msgNewDepartingFlight(callNumber, runway, gate, destination, clearanceDelivery); coPilot.msgNewDepartingFlight(callNumber, runway, gate, destination, clearanceDelivery); } } public Plane getPlane(String callNumber) { RealPlane plane = (RealPlane) airport.towers.get(0).getPlane(callNumber); return plane; } public void movePlane(String callNumber) { RealPlane plane = (RealPlane) airport.towers.get(0).getPlane(callNumber); plane.getObjectState().velocity = new WorldVector(25, 0, 50); plane.getObjectState().acceleration = new WorldVector(10, 0, 10); plane.getObjectState().force = new WorldVector(10, 0, 10); plane.getObjectState().mass = 500; } public ArrayList<PilotInterface> getPilotAgents() { return pilotAgents; } public ArrayList<CoPilotInterface> getCoPilotAgents() { return coPilotAgents; } public void runWorld() { World.setAnimation(new Animation()); World.startWorld(100, 3); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?