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
public void actionPerformed(ActionEvent e) {
canvas.setElevatorY((canvas.getElevatorY() + 1) % canvas.getHeight());
elevator.update();
canvas.repaint();
}
private void createAndShowGUI() {

View File

@ -11,7 +11,6 @@ class ElevatorCanvas extends JPanel {
private final static int HEIGHT = 50;
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
private int y = 0;
private Elevator elevator;
public ElevatorCanvas(Elevator elevator) {
@ -46,16 +45,7 @@ class ElevatorCanvas extends JPanel {
else {
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();
}
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;
floorHeight = 1. / nbFloors;
step = floorHeight / PRECISION;
System.out.println("step: " + step + ", floorHeight: " + floorHeight);
}
public void setActionListener(ElevatorListener listener) {
@ -41,7 +42,7 @@ public class Elevator {
direction = Direction.DOWN;
}
public void stopElevator() {
public void stop() {
direction = Direction.NONE;
}
@ -73,7 +74,7 @@ public class Elevator {
/* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
listener.floorChange(Direction.UP);
listener.floorChange(Direction.DOWN);
}
// /* Go halfway below. */
// if (!betweenFloors) currentFloor--;
@ -81,6 +82,9 @@ public class Elevator {
break;
case UP:
height += step;
System.out.println("height: " + height
+ ", prevFloor: " + prevFloor
+ ", floor: " + Math.floor(height / floorHeight));
/* Top reached. */
if (height > 1-step) {
@ -89,8 +93,8 @@ public class Elevator {
}
/* Passed a floor, invoke the listener. */
if (Math.floor(height / floorHeight) < prevFloor && listener != null) {
listener.floorChange(Direction.DOWN);
if (Math.floor(height / floorHeight) > prevFloor && listener != null) {
listener.floorChange(Direction.UP);
}
// /* Go halfway above. */
// if (betweenFloors) currentFloor++;

View File

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