📄 airportbuilder.java.svn-base
字号:
package gui;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import javax.swing.*;import base.*;import world.*;import agents.*;import interfaces.*;public class AirportBuilder extends JFrame implements ActionListener, MouseListener { private static final int MAX_RUNWAYS = 2; //keeping track of the states of the airport builder so airport builder animation knows what to draw enum State { IDLE, ADDING_RUNWAY, ADDING_TOWER, ADDING_GATE, ADDING_TAXIWAY }; private AirportBuilderPanel airportBuilderPanel; private AirportBuilderAnimation airportBuilderAnimation; private ArrayList<BuilderObject> builderObjects = new ArrayList<BuilderObject>(); //all builder objects currently on screen private boolean userControlled,cdControlled, gcControlled, lcControlled;; State state = State.IDLE; int numRunways = 0, numGates = 0, numTaxiways = 0; boolean madeTaxiways = false, addedTower = false; Color taxiwayColor = Color.BLUE; public AirportBuilder() { JPanel windowPanel = new JPanel(); windowPanel.setLayout(new GridLayout(1, 2)); airportBuilderPanel = new AirportBuilderPanel(this); airportBuilderAnimation = new AirportBuilderAnimation(this); windowPanel.add(airportBuilderPanel); windowPanel.add(airportBuilderAnimation); setContentPane(windowPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void setUserControlled(boolean enable, boolean cdEnable, boolean gcEnable, boolean lcEnable) { userControlled = enable; cdControlled = cdEnable; gcControlled = gcEnable; lcControlled = lcEnable; } public boolean isAddingRunway() { return state == State.ADDING_RUNWAY; } public boolean isAddingTower() { return state == State.ADDING_TOWER; } public boolean isAddingTaxiway() { return state == State.ADDING_TAXIWAY; } public boolean isAddingGate() { return state == State.ADDING_GATE; } public ArrayList<BuilderObject> getBuilderObjects() { return builderObjects; } public void addingTower() { if(state == State.IDLE) { state = State.ADDING_TOWER; airportBuilderPanel.setNameEnabled(true); airportBuilderPanel.setNameText("LA Tower"); airportBuilderAnimation.makeNewTower(); } else if(state == State.ADDING_TOWER) { state = State.IDLE; } airportBuilderPanel.setButtonText("addTower", (state == State.IDLE ? "Add Tower" : "Cancel Tower")); addedTower = true; } public void finishAddingTower(BuilderObject builderObject) { builderObject.setName(airportBuilderPanel.getNameText()); builderObjects.add(builderObject); airportBuilderPanel.setNameEnabled(false); airportBuilderPanel.setNameText(""); airportBuilderPanel.setButtonText("addTower", "Tower Set"); airportBuilderPanel.setButtonEnabled("addTower", false); if(numGates > 0) { airportBuilderPanel.setButtonEnabled("makeTaxiways", true); } state = State.IDLE; } public void addingRunway(int angle) { state = State.ADDING_RUNWAY; airportBuilderPanel.setNameEnabled(true); airportBuilderPanel.setNameText("Runway" + (numRunways + 1)); //disable runway buttons airportBuilderPanel.enableRunwayButtons(false); airportBuilderAnimation.makeNewRunway(angle); } public void finishAddingRunway(BuilderObject builderObject) { builderObject.setName(airportBuilderPanel.getNameText()); builderObjects.add(builderObject); numRunways++; airportBuilderPanel.setNameEnabled(false); airportBuilderPanel.setNameText(""); if(numRunways < MAX_RUNWAYS) { airportBuilderPanel.enableRunwayButtons(true); } else { airportBuilderPanel.enableRunwayButtons(false); } state = State.IDLE; } public void addingGate() { if(state == State.IDLE) { state = State.ADDING_GATE; airportBuilderPanel.setNameEnabled(true); airportBuilderPanel.setNameText("Gate" + (numGates + 1)); airportBuilderAnimation.makeNewGate(); } else if(state == State.ADDING_GATE) { state = State.IDLE; } airportBuilderPanel.setButtonText("addGate", (state == State.IDLE ? "Add Gate" : "Cancel Gate")); } public void finishAddingGate(BuilderObject builderObject) { builderObject.setName(airportBuilderPanel.getNameText()); builderObjects.add(builderObject); numGates++; airportBuilderPanel.setNameEnabled(false); airportBuilderPanel.setNameText(""); airportBuilderPanel.setButtonText("addGate", "Add Gate"); if(addedTower) { airportBuilderPanel.setButtonEnabled("makeTaxiways", true); } state = State.IDLE; } public void makeTaxiways() { final int taxiwayWidth = 20; ArrayList<BuilderObject> gateBuilderObjects = new ArrayList<BuilderObject>(); for(BuilderObject bo : builderObjects) { if(((GuiObject) bo.getGuiObject()).type == "Gate") { gateBuilderObjects.add(bo); } } //for each gate, find the nearest runway using the distance formula boolean shortestSet; double shortestDistance, thisDistance; BuilderObject nearestRunway; WorldVector gatePosition, runwayPosition; ArrayList<WorldVector> gateVertices, runwayVertices; for(BuilderObject gate : gateBuilderObjects) { shortestSet = false; shortestDistance = thisDistance = 0; nearestRunway = new BuilderObject(); gatePosition = gate.getObjectState().position; gateVertices = (ArrayList<WorldVector>) gate.getObjectState().vertices; for(BuilderObject bo : builderObjects) { if(((GuiObject) bo.getGuiObject()).type == "Runway") { runwayPosition = bo.getObjectState().position; runwayVertices = (ArrayList<WorldVector>) bo.getObjectState().vertices; thisDistance = Math.sqrt(Math.pow((runwayPosition.x - gatePosition.x), 2) + Math.pow((runwayPosition.y - gatePosition.y), 2)); if(!shortestSet || thisDistance < shortestDistance) { shortestDistance = thisDistance; nearestRunway = bo; shortestSet = true; } } } runwayPosition = nearestRunway.getObjectState().position; runwayVertices = (ArrayList<WorldVector>) nearestRunway.getObjectState().vertices; Point gateConnection = new Point((int) gatePosition.x, (int) (gatePosition.y + gateVertices.get(3).y)); Point runwayConnection = new Point((int) gatePosition.x, (int) (runwayPosition.y + runwayVertices.get(0).y)); int taxiwayHeight = Math.abs(gateConnection.y - runwayConnection.y); gate.getObjectState().connections.add(new WorldVector(gateConnection.x - gatePosition.x, gateConnection.y - gatePosition.y, 0)); nearestRunway.getObjectState().connections.add(new WorldVector(runwayConnection.x - runwayPosition.x, runwayConnection.y - runwayPosition.y, 0)); WorldVector taxiwayPosition = new WorldVector(gateConnection.x, (gateConnection.y + runwayConnection.y) / 2, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -