initial commit
This commit is contained in:
commit
aa0b3a6bb4
1
MANIFEST.MF
Normal file
1
MANIFEST.MF
Normal file
@ -0,0 +1 @@
|
||||
Main-Class: ElevatorApplication
|
12
Makefile
Normal file
12
Makefile
Normal file
@ -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
|
5
src/Direction.java
Normal file
5
src/Direction.java
Normal file
@ -0,0 +1,5 @@
|
||||
public enum Direction {
|
||||
NONE,
|
||||
UP,
|
||||
DOWN,
|
||||
}
|
70
src/Elevator.java
Normal file
70
src/Elevator.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
53
src/ElevatorApplication.java
Normal file
53
src/ElevatorApplication.java
Normal file
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
41
src/ElevatorCanvas.java
Normal file
41
src/ElevatorCanvas.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
25
src/ElevatorPanel.java
Normal file
25
src/ElevatorPanel.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
24
src/FloorPanels.java
Normal file
24
src/FloorPanels.java
Normal file
@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user