-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawPanel.java#backup
More file actions
806 lines (767 loc) · 28.6 KB
/
DrawPanel.java#backup
File metadata and controls
806 lines (767 loc) · 28.6 KB
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
/**
* DrawPanel is the Driver for my Level Development Engine. While the majority of the code here is Written by me, Jacob Waters, I have Professor David Wisneski to
* thank for the skeleton of the project. Originally this project allowed the user to draw a circle after pressing C, a Rectangle after R.
* For a copy of this original code email me jwaters8978@mpc.edu. You may wish to implement your own drawing project without all the fluff this one offers. Thanks David!
*
* Currently this program is designed to create a level for a 2D race car game. It's cheif functions are generation of Polynomials that intersect inputted points, connecting those
* Polynomials in a peicewise function, and writing them to file.
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
public class DrawPanel extends JPanel
implements MouseListener, MouseMotionListener, KeyListener
{
Thread p=new Thread();
public static final int SELECT = 1; // select mode
public static final int draw = 2; // draw mode
public static final int ERASE = 3;
public static final int merge = 4;
int mode; // select or draw mode
public static final int NONE =0; // no shape selected
public static final int polynomial=1; // Create A Polynomial
public static final int RECTANGLE = 2; // draw a rectangle
public static final int CIRCLE = 3; // draw a circle
int type; // type of figure to draw when in draw mode
int index=0;
int pIndex=-1;
int width;int height;
ArrayList<Draw> figures; // list of figures on the panel
Draw current; // the current figure selected
int startDragX, startDragY; //(x,y) of start of drag operation
int[] points=new int[0];
Polynomial[] equations=new Polynomial[0];
ArrayList<Draw> circles;
boolean testing=true;
int globalX, globalY;
Object[] totalCircles;
Level level;//testing
static String levelName=null;
static String fileName="level1";
//NormalLine normalLine;
Circle temp;
public static void main()
{
// Scanner scan=new Scanner(System.in);
// System.out.println("Would you like to read from file");
// String answer=scan.nextLine();
// if(answer.equals("yes"))
// {
// System.out.println("What is the level name?");
// levelName=scan.nextLine();
// }
// System.out.println("Before we start, what level will this be when completed?");
// fileName=scan.nextLine();
levelName="level1";
JFrame window = new JFrame();
window.setSize(1080,720);
window.setTitle(levelName);
DrawPanel panel = new DrawPanel(); // window content pane to hold the draw and tool panels
window.add(panel);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
// panel.test();
}
public void test()
{
Thread p=new Thread();
try{
int x=200;
p.sleep(2000);
while(true)
{
globalX-=25;
//temp=new Circle((double)x,level.find(x,globalX).doubleHeight(x-level.find(x,globalX).getXShift())-globalY,2.);
//temp.setColor(level.find(x,globalX).getColor());
repaint();
p.sleep(2000);
}
}
catch(Exception e)
{
}
}
public void findXTest(int inputX,int globalX, Level l)
{
int temp=l.indexOf(l.find(inputX,globalX));
if(inputX>=0&&inputX<=479)
{
System.out.println("At input "+inputX+" l.find(inputX) returned "+temp+", it should="+0+", success== "+(temp==0));
}else if(inputX>=479&&inputX<=850)
{
System.out.println("At input "+inputX+" l.find(inputX) returned "+temp+", it should="+1+", success== "+(temp==1));
}else if(inputX>=850&&inputX<=1109)
{
System.out.println("At input "+inputX+" l.find(inputX) returned "+temp+", it should="+2+", success== "+(temp==2));
}else if(inputX>=1109&&inputX<=1153)
{
System.out.println("At input "+inputX+" l.find(inputX) returned "+temp+", it should="+3+", success== "+(temp==3));
}else if(inputX>=1153&&inputX<=2389)
{
System.out.println("At input "+inputX+" l.find(inputX) returned "+temp+", it should="+4+", success== "+(temp==4));
}else if(inputX>=2389&&inputX<=3410)
{
System.out.println("At input "+inputX+" l.find(inputX) returned "+temp+", it should="+5+", success== "+(temp==5));
}
}
public DrawPanel( ){//Jacob Waters, David Wisnesky
if(levelName!=null)
{
level=new Level(new File(levelName+".txt"));//works great
int x=200;
//normalLine=new NormalLine(level.find(x,globalX), x,globalX);
//temp=new Circle((double)x,level.find(x,globalX).doubleHeight(x-level.find(x,globalX).getXShift())-globalY,2.);
//temp.setColor(level.find(x,globalX).getColor());
//I think the problem is that the polynomial I find at x may not be drawn at x, because of the xShift. I want the polynomial that globally exists at x
//not the polynomial that theoretically encompasses x(without relation to globalX
}
figures = new ArrayList<Draw>();
circles = new ArrayList<Draw>();
current = null;
mode = draw;
type = polynomial;
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
requestFocus();
}
public void paintComponent(Graphics g){//Jacob Waters, David Wisnesky
g.clearRect(0, 0, getWidth(), getHeight());
width=getWidth();height=getHeight();
// draw each figure
for (Draw d: figures){
d.globalDraw(g,globalX,globalY);
}
if(circles!=null)
for (Draw d: circles){
d.globalDraw(g,globalX,globalY);
}
if(equations!=null)
for(Polynomial p:equations)
{
p.globalDraw(g,globalX,globalY);
}
if(totalCircles!=null)
for(int i=0;i<totalCircles.length;i++)
{
if(totalCircles[i]instanceof Object[])
{
for(Circle c:((Circle[])totalCircles[i]))
{
c.globalDraw(g,globalX,globalY);
}
}
else
((Circle)totalCircles[i]).globalDraw(g,globalX,globalY);;
}
if(level!=null)
{
level.refreshBounds(globalX,getWidth());
level.globalDraw(g,globalX,globalY);
}
// if(normalLine!=null)
// {
// normalLine.globalDraw(g,globalX,globalY);
// }
if(temp!=null)
temp.globalDraw(g,globalX,globalY);
}
/**
*
* @param e This holds the data relevant to the mouse press. In this method the only values which are pulled from e are e.getX() and e.getY()
* Those are the x and y coordinates of the current mouse press.
* Due to the complexity of this method, refer to comments within the method for more understanding.
*/
public void mousePressed(MouseEvent e){//Jacob Waters, David Wisnesky
requestFocus(); // required for key events
// normalLine=new NormalLine(level.find(e.getX(),globalX), e.getX(),globalX);repaint();
if (mode == SELECT) {//Obsolete, used for moving Polygons such as circles or squares.
current = null;
for(int i=0;i<figures.size();i++)
{
if(figures.get(i).contains(e.getX(), e.getY()))
{
current = figures.get(i);
repaint();
startDragX = e.getX();
startDragY = e.getY();
index=i;
break;
}
}
for(int i=0;i<circles.size();i++)
{
if(circles.get(i).contains(e.getX(), e.getY()))
{
current = circles.get(i);
repaint();
startDragX = e.getX();
startDragY = e.getY();
index=i;
break;
}
}
} else if (mode == draw) {
// draw action
current = null;
switch (type) {
case RECTANGLE:{//draws a rectangle, interfaces with the mouseDragged method to finish drawing it.
current = new Rectangle(e.getX()+globalX, e.getY()+globalY, 0, 0);
if (current != null) {
figures.add(current);
}
break;
}
case CIRCLE:{//draws a circle
current = new Circle(e.getX(), e.getY(), 0);
if (current != null) {
figures.add(current);
}
break;
}
case polynomial:{//draws mini circles at the point clicked, and appends those x and y values to a list of points.
for(int i=0;i<points.length;i+=2)
{
if(points[i]==e.getX())
return;
}
int[] swap=new int[points.length+2];
for(int i=0;i<points.length;i++)
{
swap[i]=points[i];
}
swap[swap.length-2]=e.getX()-globalX;
swap[swap.length-1]=e.getY()-globalY;
points=swap;
if(testing)
{
circles.add(new Circle(e.getX()-2-globalX,e.getY()-2-globalY,2));
}
break;
}
}
}else if(mode==merge)//merge mode is triggered by sellecting a specific polynomial, or Level.
{
if(totalCircles!=null)//if totalCircles.length is 1, that is an invalid value because One references the sellected polynomials and the other(s) are to be chosen
if(totalCircles.length<2)
totalCircles=null;
if(totalCircles==null)
{
if(pIndex>=0)
{
double slope =new Dirivative(equations[pIndex]).height(e.getX()-equations[pIndex].getXShift()-globalX);
Object[] equalsInt=new Object[equations.length];
for(int i=0;i<equations.length;i++)
{
if(i!=pIndex)
{
equalsInt[i]=new Dirivative(equations[i]).equals(slope);
}
}
totalCircles=new Object[equalsInt.length];
for(int i=0;i<equations.length;i++)
{
if(i!=pIndex)
{
totalCircles[i]=new Circle[((int[])equalsInt[i]).length];
}
else
{
totalCircles[i]=new Circle(e.getX()-4-globalX,equations[i].height(e.getX()-equations[i].getXShift()-globalX)-4,4);
((Circle)totalCircles[i]).setColor(Color.RED);
}
}
for(int i=0;i<equations.length;i++)
{
if(i!=pIndex)
{
for(int a=0;a<(((Circle[])totalCircles[i]).length);a++)
{
((Circle[])totalCircles[i])[a]=new Circle(((int[])equalsInt[i])[a]-4,
equations[i].height(((int[])equalsInt[i])[a])-4,
4);
}
}
}
}
else if(pIndex==-2)
{
if(e.getX()>=level.getLast().getStart()+level.getLast().getXShift()+globalX&&e.getX()+level.getLast().getXShift()+globalX<=level.getLast().getStop())
{
double slope =new Dirivative(level.getLast()).height(e.getX()-level.getLast().getXShift()-globalX);
Object[] equalsInt=new Object[equations.length];
for(int i=0;i<equations.length;i++)
{
equalsInt[i]=new Dirivative(equations[i]).equals(slope);
}
totalCircles=new Object[equalsInt.length+1];
for(int i=0;i<equations.length;i++)
{
totalCircles[i]=new Circle[((int[])equalsInt[i]).length];
}
totalCircles[totalCircles.length-1]=new Circle(e.getX()-4-globalX,level.getLast().height(e.getX()-level.getLast().getXShift()-globalX)-4,4);
((Circle)totalCircles[totalCircles.length-1]).setColor(Color.RED);
for(int i=0;i<equations.length;i++)
{
for(int a=0;a<(((Circle[])totalCircles[i]).length);a++)
{
((Circle[])totalCircles[i])[a]=new Circle(((int[])equalsInt[i])[a]-4,
equations[i].height(((int[])equalsInt[i])[a])-4,
4);
}
}
}
}
}
else
{
if(pIndex>=0)
{
for(int i=0;i<totalCircles.length;i++)
{
if(i!=pIndex)
for(int a=0;a<(((Circle[])totalCircles[i]).length);a++)
{
if(((Circle[])totalCircles[i])[a].contains(e.getX()-globalX,e.getY()-globalY))
{
equations[pIndex].setStop((int)(((Circle)totalCircles[pIndex]).getX()+4-equations[pIndex].getXShift()));
equations[i].setStart((int)(((Circle[])totalCircles[i])[a].getX()+4-equations[i].getXShift()));
equations[i].changeXShift((int)((Circle)totalCircles[pIndex]).getX()-(int)(((Circle[])totalCircles[i])[a].getX()));
equations[i].changeYShift((int)((Circle)totalCircles[pIndex]).getY()-(int)(((Circle[])totalCircles[i])[a].getY()));
totalCircles=null;
circles.clear();
mode=draw;
if(level==null)
{
level=new Level(equations[pIndex],equations[i]);
if(pIndex>i)
{
remove(pIndex);
remove(i);
}
else
{
remove(i);
remove(pIndex);
}
}
else
{
}
pIndex=-1;
return;
}
}
}
}
else if(pIndex==-2)
{
for(int i=0;i<totalCircles.length-1;i++)
{
for(int a=0;a<(((Circle[])totalCircles[i]).length);a++)
{
if(((Circle[])totalCircles[i])[a].contains(e.getX()-globalX,e.getY()-globalY))
{
level.getLast().setStop((int)(((Circle)totalCircles[totalCircles.length-1]).getX()+4-level.getLast().getXShift()));
equations[i].setStart((int)(((Circle[])totalCircles[i])[a].getX()+4-equations[i].getXShift()));
equations[i].changeXShift((int)((Circle)totalCircles[totalCircles.length-1]).getX()-(int)(((Circle[])totalCircles[i])[a].getX()));
equations[i].changeYShift((int)((Circle)totalCircles[totalCircles.length-1]).getY()-(int)(((Circle[])totalCircles[i])[a].getY()));
totalCircles=null;
circles.clear();
mode=draw;
if(level==null)
{
level=new Level(equations[pIndex],equations[i]);
if(pIndex>i)
{
remove(pIndex);
remove(i);
}
else
{
remove(i);
remove(pIndex);
}
}
else
{
level.add(equations[i]);
level.setColor(Color.black);
remove(i);
}
pIndex=-1;
return;
}
}
}
}
}
}
}
public void mouseReleased(MouseEvent e){//Jacob Waters, David Wisnesky
if(current instanceof Rectangle)
{
((Rectangle)current).invert();
}
repaint();
}
public void mouseClicked(MouseEvent e){//David Wisnesky
}
public void mouseEntered(MouseEvent e){//David Wisnesky
}
public void mouseExited(MouseEvent e){////David Wisnesky
}
public void mouseDragged(MouseEvent e){//Jacob Waters, David Wisnesky
if (mode == SELECT ) {
// select and drag mode
if (current!=null){
current.move(e.getX()-startDragX, e.getY()-startDragY);
startDragX = e.getX();
startDragY = e.getY();
repaint();
}
} else if (mode == draw ){
if (current!=null){
if(type!=polynomial)
{
current.setEnd(e.getX(), e.getY());
repaint();
}
}
}
}
public void mouseMoved(MouseEvent e){//David Wisnesky
}
public void keyPressed(KeyEvent e){//Jacob Waters, David Wisnesky
int key = e.getKeyCode();
if (key == KeyEvent.VK_D ){
// delete current object. If there is one.
if(type==polynomial)
{
if(equations.length>0)
{
if(pIndex>-1)
{
remove(pIndex);
circles.clear();
pIndex=-1;
totalCircles=null;
repaint();
}
}
if(mode==merge)
{
mode=draw;
pIndex=-1;
}
}
else
{
if (current!=null) {
figures.remove(current);
current=null;
repaint();
}
}
} else if (key == KeyEvent.VK_C) {
totalCircles=null;
mode=draw;
if(pIndex>=0)
{
equations[pIndex].setColor(Color.black);
}
else if(pIndex==-2)
{
level.setColor(Color.black);
}
pIndex=-1;
repaint();
} else if (key == KeyEvent.VK_R) {
// draw a rectangle
mode=draw;
type=RECTANGLE;
} else if (key == KeyEvent.VK_S) {
// mode is select
mode = SELECT;
} else if(key == KeyEvent.VK_N)
{
save(fileName);
}else if(key == KeyEvent.VK_V){
clean();
repaint();
}
else if(key == KeyEvent.VK_P)
{
mode=draw;
type=polynomial;
if(totalCircles!=null)
{
totalCircles=null;
equations[pIndex].setColor(Color.BLACK);
pIndex=-1;
repaint();
}
}
else if(key == KeyEvent.VK_ENTER)
{
if(points.length!=0)
{
Polynomial[] swap=new Polynomial[equations.length+1];
for(int i=0;i<equations.length;i++)
{
swap[i]=equations[i];
}
swap[swap.length-1]=new Polynomial(points,-globalX,width-globalX);
points=new int[0];
equations=swap;
repaint();
}
}
else if(key==KeyEvent.VK_UP)
{
if(equations.length!=0)
{
if(pIndex==equations.length-1)
{
if(level!=null)
{
equations[equations.length-1].setColor(Color.BLACK);
level.setColor(Color.RED);
pIndex=-2;
mode=merge;
repaint();
}
else
{
pIndex=-1;
equations[equations.length-1].setColor(Color.BLACK);
mode=draw;
repaint();
}
}
else if(pIndex==-1)
{
pIndex++;
equations[pIndex].setColor(Color.RED);
repaint();
mode=merge;
}
else if(pIndex==-2)
{
level.setColor(Color.BLACK);
pIndex++;
mode=draw;
repaint();
}
else
{
pIndex++;
mode=merge;
equations[pIndex].setColor(Color.RED);
equations[pIndex-1].setColor(Color.BLACK);
repaint();
}
}
else
{
if(level!=null)
{
if(pIndex==-1)
{
pIndex=-2;
level.setColor(Color.red);
repaint();
mode=merge;
}
else if(pIndex==-2)
{
pIndex=-1;
level.setColor(Color.black);
repaint();
mode=draw;
}
}
}
}else if(key==KeyEvent.VK_DOWN)
{
if(equations.length!=0)
{
if(pIndex==0)
{
pIndex--;
equations[pIndex+1].setColor(Color.BLACK);
repaint();
}
else if(pIndex==equations.length-1)
{
equations[equations.length-1].setColor(Color.BLACK);
pIndex--;
equations[pIndex].setColor(Color.RED);
repaint();
}
else if(pIndex==-1)
{
if(level!=null)
{
pIndex--;
level.setColor(Color.RED);
repaint();
mode=merge;
}
else
{
pIndex=equations.length-1;
equations[equations.length-1].setColor(Color.RED);
mode=merge;
repaint();
}
}
else if(pIndex==-2)
{
pIndex=equations.length-1;
level.setColor(Color.BLACK);
equations[pIndex].setColor(Color.red);
repaint();
}
else
{
pIndex--;
equations[pIndex].setColor(Color.RED);
equations[pIndex+1].setColor(Color.BLACK);
repaint();
}
}
else if(level!=null)
{
if(pIndex==-2)
{
pIndex=-1;
level.setColor(Color.black);
repaint();
}
else if(pIndex==-1)
{
pIndex=-2;
level.setColor(Color.red);
repaint();
}
}
}
else if(key==KeyEvent.VK_T)
{
globalY+=10;
repaint();
}
else if(key==KeyEvent.VK_F)
{
globalX+=10;
repaint();
}
else if(key==KeyEvent.VK_G)
{
globalY-=10;
repaint();
}
else if(key==KeyEvent.VK_H)
{
globalX-=10;
repaint();
}
}
public void keyReleased(KeyEvent e){//David Wisnesky
}
public void keyTyped(KeyEvent e){//David Wisnesky
}
public void save(String levelName)//Jacob Waters
{
BufferedWriter output = null;
if(level!=null)
try
{
output = new BufferedWriter(new FileWriter(levelName+".txt",false));
for(int i=0;i<level.length();i++)
{
for(String a:level.get(i).save())
{
output.append(level);
output.newLine();
}
}
}
catch ( IOException e)
{
System.out.println(e);
}
try
{
output.close();
}
catch ( IOException e)
{
System.out.println(e);
}
}
public void save(Level level)//Jacob Waters
{
// Scanner scan=new Scanner(System.in);
// System.out.println("What is the Level Name?");
// String levelName=scan.nextLine();
BufferedWriter output = null;
try
{
output = new BufferedWriter(new FileWriter("level1.txt",false));
for(int i=0;i<level.length();i++)
{
for(String a:level.get(i).save())
{
output.append(a);
output.newLine();
}
}
}
catch ( IOException e)
{
System.out.println(e);
}
try
{
output.close();
}
catch ( IOException e)
{
System.out.println(e);
}
}
public void clean()//Jacob Waters
{
int i=0;
while(i<figures.size()){
Draw shape=figures.get(i);
if(shape.getWidth()<3||shape.getHeight()<3)
{
figures.remove(i);
}
else
{
i++;
}
}
}
public void remove(int index)//Jacob Waters
{
Polynomial[] swap=new Polynomial[equations.length-1];
for(int i=0;i<index;i++)
{
swap[i]=equations[i];
}
for(int i=index;i<swap.length;i++)
{
swap[i]=equations[i+1];
}
equations=swap;
}
}