commit aa0b3a6bb4f493024e94083e17946969cd330fd6 Author: cyril-colin Date: Wed Oct 2 22:08:43 2019 +0200 initial commit diff --git a/MANIFEST.MF b/MANIFEST.MF new file mode 100644 index 0000000..8de9d24 --- /dev/null +++ b/MANIFEST.MF @@ -0,0 +1 @@ +Main-Class: ElevatorApplication diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4024347 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +all: build assemble + +.PHONY: build +build: + mkdir -p build + javac -Xlint src/*.java -d build + +assemble: + cd build/; jar cvmf ../MANIFEST.MF ../Elevator.jar *.class + +clean: + -rm build/*.class diff --git a/src/Direction.java b/src/Direction.java new file mode 100644 index 0000000..7863c87 --- /dev/null +++ b/src/Direction.java @@ -0,0 +1,5 @@ +public enum Direction { + NONE, + UP, + DOWN, +} diff --git a/src/Elevator.java b/src/Elevator.java new file mode 100644 index 0000000..605e39d --- /dev/null +++ b/src/Elevator.java @@ -0,0 +1,70 @@ +public class Elevator { + private int currentFloor = 0; + private boolean betweenFloors = false; /* Between currentFloor and the one above it. */ + private Direction direction = Direction.NONE; + private boolean stoppingNextFloor = false; + private boolean emergency = false; + + public Elevator(int currentFloor) { + this.currentFloor = currentFloor; + } + + public void goUp() { + if (emergency) return; + stoppingNextFloor = false; + direction = Direction.UP; + } + + public void goDown() { + if (emergency) return; + stoppingNextFloor = false; + direction = Direction.DOWN; + } + + public void stopElevator() { + direction = Direction.NONE; + } + + public void stopNextFloor() { + stoppingNextFloor = true; + } + + public void emergencyStop() { + emergency = true; + direction = Direction.NONE; + } + + public void cancelEmergency() { + emergency = false; + } + + public void move() { + if (emergency) return; + switch (direction) { + case DOWN: + /* Go halfway below. */ + if (!betweenFloors) currentFloor--; + betweenFloors = !betweenFloors; + break; + case UP: + /* Go halfway above. */ + if (betweenFloors) currentFloor++; + betweenFloors = !betweenFloors; + break; + default: + break; + } + } + + public int getCurrentFloor() { + return currentFloor; + } + + public boolean getBetweenFloors() { + return betweenFloors; + } + + public Direction getDirection() { + return direction; + } +} diff --git a/src/ElevatorApplication.java b/src/ElevatorApplication.java new file mode 100644 index 0000000..f76a72f --- /dev/null +++ b/src/ElevatorApplication.java @@ -0,0 +1,53 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + + +public class ElevatorApplication implements ActionListener { + private ElevatorCanvas canvas; + + @Override + public void actionPerformed(ActionEvent e) { + canvas.setElevatorY((canvas.getElevatorY() + 1) % canvas.getHeight()); + } + + private void createAndShowGUI() { + JFrame frame = new JFrame(); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + Container pane = frame.getContentPane(); + pane.setLayout(new BoxLayout(pane, BoxLayout.LINE_AXIS)); + + pane.add(Box.createHorizontalGlue()); + + canvas = new ElevatorCanvas(); + canvas.setAlignmentY(Component.TOP_ALIGNMENT); + pane.add(canvas); + + FloorPanels fp = new FloorPanels(5); + fp.setAlignmentY(Component.TOP_ALIGNMENT); + pane.add(fp); + + pane.add(Box.createRigidArea(new Dimension(20, 0))); + + ElevatorPanel elevatorPanel = new ElevatorPanel(5); + elevatorPanel.setAlignmentY(Component.TOP_ALIGNMENT); + pane.add(elevatorPanel); + + pane.add(Box.createHorizontalGlue()); + + frame.pack(); + frame.setVisible(true); + } + + public static void main(String[] args) { + ElevatorApplication app = new ElevatorApplication(); + SwingUtilities.invokeLater(new Runnable() { + public void run() { + app.createAndShowGUI(); + Timer t = new Timer(16, app); + t.start(); + } + }); + } +} diff --git a/src/ElevatorCanvas.java b/src/ElevatorCanvas.java new file mode 100644 index 0000000..8ca7d48 --- /dev/null +++ b/src/ElevatorCanvas.java @@ -0,0 +1,41 @@ +import javax.swing.*; +import java.awt.*; + + +class ElevatorCanvas extends JPanel { + private final static int WIDTH = 40; + private final static int HEIGHT = 50; + private final static Color COLOR = Color.PINK; + private final static Dimension DIMENSIONS = new Dimension(WIDTH, 200); + + private int y = 0; + + @Override + public Dimension getMinimumSize() { + return DIMENSIONS; + } + @Override + public Dimension getPreferredSize() { + return DIMENSIONS; + } + @Override + public Dimension getMaximumSize() { + return DIMENSIONS; + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + g.setColor(COLOR); + g.fillRect(0, y, WIDTH, HEIGHT); + } + + public void setElevatorY(int y) { + this.y = y; + repaint(); + } + + public int getElevatorY() { + return y; + } +} diff --git a/src/ElevatorPanel.java b/src/ElevatorPanel.java new file mode 100644 index 0000000..7ef5bc4 --- /dev/null +++ b/src/ElevatorPanel.java @@ -0,0 +1,25 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + + +class ElevatorPanel extends JPanel { + private JButton emergencyStop = new JButton("Emergency stop."); + private JButton[] buttons; + + public ElevatorPanel(int nbFloors) { + this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); + this.add(new JLabel("Elevator panel.")); + + buttons = new JButton[nbFloors]; + for (int i = 0; i < nbFloors; i++) { + buttons[i] = new JButton("" + i); + this.add(buttons[i]); + } + add(emergencyStop); + } + + public void addEmergencyStopListener(ActionListener l) { + emergencyStop.addActionListener(l); + } +} diff --git a/src/FloorPanels.java b/src/FloorPanels.java new file mode 100644 index 0000000..b225e54 --- /dev/null +++ b/src/FloorPanels.java @@ -0,0 +1,24 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; + + +class FloorPanels extends JPanel { + private int nbFloors; + private JPanel[] floors; + + public FloorPanels(int nbFloors) { + this.nbFloors = nbFloors; + floors = new JPanel[nbFloors]; + this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); + this.add(new JLabel("Call.")); + for (int i = 0; i < nbFloors; i++) { + floors[i] = new JPanel(); + floors[i].setLayout(new BoxLayout(floors[i], BoxLayout.LINE_AXIS)); + floors[i].add(new JLabel("" + (i + 1))); + floors[i].add(new JButton("↑")); + floors[i].add(new JButton("↓")); + this.add(floors[i]); + } + } +}