make ElevatorCanvas reflect the state of the Elevator

This commit is contained in:
cyril-colin 2019-10-14 08:45:57 +02:00
parent 4159a1fbf8
commit 26d9057e85
2 changed files with 36 additions and 3 deletions

View File

@ -1,11 +1,25 @@
package gui; package gui;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import Requests.CallFromElevatorRequest;
import Requests.CallFromFloorRequest;
import simulation.Elevator;
import simulation.Simulation;
public class ElevatorApplication implements ActionListener { public class ElevatorApplication implements ActionListener {
private ElevatorCanvas canvas; private ElevatorCanvas canvas;
private Simulation simulation;
private Elevator elevator;
public ElevatorApplication() {
elevator = new Elevator(0, 5);
simulation = new Simulation(elevator);
elevator.setActionListener(simulation);
}
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
@ -21,7 +35,7 @@ public class ElevatorApplication implements ActionListener {
pane.add(Box.createHorizontalGlue()); pane.add(Box.createHorizontalGlue());
canvas = new ElevatorCanvas(); canvas = new ElevatorCanvas(elevator);
canvas.setAlignmentY(Component.TOP_ALIGNMENT); canvas.setAlignmentY(Component.TOP_ALIGNMENT);
pane.add(canvas); pane.add(canvas);
@ -43,6 +57,7 @@ public class ElevatorApplication implements ActionListener {
public static void main(String[] args) { public static void main(String[] args) {
ElevatorApplication app = new ElevatorApplication(); ElevatorApplication app = new ElevatorApplication();
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
app.createAndShowGUI(); app.createAndShowGUI();

View File

@ -1,15 +1,22 @@
package gui; package gui;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import simulation.Elevator;
import commandSystem.Direction;
@SuppressWarnings("serial")
class ElevatorCanvas extends JPanel { class ElevatorCanvas extends JPanel {
private final static int WIDTH = 40; private final static int WIDTH = 40;
private final static int HEIGHT = 50; private final static int HEIGHT = 50;
private final static Color COLOR = Color.PINK;
private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200); private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200);
private int y = 0; private int y = 0;
private Elevator elevator;
public ElevatorCanvas(Elevator elevator) {
this.elevator = elevator;
}
@Override @Override
public Dimension getMinimumSize() { public Dimension getMinimumSize() {
@ -27,7 +34,18 @@ class ElevatorCanvas extends JPanel {
@Override @Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
g.setColor(COLOR); if (elevator.getStoppingNextFloor()) {
g.setColor(Color.BLUE);
}
else if (elevator.getDirection() == Direction.NONE) {
g.setColor(Color.GRAY);
}
else if (elevator.getEmergency()) {
g.setColor(Color.RED);
}
else {
g.setColor(Color.BLACK);
}
g.fillRect(0, y, WIDTH, HEIGHT); g.fillRect(0, y, WIDTH, HEIGHT);
} }