1 package main;
2
3 import interfaces.*;
4
5 import javax.swing.*;
6 import java.awt.*;
7 import java.lang.*;
8 import java.util.*;
9 import java.awt.geom.*;
10
11
16 class SimpleDisplay extends SimDisplay
17 {
18 private SimWorld world;
19 private Graphics2D g2;
20 private BasicStroke stroke;
21 private Color color;
22
23
28 public SimpleDisplay(SimWorld s)
29 {
30 world=s;
32 this.repaint();
33 stroke=new BasicStroke(1.0f,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_BEVEL);
34 }
35
36
39 public void paintComponent(Graphics g)
40 {
41 super.paintComponent(g);
43
44 this.setBackground(Color.white);
45
46 g2=(Graphics2D)g;
48
49 LinkedList list=world.getObjectList();
51
52 for (int i=0;i<list.size();i++)
54 {
55 SimObject o=(SimObject)list.get(i);
57
58 Shape s=getShape(o);
60
61 s=rotateShape(s,o.getActualBearingXZ(),o.getXCoord(),o.getZCoord());
63
64
66 g2.setPaint(Color.black);
67
68 Shape outline=stroke.createStrokedShape(s);
69
70 g2.draw(outline);
71
72 g2.setPaint(color);
73
74 g2.fill(s);
75 }
76 }
77
78
85 public Shape getShape(SimObject o)
86 {
87 Shape s;
89
90 if(o.getType().equalsIgnoreCase("robot"))
92 {
93 s=createRobotShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength());
95 }
96 else if(o.getType().equalsIgnoreCase("light"))
97 {
98 s=createLightShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength());
100 }
101 else if(o.getType().equalsIgnoreCase("sensor"))
102 {
103 s=createSensorShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength());
105 }
106 else if (o.getType().equalsIgnoreCase("wall"))
107 {
108 s=createWallShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength());
110 }
111 else
112 {
113 s=createStandardShape(o.getXCoord(),o.getZCoord(),o.getWidth(),o.getLength());
115 }
116
117 return s;
119 }
120
121
130 private Shape rotateShape(Shape shape, double angle, double X, double Z)
131 {
132 double theta=Math.toRadians(angle);
134
135 AffineTransform atx = AffineTransform.getRotateInstance(theta,X,Z);
137
138 shape = atx.createTransformedShape(shape);
140
141 return shape;
143 }
144
145
155 private Shape createRobotShape(double x, double z, double width, double length)
156 {
157 double a=5.0;
159 double b=15.0;
160
161 double X=(x-(width/2));
163 double Z=(z-(length/2));
164
165 Rectangle2D.Double body=new Rectangle2D.Double(X,Z,width,length);
167
168 Rectangle2D.Double wheel1=new Rectangle2D.Double(X-a-1,Z+5,a,b);
169 Rectangle2D.Double wheel2=new Rectangle2D.Double(X-a-1,Z+40,a,b);
170 Rectangle2D.Double wheel3=new Rectangle2D.Double(X+width+1,Z+5,a,b);
171 Rectangle2D.Double wheel4=new Rectangle2D.Double(X+width+1,Z+40,a,b);
172
173 Area shape=new Area(body);
174 shape.add(new Area(wheel1));
175 shape.add(new Area(wheel2));
176 shape.add(new Area(wheel3));
177 shape.add(new Area(wheel4));
178
179 color=Color.lightGray;
180
181 return (Shape) shape;
183 }
184
185
188 private Shape createLightShape(double x, double z, double width, double length)
189 {
190 double X=(x-(width/2));
192 double Z=(z-(length/2));
193
194 color=Color.yellow;
195
196 return new Ellipse2D.Double(X,Z,width,length);
197 }
198
199
202 private Shape createSensorShape(double x, double z, double width, double length)
203 {
204 double X=(x-(width/2));
206 double Z=(z-(length/2));
207
208 color=Color.cyan;
209 color=color.darker();
210
211 return new Rectangle2D.Double(X,Z,width,length);
212 }
213
214
217 private Shape createWallShape(double x, double z, double width, double length)
218 {
219 double X=(x-(width/2));
221 double Z=(z-(length/2));
222
223 color=Color.lightGray;
224 color=color.darker();
225
226 return new Rectangle2D.Double(X,Z,width,length);
227 }
228
229
232 private Shape createStandardShape(double x, double z, double width, double length)
233 {
234 double X=(x-(width/2));
236 double Z=(z-(length/2));
237
238 color=Color.black;
239
240 return new Rectangle2D.Double(X,Z,width,length);
241 }
242 }
243