-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyPanel.java
96 lines (89 loc) · 2.43 KB
/
MyPanel.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
import javax.swing.JPanel;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class MyPanel extends JPanel
{
//**************************************properties**************************************
final int NUM =200;
ArrayList <MyShape> shapes;
ArrayList <MyShape> copyShapes;
Random rand=new Random();
int red, blue, green;
Color color;
MyShape shape;
MyLine copyLine;
MyRectangle copyRect;
MyOval copyOval;
//*****************************************constructor*************************************
public MyPanel()
{
shapes=new ArrayList <MyShape>();
copyShapes=new ArrayList <MyShape>();
}
//*****************************************methods******************************************
//create 2shapes of each type
public void createShapes()
{
for (int i=0; i<6; i++)
{
red=rand.nextInt(256);
blue=rand.nextInt(256);
green=rand.nextInt(256);
color=new Color(red, blue, green);
if(i<2)
{
shape=new MyLine (rand.nextInt(NUM), rand.nextInt(NUM), rand.nextInt(NUM), rand.nextInt(NUM), color);
shapes.add(shape);
}
else if (i>=2 && i<4)
{
shape=new MyRectangle (rand.nextInt(NUM), rand.nextInt(NUM), rand.nextInt(NUM), rand.nextInt(NUM), color, true);
shapes.add(shape);
}
else if (i>=4 && i<6)
{
shape=new MyOval (rand.nextInt(NUM), rand.nextInt(NUM), rand.nextInt(NUM), rand.nextInt(NUM), color, false);
shapes.add(shape);
}
}
}
//this method copy shapes, move thie start point and changes full property status
public void copyShapes()
{
for(int i=0; i<6; i++)
{
try {
if(shapes.get(i) instanceof MyLine)
{
copyLine=(MyLine) shapes.get(i).clone();
copyLine.move();
copyShapes.add(copyLine);
}
else if (shapes.get(i) instanceof MyRectangle)
{
copyRect=(MyRectangle) shapes.get(i).clone();
copyRect.move();
copyRect.changeFull();
copyShapes.add(copyRect);
}
else if (shapes.get(i) instanceof MyOval)
{
copyOval=(MyOval) shapes.get(i).clone();
copyOval.move();
copyOval.changeFull();
copyShapes.add(copyOval);
}
}catch (CloneNotSupportedException c) {
}
}
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
for (MyShape sh: shapes)
sh.drawShape(g);
for (MyShape csh: copyShapes)
csh.drawShape(g);
}
}