-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTurtle.java
2656 lines (2494 loc) · 86.5 KB
/
Turtle.java
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**AUTHOR: NICHOLAS SEWARD
* EMAIL: [email protected]
* LICENSE: MIT (USE THIS HOWEVER YOU SEE FIT.)
* DATE: 6/21/2012
* VERSION: 2 (THIS SHOULD BE VERSION 0.1 BUT THAT IS A SILLY NUMBERING SYSTEM.)
*
*
* THIS SOFTWARE HAS NO WARRANTY. IF IT WORKS, SUPER. IF IT DOESN'T, LET ME
* KNOW AND I MIGHT OR MIGHT NOT DO SOMETHING ABOUT IT.
*
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
*/
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;
import java.lang.reflect.Method;
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
* Turtle is a selfcontained class that will allow students to make
* beautiful turtle graphics with ease.
*
* @author Nicholas Seward
*/
@SuppressWarnings("unchecked")
public class Turtle implements Runnable, ActionListener, MouseListener, MouseMotionListener, KeyListener, ComponentListener, MouseWheelListener
{
private static ArrayList<Turtle> turtles;
private static TreeMap<Long,ArrayList> turtleStates;
private static TreeMap<Long,ArrayList> redoStates;
private static JFrame window;
private static JApplet applet;
private static JLabel draw;
private static int width, height;
private static BufferedImage offscreenImage, midscreenImage, onscreenImage;
private static Graphics2D offscreen, midscreen, onscreen;
private static BufferedImage backgroundImage;
private static Color backgroundColor;
private static ImageIcon icon;
private static Turtle turtle;
private static HashMap<String,Polygon> shapes; //You can only add. Never remove.
private static HashMap<String, Color> colors;
private static HashMap<String,ArrayList<ArrayList>> keyBindings;
private static HashMap<Turtle,ArrayList<ArrayList>> mouseBindings;
private static double centerX, centerY;
private static double scale;
private static TreeSet<String> keysDown;
private static TreeSet<String> processedKeys;
private static TreeSet<String> unprocessedKeys;
private static long lastUpdate;
private static long fps;
private static final Object turtleLock=new Object();
private static int dragx=0,dragy=0,x=0,y=0,modifiers=0;
private static final Object keyLock = new Object();
private static final int REFRESH_MODE_ANIMATED=0;
private static final int REFRESH_MODE_STATE_CHANGE=1;
private static final int REFRESH_MODE_ON_DEMAND=2;
private static int refreshMode;
private static final int BACKGROUND_MODE_STRETCH=0;
private static final int BACKGROUND_MODE_CENTER=1;
private static final int BACKGROUND_MODE_TILE=2;
private static final int BACKGROUND_MODE_CENTER_RELATIVE=3;
private static final int BACKGROUND_MODE_TILE_RELATIVE=4;
private static int backgroundMode;
private static Turtle selectedTurtle;
private static boolean running;
static { init(); }
/**
* This is an internal method that should never be called.
*/
public void run()
{
if(Thread.currentThread().getName().equals("render")) renderLoop();
else if(Thread.currentThread().getName().equals("listen")) eventLoop();
}
private void eventLoop()
{
//System.out.println("EVENT LOOP STARTED");
long time=0;
while(running)
{
time=System.nanoTime();
processKeys();
waitUntil(time+1000000000/fps);
}
}
private void renderLoop()
{
//System.out.println("RENDER LOOP STARTED");
long time=0;
while(running)
{
time=System.nanoTime();
//System.out.println(time);
if(refreshMode==REFRESH_MODE_ANIMATED) draw();
if (!waitUntil(time+1000000000/fps)) fps--;
else if (fps<30) fps++;
}
}
private static boolean waitUntil(Long time)
{
long now=System.nanoTime();
if (now<time)
{
try {Thread.sleep((time - now) / 1000000);}
catch(Exception e) {}
return true;
}
else return false;
}
private static void init()
{
//mouseBindings.put(null, new ArrayList<ArrayList>());
turtles=new ArrayList<Turtle>();
turtleStates=new TreeMap<Long,ArrayList>();
redoStates=new TreeMap<Long,ArrayList>();
width=500;
height=500;
backgroundColor=Color.WHITE;
keyBindings=new HashMap<String,ArrayList<ArrayList>>();
mouseBindings=new HashMap<Turtle,ArrayList<ArrayList>>();
centerX=0;
centerY=0;
scale=1;
keysDown=new TreeSet<String>();
processedKeys=new TreeSet<String>();
unprocessedKeys=new TreeSet<String>();
lastUpdate=0;
fps=30;
dragx=0;
dragy=0;
x=0;
y=0;
modifiers=0;
refreshMode=REFRESH_MODE_ANIMATED;
backgroundMode=BACKGROUND_MODE_TILE_RELATIVE;
selectedTurtle=null;
running=true;
window = new JFrame("Turtle");
icon = new ImageIcon();
setupBuffering();
draw = new JLabel(icon);
window.setContentPane(draw);
//window.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
try{window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}
catch(Exception e){}
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem menuItem1 = new JMenuItem("Save...");
menuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
menu.add(menuItem1);
window.setJMenuBar(menuBar);
window.pack();
window.requestFocusInWindow();
drawTurtleIcon();
window.setVisible(true);
makeShapes();
turtle=new Turtle(0);
draw.setFocusable(true);
menuItem1.addActionListener(turtle);
window.addComponentListener(turtle);
draw.addComponentListener(turtle);
draw.addMouseListener(turtle);
draw.addMouseMotionListener(turtle);
draw.addMouseWheelListener(turtle);
window.addKeyListener(turtle);
draw.addKeyListener(turtle);
draw.requestFocus();
initColors();
// GraphicsEnvironment ge;
// ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
// String[] fontNames = ge.getAvailableFontFamilyNames();
// System.out.println(Arrays.toString(fontNames));
(new Thread(turtle,"render")).start();
(new Thread(turtle,"listen")).start();
}
public static void exit()
{
running=false;
window.setVisible(false);
window.dispose();
}
/**
* This is an experimental method that should allow you to make turtle
* applets in the future. For now, it doesn't work because the key and
* mouse bindings require reflection and applets think that allowing
* reflection would open a security hole. Theoretically in the init method
* of the applet you need to place <code>Turtle.startApplet(this);</code>.
* <b>This is not currently working.</b>
*
* @param applet
*/
public static void startApplet(JApplet applet)
{
//turtleStates.clear();
//turtles.clear();
//init();
Turtle.applet=applet;
applet.setContentPane(window.getContentPane());
window.setVisible(false);
try{(new Thread((Runnable)applet,"turtle")).start();}
catch(Exception e)
{
e.printStackTrace();
}
}
private static void initColors()
{
colors = new HashMap<String, Color>();
colors.put("aliceblue", new Color(0xf0f8ff));
colors.put("antiquewhite", new Color(0xfaebd7));
colors.put("aqua", new Color(0x00ffff));
colors.put("aquamarine", new Color(0x7fffd4));
colors.put("azure", new Color(0xf0ffff));
colors.put("beige", new Color(0xf5f5dc));
colors.put("bisque", new Color(0xffe4c4));
colors.put("black", new Color(0x000000));
colors.put("blanchedalmond", new Color(0xffebcd));
colors.put("blue", new Color(0x0000ff));
colors.put("blueviolet", new Color(0x8a2be2));
colors.put("brown", new Color(0xa52a2a));
colors.put("burlywood", new Color(0xdeb887));
colors.put("cadetblue", new Color(0x5f9ea0));
colors.put("chartreuse", new Color(0x7fff00));
colors.put("chocolate", new Color(0xd2691e));
colors.put("coral", new Color(0xff7f50));
colors.put("cornflowerblue", new Color(0x6495ed));
colors.put("cornsilk", new Color(0xfff8dc));
colors.put("crimson", new Color(0xdc143c));
colors.put("cyan", new Color(0x00ffff));
colors.put("darkblue", new Color(0x00008b));
colors.put("darkcyan", new Color(0x008b8b));
colors.put("darkgoldenrod", new Color(0xb8860b));
colors.put("darkgray", new Color(0xa9a9a9));
colors.put("darkgreen", new Color(0x006400));
colors.put("darkkhaki", new Color(0xbdb76b));
colors.put("darkmagenta", new Color(0x8b008b));
colors.put("darkolivegreen", new Color(0x556b2f));
colors.put("darkorange", new Color(0xff8c00));
colors.put("darkorchid", new Color(0x9932cc));
colors.put("darkred", new Color(0x8b0000));
colors.put("darksalmon", new Color(0xe9967a));
colors.put("darkseagreen", new Color(0x8fbc8f));
colors.put("darkslateblue", new Color(0x483d8b));
colors.put("darkslategray", new Color(0x2f4f4f));
colors.put("darkturquoise", new Color(0x00ced1));
colors.put("darkviolet", new Color(0x9400d3));
colors.put("deeppink", new Color(0xff1493));
colors.put("deepskyblue", new Color(0x00bfff));
colors.put("dimgray", new Color(0x696969));
colors.put("dodgerblue", new Color(0x1e90ff));
colors.put("firebrick", new Color(0xb22222));
colors.put("floralwhite", new Color(0xfffaf0));
colors.put("forestgreen", new Color(0x228b22));
colors.put("fuchsia", new Color(0xff00ff));
colors.put("gainsboro", new Color(0xdcdcdc));
colors.put("ghostwhite", new Color(0xf8f8ff));
colors.put("gold", new Color(0xffd700));
colors.put("goldenrod", new Color(0xdaa520));
colors.put("gray", new Color(0x808080));
colors.put("green", new Color(0x008000));
colors.put("greenyellow", new Color(0xadff2f));
colors.put("honeydew", new Color(0xf0fff0));
colors.put("hotpink", new Color(0xff69b4));
colors.put("indianred", new Color(0xcd5c5c));
colors.put("indigo", new Color(0x4b0082));
colors.put("ivory", new Color(0xfffff0));
colors.put("khaki", new Color(0xf0e68c));
colors.put("lavender", new Color(0xe6e6fa));
colors.put("lavenderblush", new Color(0xfff0f5));
colors.put("lawngreen", new Color(0x7cfc00));
colors.put("lemonchiffon", new Color(0xfffacd));
colors.put("lightblue", new Color(0xadd8e6));
colors.put("lightcoral", new Color(0xf08080));
colors.put("lightcyan", new Color(0xe0ffff));
colors.put("lightgoldenrodyellow", new Color(0xfafad2));
colors.put("lightgreen", new Color(0x90ee90));
colors.put("lightgrey", new Color(0xd3d3d3));
colors.put("lightpink", new Color(0xffb6c1));
colors.put("lightsalmon", new Color(0xffa07a));
colors.put("lightseagreen", new Color(0x20b2aa));
colors.put("lightskyblue", new Color(0x87cefa));
colors.put("lightslategray", new Color(0x778899));
colors.put("lightsteelblue", new Color(0xb0c4de));
colors.put("lightyellow", new Color(0xffffe0));
colors.put("lime", new Color(0x00ff00));
colors.put("limegreen", new Color(0x32cd32));
colors.put("linen", new Color(0xfaf0e6));
colors.put("magenta", new Color(0xff00ff));
colors.put("maroon", new Color(0x800000));
colors.put("mediumaquamarine", new Color(0x66cdaa));
colors.put("mediumblue", new Color(0x0000cd));
colors.put("mediumorchid", new Color(0xba55d3));
colors.put("mediumpurple", new Color(0x9370db));
colors.put("mediumseagreen", new Color(0x3cb371));
colors.put("mediumslateblue", new Color(0x7b68ee));
colors.put("mediumspringgreen", new Color(0x00fa9a));
colors.put("mediumturquoise", new Color(0x48d1cc));
colors.put("mediumvioletred", new Color(0xc71585));
colors.put("midnightblue", new Color(0x191970));
colors.put("mintcream", new Color(0xf5fffa));
colors.put("mistyrose", new Color(0xffe4e1));
colors.put("moccasin", new Color(0xffe4b5));
colors.put("navajowhite", new Color(0xffdead));
colors.put("navy", new Color(0x000080));
colors.put("oldlace", new Color(0xfdf5e6));
colors.put("olive", new Color(0x808000));
colors.put("olivedrab", new Color(0x6b8e23));
colors.put("orange", new Color(0xffa500));
colors.put("orangered", new Color(0xff4500));
colors.put("orchid", new Color(0xda70d6));
colors.put("palegoldenrod", new Color(0xeee8aa));
colors.put("palegreen", new Color(0x98fb98));
colors.put("paleturquoise", new Color(0xafeeee));
colors.put("palevioletred", new Color(0xdb7093));
colors.put("papayawhip", new Color(0xffefd5));
colors.put("peachpuff", new Color(0xffdab9));
colors.put("peru", new Color(0xcd853f));
colors.put("pink", new Color(0xffc0cb));
colors.put("plum", new Color(0xdda0dd));
colors.put("powderblue", new Color(0xb0e0e6));
colors.put("purple", new Color(0x800080));
colors.put("red", new Color(0xff0000));
colors.put("rosybrown", new Color(0xbc8f8f));
colors.put("royalblue", new Color(0x4169e1));
colors.put("saddlebrown", new Color(0x8b4513));
colors.put("salmon", new Color(0xfa8072));
colors.put("sandybrown", new Color(0xf4a460));
colors.put("seagreen", new Color(0x2e8b57));
colors.put("seashell", new Color(0xfff5ee));
colors.put("sienna", new Color(0xa0522d));
colors.put("silver", new Color(0xc0c0c0));
colors.put("skyblue", new Color(0x87ceeb));
colors.put("slateblue", new Color(0x6a5acd));
colors.put("slategray", new Color(0x708090));
colors.put("snow", new Color(0xfffafa));
colors.put("springgreen", new Color(0x00ff7f));
colors.put("steelblue", new Color(0x4682b4));
colors.put("tan", new Color(0xd2b48c));
colors.put("teal", new Color(0x008080));
colors.put("thistle", new Color(0xd8bfd8));
colors.put("tomato", new Color(0xff6347));
colors.put("turquoise", new Color(0x40e0d0));
colors.put("violet", new Color(0xee82ee));
colors.put("wheat", new Color(0xf5deb3));
colors.put("white", new Color(0xffffff));
colors.put("whitesmoke", new Color(0xf5f5f5));
colors.put("yellow", new Color(0xffff00));
colors.put("yellowgreen", new Color(0x9acd32));
}
private static Color getColor(String color)
{
String origColor=color;
color = color.toLowerCase().replaceAll("[^a-z]", "");
if (colors.containsKey(color))
{
return colors.get(color);
}
else
{
return null;
}
}
private static void setupBuffering()
{
synchronized(turtleLock)
{
lastUpdate=0;
offscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
midscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
onscreenImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
offscreen = offscreenImage.createGraphics();
midscreen = midscreenImage.createGraphics();
onscreen = onscreenImage.createGraphics();
//offscreen.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
//offscreen.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
offscreen.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
midscreen.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
onscreen.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
drawBackground(offscreen);
drawBackground(onscreen);
icon.setImage(onscreenImage);
}
}
private static void drawTurtleIcon()
{
byte[] imageData= new byte[]{71,73,70,56,57,97,16,0,16,0,-95,2,0,0,-103,
0,0,-1,0,-1,-1,-1,-1,-1,-1,33,-7,4,1,10,0,2,0,44,0,0,0,0,16,0,16,0,
0,2,44,-108,-113,-87,-53,-19,-33,-128,4,104,74,35,67,-72,34,-21,11,
124,27,-90,-107,-109,72,117,-91,-71,110,103,-37,90,-31,-10,-55,-87,
122,-34,74,72,-15,17,-56,-127,8,33,5,0,59};
try
{
BufferedImage tmpicon = ImageIO.read(new ByteArrayInputStream(imageData));
window.setIconImage(tmpicon);
}
catch (Exception e) {}
}
private static void makeShapes()
{
shapes=new HashMap<String,Polygon>();
int[] xs=new int[]{66, 65, 63, 61, 53, 44, 33, 24, 23, 19, 17, 14, 9, 8, 8, 10, 13, 11, 10, 2, 9, 11, 15, 11, 11, 10, 12, 18, 20, 22, 23, 26, 35, 44, 53, 61, 62, 64, 66, 71, 77, 78, 77, 76, 72, 77, 81, 86, 91, 94, 97, 98, 97, 95, 92, 87, 82, 77, 72, 74, 77, 78, 76, 70};
int[] ys=new int[]{18, 19, 21, 25, 23, 22, 23, 27, 25, 21, 20, 21, 27, 30, 32, 34, 37, 42, 47, 50, 53, 59, 65, 68, 69, 71, 74, 79, 80, 80, 78, 74, 77, 78, 77, 75, 79, 81, 82, 81, 76, 73, 71, 69, 66, 59, 60, 61, 60, 58, 54, 50, 46, 42, 40, 39, 40, 41, 34, 32, 28, 27, 24, 19};
Polygon p = new Polygon(xs,ys,xs.length);
shapes.put("turtle", p);
xs=new int[]{0,100,0,20};
ys=new int[]{0,50,100,50};
p = new Polygon(xs,ys,xs.length);
shapes.put("arrow", p);
xs=new int[]{0,100,100,0};
ys=new int[]{0,0,100,100};
p = new Polygon(xs,ys,xs.length);
shapes.put("rectangle", p);
shapes.put("square", p);
xs=new int[]{0,100,0};
ys=new int[]{0,50,100};
p = new Polygon(xs,ys,xs.length);
shapes.put("triangle", p);
int divisions=24;
xs=new int[divisions];
ys=new int[divisions];
for(int i=0;i<divisions;i++)
{
double angle=Math.toRadians(i*360.0/divisions);
xs[i]=(int)Math.round(50+50*Math.cos(angle));
ys[i]=(int)Math.round(50+50*Math.sin(angle));
}
p = new Polygon(xs,ys,xs.length);
shapes.put("circle", p);
}
/* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ TURTLE CONSTRUCTION_/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
*/
/**
* This is a internal constuctor that makes a singleton that does the
* listening but is not added to the stack of turtles to be rendered.
* You don't need to use this outside of the Turtle.java file.
*
* @param i Pass this any integer. It doesn't do anything.
*/
private Turtle(int i){}
private Point2D.Double location=new Point2D.Double(0,0);
private double direction=0; //degrees
private String shape="turtle"; //Stores a key to the shapes hashmap
private BufferedImage image=null;
private double shapeWidth=33;
private double shapeHeight=33;
private double tilt=0;
private double penWidth=2;
private Color penColor=Color.BLACK;
private double outlineWidth=2;
private Color outlineColor=Color.BLACK;
private Color fillColor=new Color(0,255,0,128);
private double speed=50; //milliseconds to execute a move
private boolean isPenDown=true;
private boolean isFilling=false;
private boolean isVisible=true;
private ArrayList<Point2D.Double> polygon=new ArrayList<Point2D.Double>();
//temporary storage
private Long _time;
private Point2D.Double _location;
private Double _direction;
private String _shape;
private BufferedImage _image;
private Double _shapeWidth;
private Double _shapeHeight;
private Double _tilt;
private Double _penWidth;
private Color _penColor;
private Double _outlineWidth;
private Color _outlineColor;
private Color _fillColor;
private Double _speed;
private Boolean _isPenDown;
private Boolean _isFilling;
private Boolean _isVisible;
private ArrayList<Point2D.Double> _polygon;
private Boolean _isFill;
private Boolean _isStamp;
private Double _dotSize;
private Color _dotColor;
private Font _font;
private String _text;
private Integer _justification;
private Point2D.Double _textOffset;
//secondary temporary storage
private Long __time;
private Point2D.Double __location;
private Double __direction;
private String __shape;
private BufferedImage __image;
private Double __shapeWidth;
private Double __shapeHeight;
private Double __tilt;
private Double __penWidth;
private Color __penColor;
private Double __outlineWidth;
private Color __outlineColor;
private Color __fillColor;
private Double __speed;
private Boolean __isPenDown;
private Boolean __isFilling;
private Boolean __isVisible;
private ArrayList<Point2D.Double> __polygon;
private Boolean __isFill;
private Boolean __isStamp;
private Double __dotSize;
private Color __dotColor;
private Font __font;
private String __text;
private Integer __justification;
private Point2D.Double __textOffset;
/**
* Makes a default turtle.
*/
public Turtle()
{
if(window==null)init();
synchronized(turtleLock){turtles.add(this);}
long time=storeCurrentState();
updateAll();
}
/**
* Makes a default turtle at the specified position.
*
* @param x x coordinate
* @param y y coordinate
*/
public Turtle(double x,double y)
{
if(window==null)init();
location=new Point2D.Double(x,y);
synchronized(turtleLock){turtles.add(this);}
long time=storeCurrentState();
updateAll();
}
/**
* This creates a cloned copy of a turtle.
*
* @return a cloned copy of a turtle
*/
public Turtle clone()
{
Turtle t=new Turtle(0);
t.location=(Point2D.Double)this.location.clone();
t.direction=this.direction;
t.shape=t.shape;
t.image=this.image;
t.shapeWidth=this.shapeWidth;
t.shapeHeight=this.shapeHeight;
t.tilt=this.tilt;
t.penWidth=this.penWidth;
t.penColor=this.penColor;
t.outlineWidth=this.outlineWidth;
t.outlineColor=this.outlineColor;
t.fillColor=this.fillColor;
t.speed=this.speed;
t.isPenDown=this.isPenDown;
t.isFilling=this.isFilling;
t.isVisible=this.isVisible;
if(window==null)init();
synchronized(turtleLock){turtles.add(t);}
long time=t.storeCurrentState();
return t;
}
/* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ STATE MANAGEMENT _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
*/
private long storeCurrentState()
{
return storeCurrentState(false, false, 0,null,null,null,0,null);
}
private long storeAnimatedState()
{
return storeCurrentState(true, false, 0,null,null,null,0,null);
}
private long storeCurrentState(boolean animate, boolean isStamp,double dotSize,Color dotColor,Font font,String text,int justification,Point2D.Double textOffset)
{
ArrayList state = new ArrayList();
long time=System.nanoTime();
synchronized(turtleLock)
{
state.add(0); //0
state.add(this); //1
state.add(location.clone());//2
state.add(direction); //3
state.add(shape); //4
state.add(image); //5
state.add(shapeWidth); //6
state.add(shapeHeight); //7
state.add(tilt); //8
state.add(penWidth); //9
state.add(penColor); //10
state.add(outlineWidth); //11
state.add(outlineColor); //12
state.add(fillColor); //13
state.add(speed); //14
state.add(isPenDown); //15
state.add(isFilling); //16
state.add(isVisible); //17
state.add(isStamp); //18
state.add(dotSize); //19
state.add(dotColor); //20
state.add(font); //21
state.add(text); //22
state.add(justification); //23
state.add(textOffset); //24
if (!turtleStates.isEmpty() && turtleStates.lastKey()>time) time=turtleStates.lastKey()+1;
if (animate) time+=(long)(speed*1000000);
state.set(0, time);
turtleStates.put(time, state);
redoStates.clear();
}
if(refreshMode==REFRESH_MODE_STATE_CHANGE) draw();
if(refreshMode==REFRESH_MODE_ANIMATED)waitUntil(time);
return time;
}
private static void clearStorage()
{
synchronized(turtleLock)
{
for(Turtle t:turtles)
{
t.__time=null;
t.__location=null;
t.__direction=null;
t.__shape=null;
t.__image=null;
t.__shapeWidth=null;
t.__shapeHeight=null;
t.__tilt=null;
t.__penWidth=null;
t.__penColor=null;
t.__outlineWidth=null;
t.__outlineColor=null;
t.__fillColor=null;
t.__speed=null;
t.__isPenDown=null;
t.__isFilling=null;
t.__isVisible=null;
t.__isStamp=null;
t.__dotSize=null;
t.__dotColor=null;
t.__font=null;
t.__text=null;
t.__justification=null;
t.__textOffset=null;
t._time=null;
t._location=null;
t._direction=null;
t._shape=null;
t._shapeWidth=null;
t._shapeHeight=null;
t._image=null;
t._tilt=null;
t._penWidth=null;
t._penColor=null;
t._outlineWidth=null;
t._outlineColor=null;
t._fillColor=null;
t._speed=null;
t._isPenDown=null;
t._isFilling=null;
t._isVisible=null;
t._isStamp=null;
t._dotSize=null;
t._dotColor=null;
t._font=null;
t._text=null;
t._justification=null;
t._textOffset=null;
}
}
}
private static void retrieveState(long time)
{
synchronized(turtleLock)
{
Turtle t=getStateTurtle(turtleStates.get(time));
t.__time=t._time;
t.__location=t._location;
t.__direction=t._direction;
t.__shape=t._shape;
t.__image=t._image;
t.__shapeWidth=t._shapeWidth;
t.__shapeHeight=t._shapeHeight;
t.__tilt=t._tilt;
t.__penWidth=t._penWidth;
t.__penColor=t._penColor;
t.__outlineWidth=t._outlineWidth;
t.__outlineColor=t._outlineColor;
t.__fillColor=t._fillColor;
t.__speed=t._speed;
t.__isPenDown=t._isPenDown;
t.__isFilling=t._isFilling;
t.__isVisible=t._isVisible;
t.__isStamp=t._isStamp;
t.__dotSize=t._dotSize;
t.__dotColor=t._dotColor;
t.__font=t._font;
t.__text=t._text;
t.__justification=t._justification;
t.__textOffset=t._textOffset;
ArrayList state=turtleStates.get(time);
t._time=getStateTime(state);
t._location=getStateLocation(state);
t._direction=getStateDirection(state);
t._shape=getStateShape(state);
t._shapeWidth=getStateShapeWidth(state);
t._shapeHeight=getStateShapeHeight(state);
t._image=getStateImage(state);
t._tilt=getStateTilt(state);
t._penWidth=getStatePenWidth(state);
t._penColor=getStatePenColor(state);
t._outlineWidth=getStateOutlineWidth(state);
t._outlineColor=getStateOutlineColor(state);
t._fillColor=getStateFillColor(state);
t._speed=getStateSpeed(state);
t._isPenDown=getStateIsPenDown(state);
t._isFilling=getStateIsFilling(state);
t._isVisible=getStateIsVisible(state);
t._isStamp=getStateIsStamp(state);
t._dotSize=getStateDotSize(state);
t._dotColor=getStateDotColor(state);
t._font=getStateFont(state);
t._text=getStateText(state);
t._justification=getStateJustification(state);
t._textOffset=getStateTextOffset(state);
}
}
private static long getStateTime(ArrayList state){return (Long)state.get(0);}
private static Turtle getStateTurtle(ArrayList state){return (Turtle)state.get(1);}
private static Point2D.Double getStateLocation(ArrayList state){return (Point2D.Double)((Point2D.Double)state.get(2)).clone();}
private static double getStateDirection(ArrayList state){return (Double)state.get(3);}
private static String getStateShape(ArrayList state){return (String)state.get(4);}
private static BufferedImage getStateImage(ArrayList state){return (BufferedImage)state.get(5);}
private static double getStateShapeWidth(ArrayList state){return (Double)state.get(6);}
private static double getStateShapeHeight(ArrayList state){return (Double)state.get(7);}
private static double getStateTilt(ArrayList state){return (Double)state.get(8);}
private static double getStatePenWidth(ArrayList state){return (Double)state.get(9);}
private static Color getStatePenColor(ArrayList state){return (Color)state.get(10);}
private static double getStateOutlineWidth(ArrayList state){return (Double)state.get(11);}
private static Color getStateOutlineColor(ArrayList state){return (Color)state.get(12);}
private static Color getStateFillColor(ArrayList state){return (Color)state.get(13);}
private static double getStateSpeed(ArrayList state){return (Double)state.get(14);}
private static boolean getStateIsPenDown(ArrayList state){return (Boolean)state.get(15);}
private static boolean getStateIsFilling(ArrayList state){return (Boolean)state.get(16);}
private static boolean getStateIsVisible(ArrayList state){return (Boolean)state.get(17);}
private static boolean getStateIsStamp(ArrayList state){return (Boolean)state.get(18);}
private static double getStateDotSize(ArrayList state){return (Double)state.get(19);}
private static Color getStateDotColor(ArrayList state){return (Color)state.get(20);}
private static Font getStateFont(ArrayList state){return (Font)state.get(21);}
private static String getStateText(ArrayList state){return (String)state.get(22);}
private static int getStateJustification(ArrayList state){return (Integer)state.get(23);}
private static Point2D.Double getStateTextOffset(ArrayList state){return (Point2D.Double)state.get(24);}
private static void restoreState(long time)
{
ArrayList state=turtleStates.get(time);
Turtle t=getStateTurtle(turtleStates.get(time));
t.location=getStateLocation(state);
t.direction=getStateDirection(state);
t.shape=getStateShape(state);
t.shapeWidth=getStateShapeWidth(state);
t.shapeHeight=getStateShapeHeight(state);
t.image=getStateImage(state);
t.tilt=getStateTilt(state);
t.penWidth=getStatePenWidth(state);
t.penColor=getStatePenColor(state);
t.outlineWidth=getStateOutlineWidth(state);
t.outlineColor=getStateOutlineColor(state);
t.fillColor=getStateFillColor(state);
t.speed=getStateSpeed(state);
t.isPenDown=getStateIsPenDown(state);
t.isFilling=getStateIsFilling(state);
t.isVisible=getStateIsVisible(state);
if(refreshMode==REFRESH_MODE_STATE_CHANGE) draw();
}
private void select()
{
selectedTurtle=this;
}
private void unselect()
{
if (selectedTurtle==this) selectedTurtle=null;
}
/**
* Determines if a turtle is covering a screen position
*
* @param x x screen coordinate
* @param y y screen coordinate
* @return true if this turtle is at the indicated screen position.
*/
public boolean contains(int x, int y)
{
Point2D.Double point=new Point2D.Double(x,y);
if (_location==null)return false;
AffineTransform m = offscreen.getTransform();
double x1,y1,dir1;
x1=_location.x;
y1=_location.y;
dir1=_direction;
m.translate(((x1-centerX)*scale+width/2),((y1-centerY)*(-scale)+height/2));
m.scale(scale, scale);
if (image==null)
{
m.rotate(-Math.toRadians(dir1));
m.scale(shapeWidth/100.0, shapeHeight/100.0);
m.translate(-50,-50);
Polygon p =shapes.get(shape);
GeneralPath gp=new GeneralPath();
gp.append(p.getPathIterator(m),false);
return gp.contains(x, y);
}
else
{
int w=image.getWidth();
int h=image.getHeight();
m.rotate(-Math.toRadians(dir1));
m.scale(shapeWidth/1.0/w, shapeHeight/1.0/h);
m.translate(-w/2,-h/2);
try {m.inverseTransform(point, point);}
catch(Exception e){return false;}
x=(int)point.x;
y=(int)point.y;
try
{
//System.out.println((new Color(image.getRGB(x, y),true)).getAlpha());
return (new Color(image.getRGB(x, y),true)).getAlpha()>50;
}
catch(Exception e){return false;}
}
}
/* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ TURTLE METHODS _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U
* .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*) .-./*)
* _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/ _/___\/
* U U U U U U U U U U U U U U U U
*/
/**
* Gets the speed of the animation.
* @return milliseconds it takes to do one action
*/
public double getSpeed()
{
return speed;
}
/**
* Sets the speed of the animation.
* @param delay milliseconds it takes to do one action
* @return state change timestamp
*/
public long speed(double delay)
{
this.speed=delay;
long timeStamp=storeCurrentState();
return timeStamp;
}
/**
* Moves the turtle forward by the distance.
*
* @param distance distance to move forward
* @return state change timestamp
*/
public long forward(double distance)
{
double angle=Math.toRadians(direction);
Point2D.Double pastLocation=(Point2D.Double)location.clone();
location.x+=distance*Math.cos(angle);
location.y+=distance*Math.sin(angle);
long timeStamp=storeAnimatedState();
return timeStamp;
}
/**
* Moves the turtle backward by the distance.
*
* @param distance distance to move backward
* @return state change timestamp
*/
public long backward(double distance)
{
double angle=Math.toRadians(direction);
Point2D.Double pastLocation=(Point2D.Double)location.clone();
location.x-=distance*Math.cos(angle);
location.y-=distance*Math.sin(angle);
long timeStamp=storeAnimatedState();
return timeStamp;
}
/**
* Turns the turtle left by the number of indicated degrees.
*
* @param angle angle in degrees
* @return state change timestamp
*/
public long left(double angle)
{
direction+=angle;
long timeStamp=storeAnimatedState();
return timeStamp;
}
/**
* Turns the turtle right by the number of indicated degrees.
*
* @param angle angle in degrees
* @return state change timestamp
*/
public long right(double angle)
{
direction-=angle;
long timeStamp=storeAnimatedState();
return timeStamp;
}
/**
* Gets the direction the turtle is facing neglecting tilt.
*
* @return state change timestamp
*/
public double getDirection()
{
double a=direction;
while(a>=360)a-=360;
while(a<0)a+=360;
return a;
}
/**