A hotel is expanding and wants to install an elevator system in its building. You have been asked to create a simulation of this new elevator system. Create an application which simulates the buttons in the elevator and displays a simple picture of the elevator (a black rectangle) moving to the correct floor when a JButton is pressed (Fig. 20.34).
a) Copying the template to your working directory. Copy the C:Examples Tutorial20ExercisesElevator directory to your C:SimplyJava directory.
b) Opening the DrawJPanel template file. Open the template file DrawJPanel.java in your text editor.
c) Creating a new MyRectangle object. Inside the DrawJPanel no-argument construc- tor, create your new MyRectangle object buildingElevator (at line 47) after the creation of delayTimer. The template already declares buildingElevator as an instance variable. When calling the MyRectangle constructor, the x and y variables should be the first two parameters and be set to 57 and 400 respectively. The height, width and color variables should be set to 36, 50 and Color.BLACK and are the third, fourth and fifth parameters, respectively.
d) Adding code to the moveEelevator method. Find the moveElevator method, which immediately follows moveElevatorToFloor. Inside the moveElevator method, add an if…else statement that tests if currentPosition is
```
1 // Elevator.java
2 // Application simulates an elevator.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class Elevator extends JFrame
8 {
9 // JLabel for elevator JButtons
10 private JLabel selectFloorJLabel;
11
12 // JButtons to select the floor
13 private JButton oneJButton;
14 private JButton twoJButton;
15 private JButton threeJButton;
16 private JButton fourJButton;
17 private JButton fiveJButton;
18 private JButton sixJButton;
19 private JButton sevenJButton;
20 private JButton eightJButton;
21 private JButton nineJButton;
22
23 // JTextArea for displaying elevator location
24 public JTextArea displayJTextArea;
25
26 // DrawJPanel for displaying rectangles
27 private DrawJPanel drawingJPanel;
28
29 // JLabel for displaying floor sign
30 private JLabel floorJLabel;
31
32 // JLabels for displaying the floor
33 private JLabel oneJLabel;
34 private JLabel twoJLabel;
35 private JLabel threeJLabel;
36 private JLabel fourJLabel;
37 private JLabel fiveJLabel;
38 private JLabel sixJLabel;
39 private JLabel sevenJLabel;
40 private JLabel eightJLabel;
41 private JLabel nineJLabel;
42
43 // no-argument constructor
44 public Elevator()
45 {
46 createUserInterface();
47 }
48
49 // create and position GUI components; register event handlers
50 private void createUserInterface()
51 {
52 // get content pane for attaching GUI components
53 Container contentPane = getContentPane();
54
55 // enable explicit positioning of GUI components
56 contentPane.setLayout( null );
57
58 // set up selectFloorJLabel
59 selectFloorJLabel = new JLabel();
60 selectFloorJLabel.setBounds( 15, 65, 100, 25 );
61 selectFloorJLabel.setText( "Select Floor:" );
62 contentPane.add( selectFloorJLabel );
63
64 // set up oneJButton
65 oneJButton = new JButton();
66 oneJButton.setBounds( 15, 100, 41, 41 );
67 oneJButton.setText( "1" );
68 contentPane.add( oneJButton );
69 oneJButton.addActionListener(
70
71 new ActionListener() // anonymous inner class
72 {
73 // event handler called when oneJButton is pressed
74 public void actionPerformed( ActionEvent event )
75 {
76 oneJButtonActionPerformed( event );
77 }
78
79 } // end anonymous inner class
80
81 ); // end call to addActionListener
82
83 // set up twoJButton
84 twoJButton = new JButton();
85 twoJButton.setBounds( 75, 100, 41, 41 );
86 twoJButton.setText( "2" );
87 contentPane.add( twoJButton );
88 twoJButton.addActionListener(
89
90 new ActionListener() // anonymous inner class
91 {
92 // event handler called when twoJButton is pressed
93 public void actionPerformed( ActionEvent event )
94 {
95 twoJButtonActionPerformed( event );
96 }
97
98 } // end anonymous inner class
99
100 ); // end call to addActionListener
101
102 // set up threeJButton
103 threeJButton = new JButton();
104 threeJButton.setBounds( 135, 100, 41, 41 );
105 threeJButton.setText( "3" );
106 contentPane.add( threeJButton );
107 threeJButton.addActionListener(
108
109 new ActionListener() // anonymous inner class
110 {
111 // event handler called when threeJButton is pressed
112 public void actionPerformed( ActionEvent event )
113 {
114 threeJButtonActionPerformed( event );
115 }
116
117 } // end anonymous inner class
118
119 ); // end call to addActionListener
120
121 // set up fourJButton
122 fourJButton = new JButton();
123 fourJButton.setBounds( 15, 160, 41, 41 );
124 fourJButton.setText( "4" );
125 contentPane.add( fourJButton );
126 fourJButton.addActionListener(
127
128 new ActionListener() // anonymous inner class
129 {
130 // event handler called when fourJButton is pressed
131 public void actionPerformed( ActionEvent event )
132 {
133 fourJButtonActionPerformed( event );
134 }
135
136 } // end anonymous inner class
137
138 ); // end call to addActionListener
139
140 // set up fiveJButton
141 fiveJButton = new JButton();
142 fiveJButton.setBounds( 75, 160, 41, 41 );
143 fiveJButton.setText( "5" );
144 contentPane.add( fiveJButton );
145 fiveJButton.addActionListener(
146
147 new ActionListener() // anonymous inner class
148 {
149 // event handler called when fiveJButton is pressed
150 public void actionPerformed( ActionEvent event )
151 {
152 fiveJButtonActionPerformed( event );
153 }
154
155 } // end anonymous inner class
156
157 ); // end call to addActionListener
158
159 // set up sixJButton
160 sixJButton = new JButton();
161 sixJButton.setBounds( 135, 160, 41, 41 );
162 sixJButton.setText( "6" );
163 contentPane.add( sixJButton );
164 sixJButton.addActionListener(
165
166 new ActionListener() // anonymous inner class
167 {
168 // event handler called when sixJButton is pressed
169 public void actionPerformed( ActionEvent event )
170 {
171 sixJButtonActionPerformed( event );
172 }
173
174 } // end anonymous inner class
175
176 ); // end call to addActionListener
177
178 // set up sevenJButton
179 sevenJButton = new JButton();
180 sevenJButton.setBounds( 15, 220, 41, 41 );
181 sevenJButton.setText( "7" );
182 contentPane.add( sevenJButton );
183 sevenJButton.addActionListener(
184
185 new ActionListener() // anonymous inner class
186 {
187 // event handler called when sevenJButton is pressed
188 public void actionPerformed( ActionEvent event )
189 {
190 sevenJButtonActionPerformed( event );
191 }
192
193 } // end anonymous inner class
194
195 ); // end call to addActionListener
196
197 // set up eightJButton
198 eightJButton = new JButton();
199 eightJButton.setBounds( 75, 220, 41, 41 );
200 eightJButton.setText( "8" );
201 contentPane.add( eightJButton );
202 eightJButton.addActionListener(
203
204 new ActionListener() // anonymous inner class
205 {
206 // event handler called when eightJButton is pressed
207 public void actionPerformed( ActionEvent event )
208 {
209 eightJButtonActionPerformed( event );
210 }
211
212 } // end anonymous inner class
213
214 ); // end call to addActionListener
215
216 // set up nineJButton
217 nineJButton = new JButton();
218 nineJButton.setBounds( 135, 220, 41, 41 );
219 nineJButton.setText( "9" );
220 contentPane.add( nineJButton );
221 nineJButton.addActionListener(
222
223 new ActionListener() // anonymous inner class
224 {
225 // event handler called when nineJButton is pressed
226 public void actionPerformed( ActionEvent event )
227 {
228 nineJButtonActionPerformed( event );
229 }
230
231 } // end anonymous inner class
232
233 ); // end call to addActionListener
234
235 // set up displayJTextArea
236 displayJTextArea = new JTextArea();
237 displayJTextArea.setBounds( 16, 290, 170, 20 );
238 displayJTextArea.setEditable( false );
239 displayJTextArea.setForeground( Color.RED );
240 contentPane.add( displayJTextArea );
241
242 // set up drawingJPanel
243 drawingJPanel = new DrawJPanel();
244 drawingJPanel.setBounds( 215, 50, 150, 450 );
245 drawingJPanel.setBackground( Color.WHITE );
246 contentPane.add( drawingJPanel );
247
248 // set up floorJLabel
249 floorJLabel = new JLabel();
250 floorJLabel.setBounds( 375, 16, 50, 25 );
251 floorJLabel.setText( "Floor:" );
252 contentPane.add( floorJLabel );
253
254 // set up oneJLabel
255 oneJLabel = new JLabel();
256 oneJLabel.setBounds( 385, 460, 25, 25 );
257 oneJLabel.setText( "1" );
258 contentPane.add( oneJLabel );
259
260 // set up twoJLabel
261 twoJLabel = new JLabel();
262 twoJLabel.setBounds( 385, 410, 25, 25 );
263 twoJLabel.setText( "2" );
264 contentPane.add( twoJLabel );
265
266 // set up t
You might also like to view...
What is the process for publishing changed content to Adobe hosting?
What will be an ideal response?
With ______, a service provider organization owns and manages the hardware, software, networking, and storage devices, with cloud user organizations (called tenants) accessing slices of shared resources via the Internet. ?
Fill in the blank(s) with the appropriate word(s).
In the accompanying figure, slide 3 has only 1 content placeholder.
Answer the following statement true (T) or false (F)
The space between the text and the top, bottom, left, and right edges of the paper.
What will be an ideal response?