19
19
*/
20
20
public class Turtle
21
21
{
22
- /**
23
- * Current types are: ExplodedTurtle, Turtle, Spider
24
- */
25
- public enum Animals {
26
- ExplodedTurtle , Turtle , Spider , Unicorn
27
- }
28
- private class Turner implements Saver <Double >
29
- {
30
- @ Override
31
- public Double save (Double save ) throws SavingException
32
- {
33
- smallTurn (save );
34
- return save ;
35
- }
36
- }
37
- private class Mover implements Saver <Double >
38
- {
39
- private final Point starting ;
40
- private LineSegment line = null ;
41
- public Mover (Point point )
42
- {
43
- this .starting = point ;
44
- }
45
- @ Override
46
- public Double save (Double save ) throws SavingException
47
- {
48
- moveWithoutAnimation (save );
49
- if (line != null )
50
- {
51
- trail .remove (line );
52
- }
53
- line = new LineSegment (color , starting , new Point (getX (), getY ()), width );
54
- trail .add (line );
55
- return save ;
56
- }
57
- }
58
- private class EmptyMover implements Saver <Double >
59
- {
60
- @ Override
61
- public Double save (Double save ) throws SavingException
62
- {
63
- moveWithoutAnimation (save );
64
- return save ;
65
- }
66
- }
67
- private static final double MAX_MOVE_AMOUNT = 5.0 ;
68
22
public static final int TEST_SPEED = Integer .MIN_VALUE ;
23
+ private static final double MAX_MOVE_AMOUNT = 5.0 ;
69
24
private double x = 640 / 2 ;
70
25
private double y = 480 / 2 ;
71
26
private double angleInDegrees = 0 ;
@@ -77,7 +32,23 @@ public Double save(Double save) throws SavingException
77
32
private boolean penDown = true ;
78
33
private boolean hidden ;
79
34
private Animals animal ;
80
- //
35
+ public static double getDeltaY (double i , double angleInDegrees2 )
36
+ {
37
+ return -i * Math .cos (Math .toRadians (angleInDegrees2 ));
38
+ }
39
+ public static double getDeltaX (double i , double angleInDegrees2 )
40
+ {
41
+ return i * Math .sin (Math .toRadians (angleInDegrees2 ));
42
+ }
43
+ public static double angleCalculator (int x1 , int y1 , int x2 , int y2 )
44
+ {
45
+ int delta_x = x1 - x2 ;
46
+ int delta_y = y1 - y2 ;
47
+ double theta_radians = Math .atan2 (delta_y , delta_x );
48
+ double degrees = Math .toDegrees (theta_radians );
49
+ double degreesWith0North = degrees - 90 ;
50
+ return degreesWith0North ;
51
+ }
81
52
public BufferedImage getImage ()
82
53
{
83
54
BufferedImage image = ComponentApprovalWriter .drawComponent (getPanel ());
@@ -108,14 +79,26 @@ private Component getPanel()
108
79
}
109
80
return panel ;
110
81
}
82
+ public void setPanel (TurtlePanel panel )
83
+ {
84
+ this .panel = panel ;
85
+ }
111
86
public int getX ()
112
87
{
113
88
return (int ) x ;
114
89
}
90
+ public void setX (Number x )
91
+ {
92
+ this .x = x .doubleValue ();
93
+ }
115
94
public int getY ()
116
95
{
117
96
return (int ) y ;
118
97
}
98
+ public void setY (Number y )
99
+ {
100
+ this .y = y .doubleValue ();
101
+ }
119
102
public double getAngleInDegrees ()
120
103
{
121
104
return angleInDegrees ;
@@ -124,14 +107,6 @@ public void setAngleInDegrees(double angleInDegrees)
124
107
{
125
108
this .angleInDegrees = angleInDegrees ;
126
109
}
127
- public void setX (Number x )
128
- {
129
- this .x = x .doubleValue ();
130
- }
131
- public void setY (Number y )
132
- {
133
- this .y = y .doubleValue ();
134
- }
135
110
public void turn (double amount )
136
111
{
137
112
double max = getTurnAmount (amount );
@@ -187,6 +162,10 @@ private long getDelay()
187
162
if (getSpeed () == TEST_SPEED ) { return TEST_SPEED ; }
188
163
return 100 / getSpeed ();
189
164
}
165
+ public int getSpeed ()
166
+ {
167
+ return speed ;
168
+ }
190
169
public void setSpeed (int speed )
191
170
{
192
171
if (speed != TEST_SPEED )
@@ -199,10 +178,6 @@ public void setSpeed(int speed)
199
178
}
200
179
this .speed = speed ;
201
180
}
202
- public int getSpeed ()
203
- {
204
- return speed ;
205
- }
206
181
public double getHeadingInDegrees ()
207
182
{
208
183
return angleInDegrees ;
@@ -218,26 +193,18 @@ private void moveWithoutAnimation(Double save)
218
193
x += getDeltaX (save , angleInDegrees );
219
194
y += getDeltaY (save , angleInDegrees );
220
195
}
221
- public static double getDeltaY (double i , double angleInDegrees2 )
222
- {
223
- return -i * Math .cos (Math .toRadians (angleInDegrees2 ));
224
- }
225
- public static double getDeltaX (double i , double angleInDegrees2 )
226
- {
227
- return i * Math .sin (Math .toRadians (angleInDegrees2 ));
228
- }
229
196
public LineSegment [] getTrail ()
230
197
{
231
198
return trail .toArray (new LineSegment [trail .size ()]);
232
199
}
233
- public void setPenColor (Color color )
234
- {
235
- this .color = color ;
236
- }
237
200
public Color getPenColor ()
238
201
{
239
202
return color ;
240
203
}
204
+ public void setPenColor (Color color )
205
+ {
206
+ this .color = color ;
207
+ }
241
208
public int getPenWidth ()
242
209
{
243
210
return width ;
@@ -301,19 +268,6 @@ public void moveSynchronized(int x, int y)
301
268
double distance = new Point (x , y ).distance (getX (), getY ());
302
269
move (distance );
303
270
}
304
- public static double angleCalculator (int x1 , int y1 , int x2 , int y2 )
305
- {
306
- int delta_x = x1 - x2 ;
307
- int delta_y = y1 - y2 ;
308
- double theta_radians = Math .atan2 (delta_y , delta_x );
309
- double degrees = Math .toDegrees (theta_radians );
310
- double degreesWith0North = degrees - 90 ;
311
- return degreesWith0North ;
312
- }
313
- public void setPanel (TurtlePanel panel )
314
- {
315
- this .panel = panel ;
316
- }
317
271
public void drawStar (int size )
318
272
{
319
273
for (int i = 1 ; i <= 5 ; i ++)
@@ -326,4 +280,49 @@ public boolean isDead()
326
280
{
327
281
return this .animal == Animals .ExplodedTurtle ;
328
282
}
283
+ /**
284
+ * Current types are: ExplodedTurtle, Turtle, Spider
285
+ */
286
+ public enum Animals {
287
+ ExplodedTurtle , Turtle , Spider , Unicorn
288
+ }
289
+ private class Turner implements Saver <Double >
290
+ {
291
+ @ Override
292
+ public Double save (Double save ) throws SavingException
293
+ {
294
+ smallTurn (save );
295
+ return save ;
296
+ }
297
+ }
298
+ private class Mover implements Saver <Double >
299
+ {
300
+ private final Point starting ;
301
+ private LineSegment line = null ;
302
+ public Mover (Point point )
303
+ {
304
+ this .starting = point ;
305
+ }
306
+ @ Override
307
+ public Double save (Double save ) throws SavingException
308
+ {
309
+ moveWithoutAnimation (save );
310
+ if (line != null )
311
+ {
312
+ trail .remove (line );
313
+ }
314
+ line = new LineSegment (color , starting , new Point (getX (), getY ()), width );
315
+ trail .add (line );
316
+ return save ;
317
+ }
318
+ }
319
+ private class EmptyMover implements Saver <Double >
320
+ {
321
+ @ Override
322
+ public Double save (Double save ) throws SavingException
323
+ {
324
+ moveWithoutAnimation (save );
325
+ return save ;
326
+ }
327
+ }
329
328
}
0 commit comments