-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElevatorDoor.java
151 lines (133 loc) · 3.16 KB
/
ElevatorDoor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class ElevatorDoor extends JPanel {
// Status of the door.
public static final int OPENING = 1;
public static final int CLOSING = -1;
public static final int OPEN = 2;
public static final int CLOSED = -2;
// Reference to the elevator.
private Elevator _elevator;
// Animation thread.
private ElevatorDoorAnimationThread _doorAnimation;
// Closing Rectangle.
private Rectangle _closingRectangle;
// Status.
private int _status = CLOSED; // Closed by default.
/**
* Constructor.
* @param elevator
*/
public ElevatorDoor(Elevator elevator){
// Base constructor.
super();
// Initialize.
_elevator = elevator;
// Initialize.
initialize();
}
/**
* Initialize.
*/
private void initialize(){
// Set the layout.
setLayout(new FlowLayout());
// Set size.
setSize(_elevator.getElevatorDef().width, 100);
// Set the color.
setBackground(_elevator.getElevatorDef().secondaryColor);
// Initialize the closing rectangle.
_closingRectangle = new Rectangle(0, 0, 0, _elevator.getBuilding().getBuildingDef().floorDef.height);
// Create animation thread.
_doorAnimation = new ElevatorDoorAnimationThread(this);
// Start the thread. Logic inside here takes care of the opening/closing/doing nothing.
_doorAnimation.start();
}
/**
* Paint the component.
*/
public void paintComponent(Graphics g){
// Draw the door (closing box).
g.setColor(Color.BLACK);
g.fillRect(_closingRectangle.x, _closingRectangle.y, _closingRectangle.width, _closingRectangle.height);
}
/**
* Called to start opening the door.
*/
public void open(){
// Only do if not already doing one.
if(_status == CLOSED){
// Set the status.
_status = OPENING;
}
}
/**
* Called to start closing the door.
*/
public void close(){
// Only do if not already doing one.
if(_status == OPEN){
// Set the status.
_status = CLOSING;
}
}
/**
* Update the closing rectangle.
* @param x
* @param y
* @param width
* @param height
*/
public void updateClosingRectangle(int x, int y, int width, int height){
// Update the rectangle.
_closingRectangle.setLocation(x, y);
_closingRectangle.setSize(width, height);
// Determine if we have finished opening or closing.
if(width == 0){
finishedClosing();
} else if(width == _elevator.getElevatorDef().width){
finishedOpening();
}
// Now repaint.
_elevator.getBuilding().repaint();
}
/**
* Called when the door has finished opening.
*/
private void finishedOpening(){
// Set status.
_status = OPEN;
}
/**
* Called when the door has finished closing.
*/
private void finishedClosing(){
// Set status.
_status = CLOSED;
}
/**
* Getter for the closing rectangle.
* @return
*/
public Rectangle getClosingRectangle(){
return _closingRectangle;
}
/**
* Getter for the status.
* @return
*/
public int getStatus(){
return _status;
}
/**
* Getter for the reference to the elevator.
* @return
*/
public Elevator getElevator(){
return _elevator;
}
}