-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeworkOne.java
More file actions
343 lines (274 loc) · 11.5 KB
/
HomeworkOne.java
File metadata and controls
343 lines (274 loc) · 11.5 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package HomeworkOne;
/*
* Name : Aziz Alqulaysh
* Depaul# : 1805201
* Class : SE 450
* Homework : #5
* Problem : HomeworkOne
* Due Date : 09/19/2016
*
* class HomeworkOne
*
*/
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
//ActionEvents
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
//for various J'Controls'
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class HomeworkOne extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;//I added this to remove the warning the Eclipse generates
private Container content;//used for the content pane
private JPanel movePanel;//l,u,r,d buttons
private JButton upButton; //up button
private JButton leftButton; //let button
private JButton rightButton; //right button
private JButton downButton; //down button
private JTextArea shapeOutput; //the output for the shape data
private ArrayList<IShape> shapes = new ArrayList<IShape>();
public HomeworkOne()
{
setSize(400, 600);//set the size of the window to 300 x 300
setTitle("MyHomeworkOne");//set the applications title
//Now create a panel to hold the move combo box and move description
movePanel = new JPanel();
//create the move buttons
upButton = new JButton("Up");
leftButton = new JButton("Left");
rightButton = new JButton("Right");
downButton = new JButton("Down");
upButton.setPreferredSize(new Dimension(100, 50));
leftButton.setPreferredSize(new Dimension(100, 50));
rightButton.setPreferredSize(new Dimension(100, 50));
downButton.setPreferredSize(new Dimension(100, 50));
//the spring layout will handle the move panels controls spacing
//when the window is initialized and sized/resized
SpringLayout movePanelLayout = new SpringLayout();
//now lets set up the controls and how they will be position
//in accordance to one another
//up button is 5 below panel
movePanelLayout.putConstraint(SpringLayout.NORTH, upButton, 5,
SpringLayout.NORTH, movePanel);
//up button top adjust horizontally
movePanelLayout.putConstraint(SpringLayout.WEST, upButton, 115,
SpringLayout.WEST, movePanel);
//down buttons left side with up button left side
movePanelLayout.putConstraint(SpringLayout.WEST, downButton, 0,
SpringLayout.WEST, upButton);
//left button top side 5 away from up button bottom side
movePanelLayout.putConstraint(SpringLayout.NORTH, leftButton, -20,
SpringLayout.SOUTH, upButton);
//right button top side aligned with left button top side
movePanelLayout.putConstraint(SpringLayout.NORTH, rightButton, 0,
SpringLayout.NORTH, leftButton);
//left button left side 5 away from panel left side
movePanelLayout.putConstraint(SpringLayout.WEST, leftButton, 5,
SpringLayout.WEST, movePanel);
//left button top side 5 away from up button bottom side
movePanelLayout.putConstraint(SpringLayout.NORTH, downButton, 10,
SpringLayout.SOUTH, upButton);
//left button top side 5 away from up button bottom side
movePanelLayout.putConstraint(SpringLayout.SOUTH, movePanel, 5,
SpringLayout.SOUTH, downButton);
//fix move boxes east border -5 pixels away from move texts west border
movePanelLayout.putConstraint(SpringLayout.EAST, rightButton, -5,
SpringLayout.EAST, movePanel);
//the panel dependencies has been configured so here we set it
movePanel.setLayout(movePanelLayout);
//add the buttons to the panel
movePanel.add(leftButton);
movePanel.add(rightButton);
movePanel.add(upButton);
movePanel.add(downButton);
//create the text area for displaying the shape(s) location
shapeOutput = new JTextArea("");
//make it read only
shapeOutput.setEditable(false);
//lets make the read only text a normal black
shapeOutput.setDisabledTextColor(Color.BLACK);
//the output could be lengthy so we'll need a scroll bar
JScrollPane scrollPane = new JScrollPane(shapeOutput);
//give the panel an nice etched and titled border with the heading of "Move"
movePanel.setBorder(new TitledBorder(new EtchedBorder(), "Move"));
//get the content pane, as it is were we'll place our controls
content = getContentPane();
//Attach the move panel to the content pane
content.add(movePanel);
//Attach the scroll pane button to the content pane
//the scroll pane wrap the move panel, so it will
//by default include it as well
content.add( scrollPane );
//lets create a final spring layout to include the other layout
//and add the rest of the controls
SpringLayout contentLayout = new SpringLayout();
//move panel setup
//set move panels west border 30 pixels away from content panes west border
contentLayout.putConstraint(SpringLayout.WEST, movePanel, 20,
SpringLayout.WEST, content);
//fix move panels east border -30 pixels away from content panes east border
contentLayout.putConstraint(SpringLayout.EAST, movePanel, -20,
SpringLayout.EAST, content);
//fix move panels north border 10 pixels away from content panes north border
contentLayout.putConstraint(SpringLayout.NORTH, content, 10,
SpringLayout.SOUTH, movePanel);
//fix scroll panes north border 10 pixels away from down buttons south border
contentLayout.putConstraint(SpringLayout.NORTH, scrollPane, 100,
SpringLayout.SOUTH, downButton);
//fix scroll panes south border -10 pixels away from content panes south border
contentLayout.putConstraint(SpringLayout.SOUTH, scrollPane, -10,
SpringLayout.SOUTH, content);
//fix scroll panes west border 30 pixels away from content panes west border
contentLayout.putConstraint(SpringLayout.WEST, scrollPane, 30,
SpringLayout.WEST, content);
//fix scroll panes east border -30 pixels away from content panes east border
contentLayout.putConstraint(SpringLayout.EAST, scrollPane, -30,
SpringLayout.EAST, content);
//set the content panes layout
content.setLayout( contentLayout );
//add button click listener to up, left, right, down
upButton.addActionListener(this);
leftButton.addActionListener(this);
rightButton.addActionListener(this);
//downButton.addActionListener(this);
downButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
doUpdate(0.0f, 1.0f);
//shapeOutput.setText("downButton pressed in Anonymous Class");
}
});
}
//the main entry point for the application
public static void main(String[] args) throws BadShapeException
{
//create a new homework application
HomeworkOne MyHomeworkOneframe = new HomeworkOne();
//set the app to exit when the user presses the close button
MyHomeworkOneframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set the app to be visible
MyHomeworkOneframe.setVisible(true);
try
{
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("/Users/SINNER/Desktop/SE450/workspace/SE450work/src/HomeworkOne/shapes.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonArray = (JSONArray) jsonObject.get("shapes");
ArrayList<IShape> shapes = new ArrayList<IShape>();
@SuppressWarnings("unchecked")
Iterator<JSONObject> jsonIterator = jsonArray.iterator();
while (jsonIterator.hasNext())
{
JSONObject someShape = jsonIterator.next();
if (someShape.containsKey("type"))
{
Object value = someShape.get("type");
if (value.equals("Circle"))
{
MyHomeworkOneframe.shapeOutput.append("I found a circle\n");
Circle circle = new Circle(MyHomeworkOneframe.extract(someShape.get("left")),
MyHomeworkOneframe.extract(someShape.get("top")),
MyHomeworkOneframe.extract(someShape.get("right")),
MyHomeworkOneframe.extract(someShape.get("bottom")));
shapes.add(circle);
}
else if (value.equals("Square"))
{
MyHomeworkOneframe.shapeOutput.append("I found a square\n");
Square square = new Square(MyHomeworkOneframe.extract(someShape.get("left")),
MyHomeworkOneframe.extract(someShape.get("top")),
MyHomeworkOneframe.extract(someShape.get("right")),
MyHomeworkOneframe.extract(someShape.get("bottom")));
shapes.add(square);
}
else if (value.equals("Line"))
{
MyHomeworkOneframe.shapeOutput.append("I found a line\n");
Line line = new Line(MyHomeworkOneframe.extract(someShape.get("left")),
MyHomeworkOneframe.extract(someShape.get("top")),
MyHomeworkOneframe.extract(someShape.get("right")),
MyHomeworkOneframe.extract(someShape.get("bottom")));
shapes.add(line);
}
else
{
//throw new BadShapeException();
//use your exception class and maybe throw exception about bad data?
}
}
MyHomeworkOneframe.setShapes(shapes);
}
}
catch(FileNotFoundException eFileNotFound)
{
}
catch(IOException eIOException)
{
}
catch(ParseException eParseException)
{
}
}
public void setShapes(final ArrayList<IShape> newShapes)
{
shapes = newShapes;
}
// Inner class for the listener which responds to the move button press.
public void actionPerformed(ActionEvent e)
{
//get the action object and test to see where it came from
Object sourceObject = e.getSource();
//see which button was pressed
if (sourceObject == leftButton)
{
doUpdate(-1.0f, 0.0f);
}
else if (sourceObject == upButton)
{
doUpdate(0.0f, -1.0f);
}
else if (sourceObject == rightButton)
{
doUpdate(1.0f, 0.0f);
}
else if (sourceObject == downButton)
{
//doUpdate(0.0f, 1.0f);
shapeOutput.setText("downButton pressed in ActionPerformed");
}
}
public void doUpdate(float x, float y)
{
shapeOutput.setText("");
Iterator<IShape> iiShapes = shapes.iterator();
while (iiShapes.hasNext())
{
IShape iShape = iiShapes.next();
iShape.move(x, y);
shapeOutput.append(iShape.print());
}
}
final Float extract(final Object object)
{
return Float.parseFloat(object.toString());
}
}