-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralApplet.prejava
5798 lines (5246 loc) · 286 KB
/
SpiralApplet.prejava
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
/*
Notes:
Uses System.nanoTime which requires java >= 1.4
(probably need even later, haven't been keeping track)
BUG: tooltip is weird on the "Anim sleep ms" textfield
BUG: on assert fail in paint, displayed line number is too small by a few lines I think
BUG: ending criteria isn't quite right-- should end when *all* contributing things have gone off screen or become tiny, need to clean up logic for that
BUG: as soon as a tooltip is shown spilling over edge of window, performance went down, forevermore?? (I think that was on windows? or maybe old version of java, 1.7 and fixed in 1.8?) so I fudged the window size so that wouldn't happen
TODO: shortcut management is not completely robust
TODO: disable Regenerate button unless out of date? or turn it red when out of date?
TODO: more friendly and usable control of speed
TODO: more friendly and usable control of shape. should be able to easily express:
- constant slope of user's choosing (have this crudely with 0gradual, 0, 0steep) (DONE)
- arbitrary slope center and std deviation? need to think about it
- random walk params? maybe need to review that.
TODO: maybe just pass arraylists to the plotter, not functors... it's not like we get any streaming benefit
TODO: non-rotating mode!
TODO: saturation slider
TODO: keystrokes to change orientation
TODO: smooth out animation when going very slowly
TODO: gui for scale
TODO: allow dragging points on spiral? hmm
todo: check out LWJGL for consistent frame rate? nah too much effort.
TODO: maybe JavaFX? she says it works well for vsync: http://www.java-gaming.org/index.php?topic=27763.0. ah good, it's included in jdk 7. using it.
TODO: use javax.swing.Timer instead of my own custom thread
WHOA! as soon as I switched to this kind of timer, I'm getting consistent timings! nice 60fps. wtf?
well except every 5 seconds or so, it gives far faster for a few frames in a row. argh!
sleeping for 11 ms seems to be a sweet spot. screwy.
and a few days later, it's not happening any more... it's going full speed again, even with the timer. wtf??
but then a few minutes later, it's throttled again. weird weird weird.
actually maybe when non-throttled, I'd been running it for a while?
TODO: allow saving/restoring window positions, but in a discoverable non-evil way
TODO: can we automatically detect missing @Override's? the web says not using javac, but Eclipse can do it I think
*/
#include "macros.h"
import com.donhatchsw.util.Arrays;
import com.donhatchsw.util.Complex;
import com.donhatchsw.util.MyMath;
import com.donhatchsw.util.VecMath;
//import com.donhatchsw.util.SortStuff;
import com.donhatchsw.util.Listenable;
import com.donhatchsw.compat.ArrayList;
//import com.donhatchsw.compat.IntArrayList;
//import com.donhatchsw.compat.DoubleArrayList;
import com.donhatchsw.awt.ColLayout;
import com.donhatchsw.awt.JRow;
import com.donhatchsw.awt.JCol;
import com.donhatchsw.awt.JTablePanel;
import com.donhatchsw.awt.MainWindowCount;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.text.JTextComponent;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSlider;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
//import javax.swing.JComboBox;
import javax.swing.JCheckBox;
public class SpiralApplet
extends com.donhatchsw.shims_for_deprecated.javax_swing_JApplet
{
//
// Utilities for applet stuff...
//
private String getParameterString(String paramName,
String defaultValue)
{
String paramValueString = getParameter(paramName);
if (paramValueString != null)
return paramValueString;
else
return defaultValue;
}
private int getParameterInt(String paramName,
int defaultValue)
{
String paramValueString = getParameter(paramName);
if (paramValueString != null)
{
try
{
return Integer.decode(paramValueString).intValue();
}
catch (NumberFormatException e)
{
return 0;
}
}
else
return defaultValue;
}
private double getParameterDouble(String paramName,
double defaultValue)
{
String paramValueString = getParameter(paramName);
if (paramValueString != null)
{
try
{
return Double.parseDouble(paramValueString);
}
catch (NumberFormatException e)
{
return 0;
}
}
else
return defaultValue;
}
private boolean getParameterBoolean(String paramName,
boolean defaultValue)
{
String paramValueString = getParameter(paramName);
if (paramValueString != null)
{
// Too simplistic, only honors case-sensitive "true"
//return Boolean.valueOf(paramValueString).booleanValue();
try
{
int n = Integer.decode(paramValueString).intValue();
return n != 0;
}
catch (NumberFormatException e)
{
if (paramValueString.equalsIgnoreCase("true")
|| paramValueString.equalsIgnoreCase("yes")
|| paramValueString.equalsIgnoreCase("t")
|| paramValueString.equalsIgnoreCase("y"))
return true;
return false;
}
}
else
return defaultValue;
}
// ButtonGroup doesn't support *at most* one checked.
// Implement one that does.
private static class ButtonGroupThatAllowsNone
{
private ArrayList buttons = new ArrayList();
private ArrayList actionListeners = new ArrayList();
public void add(final JToggleButton button) // checkbox or radio button
{
java.awt.event.ActionListener actionListener = new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
if (button.isSelected())
{
// then deselect everyone else
int nButtons = buttons.size();
for (int i = 0; i < nButtons; ++i)
{
JToggleButton otherButton = (JToggleButton)buttons.get(i);
if (otherButton != button)
if (otherButton.isSelected())
otherButton.setSelected(false);
}
}
}
};
button.addActionListener(actionListener);
buttons.add(button);
actionListeners.add(actionListener);
}
}; // class ButtonGroupThatAllowsNone
// That was fun, let's make another that enforces
// that the selection is a prefix of the set of buttons.
private static class PrefixButtonGroup
{
private ArrayList buttons = new ArrayList();
private ArrayList actionListeners = new ArrayList();
public void add(final JToggleButton button) // checkbox or radio button
{
java.awt.event.ActionListener actionListener = new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
int nButtons = buttons.size();
if (button.isSelected())
{
// then select everyone before me
FORI (i, nButtons)
{
JToggleButton otherButton = (JToggleButton)buttons.get(i);
if (otherButton == button)
break;
if (!otherButton.isSelected())
otherButton.setSelected(true);
}
}
else
{
// deselect everyone after me
FORIDOWN(i, nButtons)
{
JToggleButton otherButton = (JToggleButton)buttons.get(i);
if (otherButton == button)
break;
if (otherButton.isSelected())
otherButton.setSelected(false);
}
}
}
};
button.addActionListener(actionListener);
buttons.add(button);
actionListeners.add(actionListener);
}
} // PrefixButtonGroup
// http://tech-eureka.blogspot.com/2010/12/how-to-implement-repeating-swing.html
private static class JRepeatingButton extends JButton
{
public JRepeatingButton(String text)
{
super(text);
}
@Override public void addActionListener(final java.awt.event.ActionListener actionListener)
{
addMouseListener(new java.awt.event.MouseAdapter() {
private javax.swing.Timer repeatTimer = null;
private java.awt.event.ActionEvent synthesizedActionEvent = null;
@Override public void mousePressed(java.awt.event.MouseEvent mouseEvent)
{
// TODO: this used to be mouseEvent.getModifiers(), but that got
// deprecated. Unfortunately the value returned by getModifiersEx()
// isn't suitable for constructing the ActionEvent, since the bits
// are interpreted differently. E.g. BUTTON1_MASK=16 vs. BUTTON1_DOWN_MASK=1024.
int modifiers = 0; // for now, can't modify
synthesizedActionEvent = new java.awt.event.ActionEvent(
JRepeatingButton.this,
java.awt.event.ActionEvent.ACTION_PERFORMED,
JRepeatingButton.this.getText(),
modifiers);
// the ctor that takes "when" introduced some time in (1.3, 1.6]
// so don't use it, so we can compile using 1.3
// TODO: use it!
actionListener.actionPerformed(synthesizedActionEvent); // initial execution
repeatTimer = new javax.swing.Timer(100, new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent timerActionEvent)
{
actionListener.actionPerformed(synthesizedActionEvent);
}
});
repeatTimer.setInitialDelay(500);
repeatTimer.start();
}
@Override public void mouseReleased(java.awt.event.MouseEvent arg0)
{
repeatTimer.stop();
repeatTimer = null;
synthesizedActionEvent = null;
}
});
}
} // JRepeatingButton
// A textfield that turns green or red when editing and not yet committed.
// Esc reverts.
private static class JValidatingTextField extends JTextField
{
// subclass can override this
public boolean validate(String text)
{
return true; // it's all good by default
}
private String committedText;
public JValidatingTextField(String text)
{
super(text);
committedText = getText();
getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
private void anyUpdate(javax.swing.event.DocumentEvent e)
{
if (isEditable()) // use this as hint-- if not editable, probably makeTextComponentLikeLabel was called
{
if (!getText().equals(committedText)) // TODO: == doesn't work here? I forget the difference
{
if (validate(getText()))
{
// TODO: if value semantically equals committed value, turn white instead. hmm, can build this into validate() by having it return a value (Object), maybe?
setBackground(new java.awt.Color(192,255,192)); // light green
}
else
setBackground(new java.awt.Color(255,192,192)); // pink
}
else
setBackground(java.awt.Color.WHITE);
}
}
@Override public void insertUpdate(javax.swing.event.DocumentEvent e) {anyUpdate(e);}
@Override public void removeUpdate(javax.swing.event.DocumentEvent e) {anyUpdate(e);}
@Override public void changedUpdate(javax.swing.event.DocumentEvent e) {anyUpdate(e);}
});
addKeyListener(new java.awt.event.KeyAdapter() {
@Override public void keyPressed(java.awt.event.KeyEvent e)
{
if (e.getKeyCode() == java.awt.event.KeyEvent.VK_ESCAPE)
{
setText(committedText);
}
}
});
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
if (validate(getText()))
{
committedText = getText();
setBackground(java.awt.Color.WHITE);
}
}
});
}
@Override public void setText(String text)
{
super.setText(text);
if (isEditable()) // use this as hint-- if not editable, probably makeTextComponentLikeLabel was called
{
if (validate(getText()))
{
committedText = getText();
setBackground(java.awt.Color.WHITE);
}
}
}
} // JValidatingTextField
private static class SpiralAppletControlPanel
extends JPanel
{
public SpiralAppletControlPanel(final SpiralApplet applet)
{
java.awt.Container contentPane = this;
contentPane.setLayout(new ColLayout());
// used as "fill" argument to add()
final java.awt.GridBagConstraints stretchx = new java.awt.GridBagConstraints(){{fill=HORIZONTAL;weightx=1.;}};
final java.awt.GridBagConstraints stretchy = new java.awt.GridBagConstraints(){{fill=VERTICAL;weighty=1.;}};
final java.awt.GridBagConstraints stretchxy = new java.awt.GridBagConstraints(){{fill=BOTH;weightx=weighty=1.;}};
final java.awt.GridBagConstraints rightjustify = new java.awt.GridBagConstraints(){{anchor=EAST;}};
final java.awt.GridBagConstraints centerjustify = new java.awt.GridBagConstraints(){{anchor=CENTER;}};
// used as arg to setMargin()
final java.awt.Insets nomargin = new java.awt.Insets(0,0,0,0);
final java.awt.Color asciiToColor[] = new java.awt.Color[128];
asciiToColor[' '] = null;
asciiToColor['b'] = java.awt.Color.BLACK;
asciiToColor['g'] = new java.awt.Color(128,128,128); // 50% gray
asciiToColor['r'] = java.awt.Color.RED;
/*
contentPane.add(new JSeparator(), stretchx);
contentPane.add(new JLabel("File/Edit"));
add(new JRow() {{
add(new JLabel(" ")); // indent a bit
add(new JCol() {{
// ...
}});
}});
*/
contentPane.add(new JSeparator(), stretchx);
contentPane.add(new JLabel("Model"));
add(new JRow() {{
add(new JLabel(" ")); // indent a bit
add(new JCol() {{
add(new JRow() {{
add(new JRow() {
private ButtonGroup buttonGroup = new ButtonGroup();
{
add(new JLabel("Delta style:"));
add(new JLabel(" "));
add(new JCol() {{
add(new JRadioButton("<html>0 very gradual (1)</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_0_VERYGRADUAL) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_0_VERYGRADUAL);
// no need to repaint
}
});
}});
add(new JRadioButton("<html>0 gradual (30)</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_0_GRADUAL) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_0_GRADUAL);
// no need to repaint
}
});
}});
add(new JRadioButton("<html>0 medium (45)</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_0) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_0);
// no need to repaint
}
});
}});
add(new JRadioButton("<html>0 steep (60)</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_0_STEEP) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_0_STEEP);
// no need to repaint
}
});
}});
add(new JRadioButton("<html>0 very steep (89)</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_0_VERYSTEEP) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_0_VERYSTEEP);
// no need to repaint
}
});
}});
add(new JRow() {{
add(new JRadioButton("<html>0</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_0_USER_SPECIFIED) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_0_USER_SPECIFIED);
// no need to repaint
}
});
}});
add(makeIncrementableTextFieldForNumber(applet.spiralModelParams.deltaStyle_0_degrees, "steepness of deltastyle 0 (0 = very gradual, 90 = very steep)", 1));
}});
add(new JRadioButton("<html>±1</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_1) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_1);
// no need to repaint
}
});
}});
add(new JRadioButton("Any", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_ANY) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_ANY);
// no need to repaint
}
});
}});
add(new JRadioButton("<html>±1 wrap</html>", applet.spiralModelParams.deltaStyle.get()==SpiralModel.DELTASTYLE_1_WRAP) {{
setMargin(nomargin);
buttonGroup.add(this);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.spiralModelParams.deltaStyle.set(SpiralModel.DELTASTYLE_1_WRAP);
// no need to repaint
}
});
}});
}});
}});
add(new JLabel(" "), stretchx);
add(new JTablePanel() {{
{
add(new JSeparator());
add(new JLabel("current"));
add(new JSeparator());
add(new JLabel(" on regen"));
}
advanceRow();
{
final String toolTipText = "period";
add(new JLabel("Period:") {{
setToolTipText(toolTipText);
}});
// Note this will get chopped badly if user enters a number with more digits than
// the original, but that generally doesn't happen
add(new JTextFieldForNumber(applet.theSpiralModel.params.n) {{
makeTextComponentLikeLabel(this);
setToolTipText(toolTipText);
}});
add(new JLabel(" "));
// period must be multiple of 4 and positive to prevent assertion error
add(makeIncrementableTextFieldForMultipleOf(applet.spiralModelParams.n, toolTipText, 4, 4, true));
}
advanceRow();
{
final String toolTipText = "number of parts into which to break 90 degrees";
add(new JLabel("Number of parts per 90 degrees:") {{
setToolTipText(toolTipText);
}});
add(new JTextFieldForNumber(applet.theSpiralModel.params.nParts) {{
makeTextComponentLikeLabel(this);
setToolTipText(toolTipText);
}});
add(new JLabel(" "));
add(makeIncrementableTextFieldForNumber(applet.spiralModelParams.nParts, toolTipText, 1));
}
advanceRow();
{
final String toolTipText = "seed";
add(new JLabel("Random seed:") {{
setToolTipText(toolTipText);
}});
add(new JTextFieldForNumber(applet.theSpiralModel.params.randomSeed) {{
makeTextComponentLikeLabel(this);
setToolTipText(toolTipText);
}});
add(new JLabel(" "));
add(makeIncrementableTextFieldForNumber(applet.spiralModelParams.randomSeed, toolTipText, 1));
}
}});
}}, stretchx);
add(new JButton("Regenerate") {{
setMargin(nomargin);
// XXX TODO: red if params out of date
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.theSpiralModel.init(applet.spiralModelParams.n.get(), applet.spiralModelParams.nParts.get(), applet.spiralModelParams.deltaStyle.get(), applet.spiralModelParams.deltaStyle_0_degrees.get(), applet.spiralModelParams.randomSeed.get());
// XXX hmm, not sure about this, seems intrusive... maybe should just do it when about to use it?
applet.theSpiralView.focus.modEquals(
applet.theSpiralModel.params.n.get(), 1);
applet.theCanvas.repaint();
}
});
}}, centerjustify);
}}, stretchx);
}}, stretchx);
contentPane.add(new JSeparator(), stretchx);
contentPane.add(new JLabel("View"));
add(new JRow() {{
add(new JLabel(" ")); // indent a bit
add(new JCol() {{
add(new JRow() {{
add(new JButton("reset focus to") {{
setMargin(nomargin);
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.theSpiralView.focus.set(applet.theSpiralView.resetFocusTo.get(),1);
applet.theCanvas.repaint();
}
});
}}, centerjustify);
add(new JTextFieldForNumber(applet.theSpiralView.resetFocusTo));
}});
add(new JRow() {{
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
" b b",
" bb bb",
" bbbbbb",
"bbbbbbb",
" bbbbbb",
" bb bb",
" b b",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.fastBackward();
}
});
setToolTipText("<html>fast backward</html>");
}}, stretchxy);
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
" gb",
" gbbb",
" gbbbbb",
"bbbbbbb",
" gbbbbb",
" gbbb",
" gb",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.playBackward();
}
});
setToolTipText("<html>play backward<br>(shift-left-arrow key)</html>");
}}, stretchxy);
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
" b bb",
" bb bb",
" bbb bb",
"bbbb bb",
" bbb bb",
" bb bb",
" b bb",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.stepBackward();
}
});
setToolTipText("<html>step backward<br>(left-arrow key)</html>");
}}, stretchxy);
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
"bbb bbb",
"bbb bbb",
"bbb bbb",
"bbb bbb",
"bbb bbb",
"bbb bbb",
"bbb bbb",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.pause();
}
});
setToolTipText("<html>stop<br>(space key)</html>");
}}, stretchxy);
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
"bb b ",
"bb bb ",
"bb bbb ",
"bb bbbb",
"bb bbb ",
"bb bb ",
"bb b ",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.stepForward();
}
});
setToolTipText("<html>step forward<br>(right-arrow key)</html>");
}}, stretchxy);
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
"bg ",
"bbbg ",
"bbbbbg ",
"bbbbbbb",
"bbbbbg ",
"bbbg ",
"bg ",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.playForward();
}
});
setToolTipText("<html>play forward<br>(shift-right-arrow key)</html>");
}}, stretchxy);
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return new java.awt.Dimension(20*hidpimag,20*hidpimag); }
@Override public void paint(java.awt.Graphics g)
{
super.paint(g);
drawPixmapCentered(g, getSize(), hidpimag, asciiToColor,
new String[] {
"b b ",
"bb bb ",
"bbbbbb ",
"bbbbbbb",
"bbbbbb ",
"bb bb ",
"b b ",
});
}
{
addActionListener(new java.awt.event.ActionListener() {
@Override public void actionPerformed(java.awt.event.ActionEvent e)
{
applet.fastForward();
}
});
setToolTipText("<html>fast forward</html>");
}}, stretchxy);
}});
}});
add(new JLabel(" "));
add(new JCol() {{
add(new JTablePanel() {{
{
// XXX TODO: comes out weird on the textfield
final String toolTipText = "<html>number of milliseconds to sleep after each vsync pulse before calling repaint()."
+ (useJavaFX ? "<br>Probably not needed since using javaFX which stabilizes the frame rate."
: "<br>Probably needed since not using javaFX.")
+ "</html>";
add(new JLabel("Anim sleep ms:") {{
setToolTipText(toolTipText);
}});
add(new JRow() {{
add(makeIncrementableTextFieldForNumber(applet.animSleepMS, toolTipText, 1));
add(new JLabel(" (javaFX "+(useJavaFX ? "enabled" : "disabled")+")"));
}});
}
advanceRow();
{
final String toolTipText = "pixels per part in ticker and stats plots";
add(new JLabel("Ticker/plot pixels per part:") {{
setToolTipText(toolTipText);
}});
add(makeIncrementableTextFieldForNumber(applet.theSpiralView.pixelsPerPart, toolTipText, 1));
}
advanceRow();
{
final String toolTipText = "parts/frame";
add(new JLabel("Animation speed parts/frame:") {{
setToolTipText(toolTipText);
}});
add(new JRow() {{
add(makeIncrementableTextFieldForNumber(applet.theSpiralView.animSpeedParts, "parts in parts/frame", 1));
add(new JLabel(" / "));
add(makeIncrementableTextFieldForNumber(applet.theSpiralView.animSpeedFrames, "frames in parts/frame", 1));
add(new JLabel(" "));
}});
}
}});
add(new JRow() {{
add(new JCheckBoxForBoolean("show rungs", applet.theSpiralView.showRungsFlag) {{
setMargin(nomargin);
}});
add(new JCheckBoxForBoolean("pastels", applet.theSpiralView.pastelsFlag) {{
setMargin(nomargin);
}});
}});
}});
add(new JRepeatingButton("") {
@Override public java.awt.Dimension getPreferredSize() { return new java.awt.Dimension(45*hidpimag,45*hidpimag); }
@Override public java.awt.Dimension getMinimumSize() { return getPreferredSize(); }
@Override public void paint(java.awt.Graphics g)
{
if (debug) OUT(" in orientation button paint");
super.paint(g);
// note the following is padded by 1 pixel on all sides,
// to allow for highlighting to change color there
String rows[] = new String[] {
" ",
" b b ",
" gg gg ",
" b b b b ",
" b gbg b ",
" bg b b b gb ",
" gbgb bgbg ",
" gb bg ",
" b b ",
" gg gg ",
" b b ",
" gg gg ",
" bbbbb bbbbb ",
" ",
" ",
" ",
" bg gb ",
" gbg gbg ",
" bb bb ",
" gbg gbg ",
" bg gb ",
" ",
" ",
" ",
" bbbbb bbbbb ",
" gg gg ",
" b b ",
" gg gg ",
" b b ",
" gb bg ",
" gbgb bgbg ",
" bg b b b gb ",
" b gbg b ",
" b b b b ",
" gg gg ",
" b b ",
" ",
};
// convert the whole thing to char array
char chars[][] = new char[rows.length][];
FORI (iRow, rows.length)
chars[iRow] = rows[iRow].toCharArray();
java.awt.Color fudgedAsciiToColor[] = (java.awt.Color[])Arrays.copy(asciiToColor,1);
{
// find which little icon is in the given direction
// and highlight it.
// holy moly!
java.awt.Dimension gsize = getSize();
int orientationNumbersToHighlight[] = new int[2];
int nToHighlight = 0;
if (applet.theSpiralView.orientationNumber.get() != -1)
orientationNumbersToHighlight[nToHighlight++] = applet.theSpiralView.orientationNumber.get();
if (this.spiralOrientationNumber != -1
&& this.spiralOrientationNumber != applet.theSpiralView.orientationNumber.get())
orientationNumbersToHighlight[nToHighlight++] = this.spiralOrientationNumber;
//PRINT(nToHighlight);
//PRINTARRAY(orientationNumbersToHighlight);
FORI (iToHighlight, nToHighlight)
{
int orientationNumberToHighlight = orientationNumbersToHighlight[iToHighlight];
double angle = orientationNumberToHighlight*(2*Math.PI/12);
char highlightChar = "0123456789!?".charAt(orientationNumberToHighlight);
fudgedAsciiToColor[highlightChar] = getHSBColor((float)(angle/(2*Math.PI)), 1.f, 1.f);
int center = (rows.length+1)/2;
double cos = Math.cos(angle);
double sin = Math.sin(angle);
CHECK_EQ(rows.length, rows[0].length());
int seedRow = -1, seedCol = -1;
boolean foundSeed = false;
FORI (i, center)
{
seedCol = (int)Math.round(center + cos*(1+i));
seedRow = (int)Math.round(center - sin*(1+i));
if (rows[seedRow].charAt(seedCol) != ' ')
{
foundSeed = true;
break;
}
}
CHECK(foundSeed);
// seed fill, setting outline to highlight char
{
boolean isSeen[][] = new boolean[rows.length][rows.length]; // false initially
int seen[][] = new int[rows.length*rows.length][2];
int nSeen = 0;
seen[nSeen][0] = seedRow;
seen[nSeen][1] = seedCol;
nSeen++;
FORI (iSeen, nSeen) // while nSeen is growing
{
int row0 = seen[iSeen][0];
int col0 = seen[iSeen][1];
if (chars[row0][col0] == ' ')
{
// don't proceed to neighbors, but change color
chars[row0][col0] = highlightChar;
}
else
{
// don't proceed to neighbors, but change color
for (int drow = -1; drow <= 1; drow++)
for (int dcol = -1; dcol <= 1; dcol++)
{
if (drow==0 && dcol==0) continue;
int row1 = row0+drow;
int col1 = col0+dcol;
if (!INRANGE(0 <=, row1, < rows.length)
|| !INRANGE(0 <=, col1, < rows.length))
CHECK(false); // shouldn't happen
if (isSeen[row1][col1]) continue;
isSeen[row1][col1] = true;
seen[nSeen][0] = row1;
seen[nSeen][1] = col1;
nSeen++;
}
}
}
if (false) // can change to true for debugging
{
fudgedAsciiToColor['w'] = java.awt.Color.WHITE;
chars[seedRow][seedCol] = 'w';
}
}
}
}
// convert the whole thing back to Strings
FORI (iRow, rows.length)
rows[iRow] = new String(chars[iRow]);
drawPixmapCentered(g, getSize(), hidpimag, fudgedAsciiToColor, rows);
if (debug) OUT(" out orientation button paint");
} // paint
private void updateSpiralOrientationNumber(int mouseX, int mouseY)
{
java.awt.Dimension gsize = getSize();
double angle = Math.atan2(-(mouseY-(gsize.height*.5)),
mouseX-(gsize.width*.5));
spiralOrientationNumber = (int)Math.round(angle*(12/(2*Math.PI)));
spiralOrientationNumber = MOD(spiralOrientationNumber,12);
}
// keep track of state...
boolean pressed = false;
boolean entered = false;
boolean debug = false;
int spiralOrientationNumber = -1; // gets set during paint
{
// Listeners for spiral orientation number control
addMouseMotionListener(new java.awt.event.MouseAdapter() {
@Override public void mouseMoved(java.awt.event.MouseEvent mouseEvent)
{
if (debug) OUT(" in mouseMoved");
updateSpiralOrientationNumber(mouseEvent.getX(), mouseEvent.getY());
repaint();
if (debug) OUT(" out mouseMoved");
}
@Override public void mouseDragged(java.awt.event.MouseEvent mouseEvent)
{