-
Notifications
You must be signed in to change notification settings - Fork 0
/
Magic.java
902 lines (753 loc) · 27.9 KB
/
Magic.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
import java.util.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Magic.Java
* Version 6.4.1
* @author ablock
*
*/
public class Magic {
// *** MAGIC LEVEL 0: Simple input/Output ***
// Output methods
public static void print(String output){
strictCheck();
System.out.print(output);
}
public static void print(int output){
strictCheck();
System.out.print(output);
}
public static void print(boolean output){
strictCheck();
System.out.print(output);
}
public static void print(double output){
strictCheck();
System.out.print(output);
}
public static void print(char output){
strictCheck();
System.out.print(output);
}
public static void println(String output){
strictCheck();
System.out.println(output);
}
public static void println(double output){
strictCheck();
System.out.println(output);
}
public static void println(int output){
strictCheck();
System.out.println(output);
}
public static void println(boolean output){
strictCheck();
System.out.println(output);
}
public static void println(char output){
strictCheck();
System.out.println(output);
}
public static int random(int max){
Random rand = new Random();
int number = rand.nextInt(max+1);
return number;
}
// Input Methods
private static Scanner scan = new Scanner(System.in);
public static int nextInt(){
strictCheck();
int rtn = scan.nextInt();
scan.nextLine();
return rtn;
}
public static double nextDouble(){
strictCheck();
double rtn = scan.nextDouble();
scan.nextLine();
return rtn;
}
public static float nextFloat(){
strictCheck();
float rtn = scan.nextFloat();
scan.nextLine();
return rtn;
}
public static long nextLong(){
strictCheck();
long rtn = scan.nextLong();
scan.nextLine();
return rtn;
}
public static short nextShort(){
strictCheck();
short rtn = scan.nextShort();
scan.nextLine();
return rtn;
}
public static byte nextByte(){
strictCheck();
byte rtn = scan.nextByte();
scan.nextLine();
return rtn;
}
public static boolean nextBoolean(){
strictCheck();
boolean rtn = scan.nextBoolean();
scan.nextLine();
return rtn;
}
public static String nextLine(){
strictCheck();
return scan.nextLine();
}
public static char nextCharacter(){
strictCheck();
String tmp = scan.next();
char rtnChar = tmp.charAt(0);
return rtnChar;
}
public static boolean fileExists(String fileName){
return (new File(fileName)).isFile();
}
public static String readFile(String fileName) {
String rtn = "";
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
String str;
str = in.readLine();
while (str!=null){
rtn+=str+"\n";
str = in.readLine();
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error: " + fileName);
}
return rtn;
}
public static void writeFile(String fileName, String data){
try{
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
writer.print(data);
writer.close();
} catch (IOException e) {
System.out.println("File Write Error: " + fileName);
}
}
public static int[] simpleIntFileRead(String fileName){
int[] rtnArray=null;
try {
BufferedReader in = new BufferedReader(new FileReader(fileName));
String str;
str = in.readLine();
if (str!=null){
String[] rawData = str.split(",");
rtnArray = new int[rawData.length];
for(int index = 0;index <rawData.length;index++ ){
rtnArray[index] = Integer.parseInt(rawData[index]);
}
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error: " + fileName);
}
return rtnArray;
}
public static int[][] intFileRead(String fileName){
int[][] rtnArray=null;
ArrayList<int[]> buildArray = null;
int maxValue=0;
try {
buildArray = new ArrayList<int[]>();
BufferedReader in = new BufferedReader(new FileReader(fileName));
String str;
str = in.readLine();
while (str!=null){
String[] rawData = str.split(",");
if (rawData.length>maxValue){
maxValue = rawData.length;
}
int[] tmpArray = new int[rawData.length];
for(int index = 0;index <rawData.length;index++ ){
tmpArray[index] = Integer.parseInt(rawData[index]);
}
buildArray.add(tmpArray);
str = in.readLine();
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error: " + fileName);
}
if(buildArray!=null){
int rows = buildArray.size();
rtnArray = new int[rows][maxValue];
for(int row =0;row<rows;row++){
int[] tmpArray = buildArray.get(row);
for(int col=0;col<tmpArray.length;col++){
rtnArray[row][col] =tmpArray[col];
}
}
}
return rtnArray;
}
public static void simpleIntFileWrite(String fileName, int[] data){
try{
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
String printFile="";
for(int i =0;i<data.length-1;i++){
printFile+=data[i]+",";
}
if (data.length>0){
printFile+=data[data.length-1];
}
writer.println(printFile);
writer.close();
} catch (IOException e) {
System.out.println("File Write Error: " + fileName);
}
}
public static void intFileWrite(String fileName, int[][] data){
try{
PrintWriter writer = new PrintWriter(fileName, "UTF-8");
for(int j = 0;j<data.length;j++){
String printFile="";
for(int i =0;i<data[j].length-1;i++){
printFile+=data[j][i]+",";
}
if (data[j].length>0){
printFile+=data[j][data[j].length-1];
}
if(j==(data.length-1)){
writer.print(printFile);
} else {
writer.println(printFile);
}
}
writer.close();
} catch (IOException e) {
System.out.println("File Write Error: " + fileName);
}
}
public static void drawGraph(int[] data){
JFrame graphFrame = new JFrame("Graph Frame");
graphFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=600;
int height=600;
GraphPanel graphPanel = new GraphPanel(data, width,height);
graphFrame.getContentPane().add(graphPanel);
graphPanel.setPreferredSize(new Dimension(width,height));
graphPanel.setBackground(Color.white);
graphFrame.pack();
graphFrame.setVisible(true);
graphFrame.repaint();
}
private static class GraphPanel extends JPanel {
private int[] data;
private int width;
private int height;
private int halfWidth;
private int halfHeight;
public GraphPanel(int[] data, int width, int height){
super();
this.width = width;
this.height = height;
this.halfHeight = this.height/2;
this.halfWidth = halfHeight/2;
this.data = data;
this.repaint();
}
public void paintComponent(Graphics page){
super.paintComponent(page);
page.setColor(Color.white);
page.fillRect(0,0,width,height);
page.setColor(Color.black);
if(data.length == 0 ){
page.drawString("No Data", halfHeight,halfWidth);
return;
}
int minValue = data[0];
int maxValue = data[0];
for(int element:data){
if(minValue > element){
minValue = element;
}
if(maxValue < element){
maxValue = element;
}
}
double range = maxValue - minValue;
String maxString = String.valueOf(maxValue);
String minString = String.valueOf(minValue);
int maxStringLength = page.getFontMetrics().stringWidth(maxString)+20;
int minStringLength = page.getFontMetrics().stringWidth(minString)+20;
int fontHeight = page.getFontMetrics().getHeight() + 20;
int stringLength = maxStringLength > minStringLength ? maxStringLength : minStringLength;
final int sep = stringLength > fontHeight ? stringLength : fontHeight;
page.drawLine(sep,height-sep,width,height-sep);
page.drawLine(sep,height-sep,sep,0);
int minValueYValue = height-(sep * 2);
int maxValueYValue = sep * 2;
if(data.length == 1){
drawNumber(1,halfWidth,height-sep/2, page);
final int width = 100;
final int height = (this.height-sep)- maxValueYValue;
page.setColor(Color.blue);
page.fillRect(halfWidth-width/2,maxValueYValue,width,height);
vertLabel(data[0],sep/2,maxValueYValue, sep, page);
} else {
int fullWidth = width - sep;
int fullHeight = minValueYValue - maxValueYValue;
int items = data.length;
int spaces = items + 1;
int baseSpace = fullWidth/(items*2 + spaces);
int currentX = sep + baseSpace;
final int barWidth = baseSpace*2;
int count = 1;
for(int element : data){
double scale = (maxValue - element)/range;
final int barHeight = (int)(fullHeight*(1.0-scale)) + sep;
page.setColor(Color.blue);
page.fillRect(currentX,(int)(maxValueYValue + fullHeight*scale),barWidth,barHeight);
drawNumber(count, currentX + barWidth/2,height-sep/2,page);
count++;
currentX += baseSpace*3;
}
final int numberOfLabels = 20;
final double labelScale = range/numberOfLabels;
for(count = 0; count <= numberOfLabels ; count++){
vertLabel(minValue + (int)(labelScale*count), sep/2,minValueYValue - (int)( fullHeight*count/(double)numberOfLabels),sep, page);
}
}
}
public static void vertLabel(int value, int x, int y, int sep, Graphics page){
page.setColor(Color.black);
int line = 10;
page.drawLine(sep-(line/2),y,sep+(line/2), y);
drawNumber(value,x,y,page);
}
public static void drawNumber(int value, int x, int y, Graphics page){
page.setColor(Color.black);
String str = String.valueOf(value);
int width = page.getFontMetrics().stringWidth(String.valueOf(value));
int actualX = x - width/2;
int height = page.getFontMetrics().getHeight();
int actualY = y + height/4; //The divide by 4 is needed to make this look it correct.
page.drawString(str,actualX,actualY);
}
}
// *** Magic Level 2: Graphics ***
public static JFrame frame = new JFrame("Magic Frame");
public static MagicPanel primaryPanel= new MagicPanel();
public static boolean startedDrawing = false;
public static MagicImage image1 =new MagicImage();
public static MagicImage image2 =new MagicImage();
public static MagicImage image3 =new MagicImage();
public static MagicImage image4 =new MagicImage();
public enum MagicColor {Red, Blue, Green, Black, White, Magenta, Cyan, Gray, DarkGray, Yellow, Pink};
private static void startDrawing(){
if(!startedDrawing){
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(primaryPanel);
primaryPanel.setPreferredSize(new Dimension(800,600));
primaryPanel.setBackground(Color.white);
frame.pack();
frame.setVisible(true);
startedDrawing = true;
Magic.drawRectangle(0, 0, 800, 600, "white");
}
}
private static Color convertMagicToNormalColor(MagicColor c){
switch (c){
case Red:
return Color.red;
case Blue:
return Color.blue;
case Green:
return Color.green;
case White:
return Color.white;
case Magenta:
return Color.magenta;
case Cyan:
return Color.cyan;
case Gray:
return Color.gray;
case DarkGray:
return Color.darkGray;
case Black:
return Color.black;
case Yellow:
return Color.yellow;
case Pink:
return Color.pink;
default:
return Color.black;
}
}
private static Color convertStringToNormalColor(String c){
if(c.equalsIgnoreCase("red")){
return Color.red;
} else if(c.equalsIgnoreCase("blue")) {
return Color.blue;
} else if(c.equalsIgnoreCase("green")) {
return Color.green;
} else if(c.equalsIgnoreCase("white")) {
return Color.white;
} else if(c.equalsIgnoreCase("magenta")) {
return Color.magenta;
} else if(c.equalsIgnoreCase("cyan")) {
return Color.cyan;
} else if(c.equalsIgnoreCase("gray")) {
return Color.gray;
} else if(c.equalsIgnoreCase("darkGray")) {
return Color.darkGray;
} else if(c.equalsIgnoreCase("black")) {
return Color.black;
} else if(c.equalsIgnoreCase("yellow")) {
return Color.yellow;
} else if(c.equalsIgnoreCase("pink")) {
return Color.pink;
} else {
return Color.black;
}
}
public static int drawRectangle(int x, int y, int width, int height, int red, int green, int blue){
return drawRectangle(x, y, width, height, new Color(red,green,blue));
}
public static int drawEmptyRectangle(int x, int y, int width, int height, int red, int green, int blue){
return drawEmptyRectangle(x, y, width, height, new Color(red,green,blue));
}
public static int drawOval(int x, int y, int width, int height, int red, int green, int blue){
return drawOval(x, y, width, height, new Color(red,green,blue));
}
public static int drawEmptyOval(int x, int y, int width, int height,int red, int green, int blue){
return drawEmptyOval(x, y, width, height, new Color(red,green,blue));
}
public static int drawLine(int startX, int startY, int endX, int endY, int red, int green, int blue){
return drawLine(startX, startY, endX, endY, new Color(red,green,blue));
}
public static int drawRectangle(int x, int y, int width, int height, String c){
return drawRectangle(x, y, width, height, convertStringToNormalColor(c));
}
public static int drawEmptyRectangle(int x, int y, int width, int height, String c){
return drawEmptyRectangle(x, y, width, height, convertStringToNormalColor(c));
}
public static int drawOval(int x, int y, int width, int height, String c){
return drawOval(x, y, width, height, convertStringToNormalColor(c));
}
public static int drawEmptyOval(int x, int y, int width, int height, String c){
return drawEmptyOval(x, y, width, height, convertStringToNormalColor(c));
}
public static int drawLine(int startX, int startY, int endX, int endY, String c){
return drawLine(startX, startY, endX, endY, convertStringToNormalColor(c));
}
public static int drawRectangle(int x, int y, int width, int height, MagicColor c){
return drawRectangle(x, y, width, height, convertMagicToNormalColor(c));
}
public static int drawEmptyRectangle(int x, int y, int width, int height, MagicColor c){
return drawEmptyRectangle(x, y, width, height, convertMagicToNormalColor(c));
}
public static int drawRectangle(int x, int y, int width, int height, Color c){
startDrawing();
MagicPaintObject paintObject = new MagicPaintObject(x,y,width,height,c, MagicObjectType.RECTANGLE);
int id = primaryPanel.addObject(paintObject);
primaryPanel.updatePanel();
return id;
}
public static int drawEmptyRectangle(int x, int y, int width, int height, Color c){
startDrawing();
MagicPaintObject paintObject = new MagicPaintObject(x,y,width,height,c, MagicObjectType.EMPTY_RECTANGLE);
int id = primaryPanel.addObject(paintObject);
primaryPanel.updatePanel();
return id;
}
public static int drawOval(int x, int y, int width, int height, Color c){
startDrawing();
MagicPaintObject paintObject = new MagicPaintObject(x,y,width,height,c, MagicObjectType.OVAL);
int id = primaryPanel.addObject(paintObject);
primaryPanel.updatePanel();
return id;
}
public static int drawOval(int x, int y, int width, int height, MagicColor c){
return drawOval(x, y, width, height, convertMagicToNormalColor(c));
}
public static int drawEmptyOval(int x, int y, int width, int height, Color c){
startDrawing();
MagicPaintObject paintObject = new MagicPaintObject(x,y,width,height,c, MagicObjectType.EMPTY_OVAL);
int id = primaryPanel.addObject(paintObject);
primaryPanel.updatePanel();
return id;
}
public static int drawEmptyOval(int x, int y, int width, int height, MagicColor c){
return drawEmptyOval(x, y, width, height, convertMagicToNormalColor(c));
}
public static int drawLine(int startX, int startY, int endX, int endY, Color c){
startDrawing();
MagicPaintObject paintObject = new MagicPaintObject(startX,startY,endX,endY,c, MagicObjectType.LINE);
int id = primaryPanel.addObject(paintObject);
primaryPanel.updatePanel();
return id;
}
public static int drawLine(int startX, int startY, int endX, int endY, MagicColor c){
return drawLine(startX, startY, endX, endY, convertMagicToNormalColor(c));
}
public static void updateImages(){
startDrawing();
primaryPanel.updateImages();
}
public static void clearImage1(){
image1.clear();
primaryPanel.updatePanel();
}
public static void clearImage2(){
image2.clear();
primaryPanel.updatePanel();
}
public static void clearImage3(){
image3.clear();
primaryPanel.updatePanel();
}
public static void clearImage4(){
image4.clear();
primaryPanel.updatePanel();
}
public static void clean(){
primaryPanel.updatePanel();
}
public static void setImage1(String imageName){
image1.setImage(imageName);
}
public static void setPosForImage1(int x, int y){
image1.setPos(x, y);
}
public static void setImage2(String imageName){
image2.setImage(imageName);
}
public static void setPosForImage2(int x, int y){
image2.setPos(x, y);
}
public static void setImage3(String imageName){
image3.setImage(imageName);
}
public static void setPosForImage3(int x, int y){
image3.setPos(x, y);
}
public static void setImage4(String imageName){
image4.setImage(imageName);
}
public static void setPosForImage4(int x, int y){
image4.setPos(x, y);
}
public static void moveObject(int x, int y,int id){
primaryPanel.moveObject(x,y,id);
primaryPanel.updatePanel();
}
public static void changeColor(Color newColor,int id){
primaryPanel.changeColor(newColor,id);
primaryPanel.updatePanel();
}
public static void changeDimensions(int width, int height,int id){
primaryPanel.changeDimensions(width,height,id);
primaryPanel.updatePanel();
}
public static void wait(int milliseconds){
try{
Thread.sleep(milliseconds);
} catch(Exception e) {
}
}
private enum MagicObjectType {OVAL, LINE, RECTANGLE, EMPTY_RECTANGLE, EMPTY_OVAL};
private static class MagicImage{
private Image myImage;
private boolean doesExist;
private int x;
private int y;
public MagicImage(){
doesExist= false;
}
public void setImage(String imageName){
try{
myImage = ImageIO.read(new File(imageName));
doesExist= true;
}catch (Exception e){
myImage= null;
System.out.println("OH OH! Can't find Iamge");
}
}
public void clear(){
myImage = null;
doesExist = false;
}
public void setPos(int x, int y){
this.x = x;
this.y = y;
}
public void paint(Graphics page){
if(doesExist){
page.drawImage(myImage,x,y,null);
}
}
}
private static class MagicPaintObject{
private int height, width, upperX, upperY;
private Color color;
private MagicObjectType objectType;
public void draw (Graphics page){
page.setColor(color);
switch(objectType){
case OVAL:
page.fillOval(upperX, upperY, width, height);
break;
case LINE:
page.drawLine(upperX, upperY, width, height);
break;
case RECTANGLE:
page.fillRect(upperX, upperY, width, height);
break;
case EMPTY_RECTANGLE:
page.drawRect(upperX, upperY, width, height);
break;
case EMPTY_OVAL:
page.drawOval(upperX, upperY, width, height);
break;
default:
break;
}
}
public MagicPaintObject(int x, int y, int w, int h, Color c, MagicObjectType objType){
height = h;
width = w;
upperX = x;
upperY = y;
color = c;
objectType = objType;
}
public void moveObject(int x, int y){
int xSep = width - this.upperX;
int ySep = height - this.upperY;
upperX = x;
upperY = y;
if(objectType==MagicObjectType.LINE){
width = x + xSep;
height = y + ySep;
}
}
public void changeDimensions(int width, int height){
this.width = width;
this.height = height;
}
public void changeColor(Color newColor){
this.color = newColor;
}
}
private static class MagicPanel extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private ArrayList<MagicPaintObject> objectsToDisplay;
public MagicPanel(){
objectsToDisplay = new ArrayList<MagicPaintObject>();
}
public void moveObject(int x, int y, int id){
if(id >= objectsToDisplay.size()){
System.err.println("Error: ID does not exisit");
return;
}
objectsToDisplay.get(id).moveObject(x,y);
}
public void changeColor(Color newColor, int id){
if(id >= objectsToDisplay.size()){
System.err.println("Error: ID does not exisit");
return;
}
objectsToDisplay.get(id).changeColor(newColor);
}
public void changeDimensions(int width, int height, int id){
if(id >= objectsToDisplay.size()){
System.err.println("Error: ID does not exisit");
return;
}
objectsToDisplay.get(id).changeDimensions(width,height);
}
public int addObject(MagicPaintObject paintObject){
int ID = objectsToDisplay.size();
objectsToDisplay.add(paintObject);
return ID;
}
public void paintComponent(Graphics page){
super.paintComponent(page);
for(int i=0; i<objectsToDisplay.size(); i++){
objectsToDisplay.get(i).draw(page);
}
//Draw the three images
image1.paint(page);
image2.paint(page);
image3.paint(page);
image4.paint(page);
}
public void updatePanel(){
this.repaint();
}
public void updateImages(){
Graphics page= this.getGraphics();
for(int i=0; i<objectsToDisplay.size(); i++){
objectsToDisplay.get(i).draw(page);
}
image1.paint(page);
image2.paint(page);
image3.paint(page);
image4.paint(page);
}
}
/**
* As long as this is true, you cannot use the methods in Magic outside of the main method.
* You SHOULD NOT change this unless your approved to do so by your professor.
* If you do change it, then do so using the method changeRestrictedMode() below.
*/
private static boolean shouldUseStrictMood = true;
public static void changeRestrictedMode(boolean newShouldUseStrictMood){
shouldUseStrictMood = newShouldUseStrictMood;
}
/**
* Method that checks to see if a method is called only in the main
* DO NOT call this method by anything other than something that can be directly used by the student
* If you call it too nested deeper into the call stack, then it will not work
*/
private static void strictCheck(){
if(!shouldUseStrictMood){
return;
}
// If a method is called in the main, then it should be 3 levels back and
// level 2 should be the name of the method calling this
// 0: getStackTrace
// 1: strictCheck
// 2: name of method
// 3: main
final int methodNameLevelsBack = 2;
final int mainMethodLevelsBack = 3;
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
if(stackTraceElements.length<4){
System.err.println("Hi Student, inform your teacher there is a bug in Magic.java");
System.exit(1);
}
String methodName = stackTraceElements[methodNameLevelsBack].getMethodName();
String shouldBeMain = stackTraceElements[mainMethodLevelsBack].getMethodName();
if(!shouldBeMain.equalsIgnoreCase("main")){
System.err.println("Sorry, you cannot call `"+methodName+"()` outside of `public static void main(String[] args)`");
System.err.println("Please rewrite your code. If you need help, ask your professor or a TA");
System.err.println("We're quitting now. Goodbye.");
System.exit(1);
}
}
}