1 import objectdraw.WindowController; 2 import objectdraw.FilledOval; 3 import objectdraw.FramedRect; 4 import objectdraw.Text; 5 import objectdraw.Location; 6 import objectdraw.Line; 7 import java.awt.Color; 8 9 10 11 public class ElevatorSystemArrays extends WindowController{ 12 13 //private Elevator[] elevator; 14 private Elevator newElevator; 15 private ElevatorControlPanelD newCtrlPanel; 16 private FilledOval buttons; 17 private Line betweenVert; 18 private Line betweenHori; 19 //private ElevatorControlPanelD[] eleControl; 20 //private FilledOval[] callButton; 21 private FilledOval button; 22 23 private static final int PIXELSPERLEVEL = 30; 24 private static final int NUMLEVELS = 10; 25 private static final int NUMELEVATORS = 10; 26 27 public Elevator[] elevator = new Elevator[NUMELEVATORS]; 28 public FilledOval[] callButton = new FilledOval[NUMLEVELS]; 29 public ElevatorControlPanelD[] eleControl = new ElevatorControlPanelD[NUMELEVATORS]; 30 31 public void begin() 32 { 33 //building elevators 34 int eleStartLocX = 10; 35 int eleStartLocY = (PIXELSPERLEVEL * NUMLEVELS) + 10; 36 int systemWidth = (20 * NUMELEVATORS) + 10; 37 int systemHeight = PIXELSPERLEVEL * NUMLEVELS; 38 int systemTop = 40; 39 int LevelTop = systemTop; 40 int systemBottom = 40 + systemHeight; 41 42 //creates elevator floor bases 43 for( int i = 0; i < NUMLEVELS; i++) { 44 betweenHori = new Line (10, LevelTop, systemWidth, LevelTop, canvas); 45 LevelTop += PIXELSPERLEVEL; 46 } 47 //creates the lines between elevators and the elevators 48 for( int i = 0; i < elevator.length; i++){ 49 betweenVert = new Line(eleStartLocX, systemTop, eleStartLocX, systemBottom, canvas); 50 elevator[i] = newElevator = new Elevator(new Location(eleStartLocX, eleStartLocY), PIXELSPERLEVEL, canvas); 51 eleStartLocX += 20; 52 } 53 betweenVert = new Line(eleStartLocX, systemTop, eleStartLocX, systemBottom, canvas); 54 betweenVert = new Line(eleStartLocX + 40, systemTop, eleStartLocX + 40, systemBottom, canvas); 55 betweenHori = new Line (10, systemBottom, systemWidth, systemBottom, canvas); 56 57 //building call buttons 58 59 int buttonLocY = systemBottom - ((PIXELSPERLEVEL/2)+10); 60 int buttonLocX = eleStartLocX + 10; 61 for( int i = 0; i < callButton.length; i++) 62 { 63 callButton[i] = button = new FilledOval(buttonLocX, buttonLocY, 20, 20, canvas); 64 button.setColor(Color.yellow); 65 buttonLocY -= PIXELSPERLEVEL; 66 } 67 68 //building control panels 69 int ctrlPanelStartLocX = buttonLocX + 40; 70 int ctrlPanelStartLocY = systemTop; 71 int ctrlPanelHeight = 50; 72 73 for( int i = 0; i < NUMELEVATORS; i++){ 74 eleControl[i] = newCtrlPanel = new ElevatorControlPanelD(elevator[i], i+1 , NUMLEVELS, new Location(ctrlPanelStartLocX, ctrlPanelStartLocY), canvas); 75 ctrlPanelStartLocY += ctrlPanelHeight; 76 } 77 } 78 public void onMouseClick(Location point){ 79 for( int i = 0; i < NUMLEVELS; i++) { 80 if( callButton[i].contains(point)){ 81 nearestElevator(i+1).moveToLevel(i+1); 82 } 83 } 84 //loop through and do a .contains on each element on the array 85 for( int i = 0; i < eleControl.length; i++){ 86 if (eleControl[i].contains(point)){ 87 eleControl[i].processMouseClick(point); 88 } 89 } 90 } 91 public Elevator nearestElevator(int level) 92 { 93 int numMin = 0; 94 int minDist = Math.abs(elevator[0].level() - level); 95 96 for(int num = 1; num < elevator.length; num++){ 97 int dist = Math.abs(elevator[num].level() - level); 98 if(dist < minDist){ 99 minDist = dist; 100 numMin = num; 101 } 102 } 103 return elevator[numMin]; 104 } 105 106 } 107