connect simulation to display

This commit is contained in:
COLIN Cyril 2019-10-16 13:35:26 +02:00
parent 579f33d34e
commit f58b035252
4 changed files with 38 additions and 43 deletions

View File

@ -26,7 +26,8 @@ public class ElevatorApplication implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
canvas.setElevatorY((canvas.getElevatorY() + 1) % canvas.getHeight()); elevator.update();
canvas.repaint();
} }
private void createAndShowGUI() { private void createAndShowGUI() {

View File

@ -11,7 +11,6 @@ class ElevatorCanvas extends JPanel {
private final static int HEIGHT = 50; private final static int HEIGHT = 50;
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200); private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
private int y = 0;
private Elevator elevator; private Elevator elevator;
public ElevatorCanvas(Elevator elevator) { public ElevatorCanvas(Elevator elevator) {
@ -46,16 +45,7 @@ class ElevatorCanvas extends JPanel {
else { else {
g.setColor(Color.BLACK); g.setColor(Color.BLACK);
} }
g.fillRect(0, y, WIDTH, HEIGHT); g.fillRect(0, 200 - (int) Math.floor(elevator.getHeight() * 200) - HEIGHT, WIDTH, HEIGHT);
Toolkit.getDefaultToolkit().sync(); Toolkit.getDefaultToolkit().sync();
} }
public void setElevatorY(int y) {
this.y = y;
repaint();
}
public int getElevatorY() {
return y;
}
} }

View File

@ -23,6 +23,7 @@ public class Elevator {
height = (double) currentFloor / nbFloors; height = (double) currentFloor / nbFloors;
floorHeight = 1. / nbFloors; floorHeight = 1. / nbFloors;
step = floorHeight / PRECISION; step = floorHeight / PRECISION;
System.out.println("step: " + step + ", floorHeight: " + floorHeight);
} }
public void setActionListener(ElevatorListener listener) { public void setActionListener(ElevatorListener listener) {
@ -41,7 +42,7 @@ public class Elevator {
direction = Direction.DOWN; direction = Direction.DOWN;
} }
public void stopElevator() { public void stop() {
direction = Direction.NONE; direction = Direction.NONE;
} }
@ -73,7 +74,7 @@ public class Elevator {
/* Passed a floor, invoke the listener. */ /* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) { if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
listener.floorChange(Direction.UP); listener.floorChange(Direction.DOWN);
} }
// /* Go halfway below. */ // /* Go halfway below. */
// if (!betweenFloors) currentFloor--; // if (!betweenFloors) currentFloor--;
@ -81,6 +82,9 @@ public class Elevator {
break; break;
case UP: case UP:
height += step; height += step;
System.out.println("height: " + height
+ ", prevFloor: " + prevFloor
+ ", floor: " + Math.floor(height / floorHeight));
/* Top reached. */ /* Top reached. */
if (height > 1-step) { if (height > 1-step) {
@ -89,8 +93,8 @@ public class Elevator {
} }
/* Passed a floor, invoke the listener. */ /* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) { if (Math.floor(height / floorHeight) > prevFloor && listener != null) {
listener.floorChange(Direction.DOWN); listener.floorChange(Direction.UP);
} }
// /* Go halfway above. */ // /* Go halfway above. */
// if (betweenFloors) currentFloor++; // if (betweenFloors) currentFloor++;

View File

@ -6,12 +6,13 @@ import Events.CallFromFloorEvent;
import commandSystem.Direction; import commandSystem.Direction;
import commandSystem.ElevatorListener; import commandSystem.ElevatorListener;
import simulation.Elevator; import simulation.Elevator;
import Events.*;
public class Simulation implements ElevatorListener { public class Simulation implements ElevatorListener {
private boolean running = false; private boolean running = false;
private int currentFloor = 0; private int currentFloor = 0;
private BasicInstructionQueue queue; private BasicInstructionQueue queue = new BasicInstructionQueue();
private Elevator elevator; private Elevator elevator;
public Simulation(Elevator elevator) { public Simulation(Elevator elevator) {
@ -20,14 +21,19 @@ public class Simulation implements ElevatorListener {
public void elevatorCall(int floor) { public void elevatorCall(int floor) {
System.out.println("elevator call " + floor); System.out.println("elevator call " + floor);
queue.computeEvent(new CallFromElevatorEvent(floor));
update();
} }
public void floorCall(int floor, Direction direction) { public void floorCall(int floor, Direction direction) {
System.out.println("floor call " + floor + " " + direction); System.out.println("floor call " + floor + " " + direction);
queue.computeEvent(new CallFromFloorEvent(floor, direction));
update();
} }
public void floorChange(Direction d) { public void floorChange(Direction direction) {
switch (d) { System.out.println("floor change " + direction);
switch (direction) {
case UP: case UP:
currentFloor++; currentFloor++;
break; break;
@ -35,30 +41,24 @@ public class Simulation implements ElevatorListener {
currentFloor--; currentFloor--;
break; break;
} }
// iter(); queue.computeEvent(new ReachedFloorEvent(currentFloor, direction));
update();
} }
// public void iter() { private void update() {
// int nextDestination = queue.getNextInstruction(); final int next = queue.getNextInstruction();
// int currentFloor = 0; System.out.println("next: " + next);
// //System.out.println("next dest = " + nextDestination); if (next == -1 || next == currentFloor) {
// if (!running) { System.out.println("stopping");
// if (nextDestination > 0) { /* We have a destination to go to. */ elevator.stop();
// running = true; }
// elevator.goDown(); else if (next > currentFloor) {
// } System.out.println("going up");
// } elevator.goUp();
// else { }
// if (elevator.currentFloor == nextDestination) { else if (next < currentFloor) {
// queue.removeInstruction(elevator.currentFloor, elevator.direction); System.out.println("going down");
// running = false; elevator.goDown();
// } }
// else { }
// elevator.direction = elevator.currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
// elevator.currentFloor += elevator.direction == Direction.UP ? 1 : -1;
// }
// }
// System.out.println("elevator floor " + elevator.currentFloor + " direction " +
// (elevator.direction == Direction.UP? "up" : "down") + " running: " + running);
// }
} }