1919 */
2020public class Turtle
2121{
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 ;
6822 public static final int TEST_SPEED = Integer .MIN_VALUE ;
23+ private static final double MAX_MOVE_AMOUNT = 5.0 ;
6924 private double x = 640 / 2 ;
7025 private double y = 480 / 2 ;
7126 private double angleInDegrees = 0 ;
@@ -77,7 +32,23 @@ public Double save(Double save) throws SavingException
7732 private boolean penDown = true ;
7833 private boolean hidden ;
7934 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+ }
8152 public BufferedImage getImage ()
8253 {
8354 BufferedImage image = ComponentApprovalWriter .drawComponent (getPanel ());
@@ -108,14 +79,26 @@ private Component getPanel()
10879 }
10980 return panel ;
11081 }
82+ public void setPanel (TurtlePanel panel )
83+ {
84+ this .panel = panel ;
85+ }
11186 public int getX ()
11287 {
11388 return (int ) x ;
11489 }
90+ public void setX (Number x )
91+ {
92+ this .x = x .doubleValue ();
93+ }
11594 public int getY ()
11695 {
11796 return (int ) y ;
11897 }
98+ public void setY (Number y )
99+ {
100+ this .y = y .doubleValue ();
101+ }
119102 public double getAngleInDegrees ()
120103 {
121104 return angleInDegrees ;
@@ -124,14 +107,6 @@ public void setAngleInDegrees(double angleInDegrees)
124107 {
125108 this .angleInDegrees = angleInDegrees ;
126109 }
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- }
135110 public void turn (double amount )
136111 {
137112 double max = getTurnAmount (amount );
@@ -187,6 +162,10 @@ private long getDelay()
187162 if (getSpeed () == TEST_SPEED ) { return TEST_SPEED ; }
188163 return 100 / getSpeed ();
189164 }
165+ public int getSpeed ()
166+ {
167+ return speed ;
168+ }
190169 public void setSpeed (int speed )
191170 {
192171 if (speed != TEST_SPEED )
@@ -199,10 +178,6 @@ public void setSpeed(int speed)
199178 }
200179 this .speed = speed ;
201180 }
202- public int getSpeed ()
203- {
204- return speed ;
205- }
206181 public double getHeadingInDegrees ()
207182 {
208183 return angleInDegrees ;
@@ -218,26 +193,18 @@ private void moveWithoutAnimation(Double save)
218193 x += getDeltaX (save , angleInDegrees );
219194 y += getDeltaY (save , angleInDegrees );
220195 }
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- }
229196 public LineSegment [] getTrail ()
230197 {
231198 return trail .toArray (new LineSegment [trail .size ()]);
232199 }
233- public void setPenColor (Color color )
234- {
235- this .color = color ;
236- }
237200 public Color getPenColor ()
238201 {
239202 return color ;
240203 }
204+ public void setPenColor (Color color )
205+ {
206+ this .color = color ;
207+ }
241208 public int getPenWidth ()
242209 {
243210 return width ;
@@ -301,19 +268,6 @@ public void moveSynchronized(int x, int y)
301268 double distance = new Point (x , y ).distance (getX (), getY ());
302269 move (distance );
303270 }
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- }
317271 public void drawStar (int size )
318272 {
319273 for (int i = 1 ; i <= 5 ; i ++)
@@ -326,4 +280,49 @@ public boolean isDead()
326280 {
327281 return this .animal == Animals .ExplodedTurtle ;
328282 }
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+ }
329328}
0 commit comments