-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSepClass.java
More file actions
113 lines (107 loc) · 2.95 KB
/
Copy pathSepClass.java
File metadata and controls
113 lines (107 loc) · 2.95 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
import java.awt.Graphics;
/**
* @author Furkan Ozev
* @since 17-01-2019
* SepClass includes static methods. drawAll, convertAll, sortShapes.
*/
public class SepClass
{
/**
* Method drawAll takes an array of Shape references and draws all shapes to an JPanel.
* @param shapes Array of Shapes Shape[]
* @param g Graphics object
* @throws MyException if type is unknown, throw object of MyException.
*/
public static void drawAll(Shape[] shapes, Graphics g) throws MyException
{
// Method drawAll takes an array of Shape references and draws all shapes to an JPanel
for(int i=0; i<shapes.length; i++)
{
if(shapes[i] != null)
shapes[i].Draw(g);
else if(shapes[i] == null) break;
else throw new MyException("Unknown variable");
}
}
/**
* Method convertAll takes an array of Shape references, converts all shapes to Polygons and returns a new array with the new shapes.
* @param shapes Array of Shapes Shape[]
* @return new_shapes Array of Shapes Shape[]
* @throws MyException if type is unknown, throw object of MyException.
*/
public static Shape[] convertAll(Shape[] shapes) throws MyException
{
// Method convertAll takes an array of Shape references, converts all shapes to Polygons and returns a new array with the new shapes.
Shape[] new_shapes = new Shape[shapes.length];
int i = 0;
for(Shape x: shapes)
{
if(x instanceof Rectangle)
{
Rectangle rec = (Rectangle) (x);
new_shapes[i] = new PolygonVect((rec));
}
else if(x instanceof Circle)
{
Circle cir = (Circle) (x);
new_shapes[i] = new PolygonVect((cir));
}
else if(x instanceof Triangle)
{
Triangle tri = (Triangle) (x);
new_shapes[i] = new PolygonVect((tri));
}
else if(x instanceof ComposedShape)
{
ComposedShape com = (ComposedShape) (x);
Shape[] temp_shapes = convertAll(com.get_inners());
for(Shape y : temp_shapes)
{
new_shapes[i] = (y);
}
}
else if(x instanceof PolygonVect)
{
PolygonVect vect = (PolygonVect) (x);
new_shapes[i] = new PolygonVect(vect);
}
else if(x instanceof PolygonDyn)
{
PolygonDyn dyn = (PolygonDyn) (x);
new_shapes[i] = new PolygonVect(dyn);
}
else if (x != null) throw new MyException("Unknown type!");
i++;
}
return new_shapes;
}
/**
* Method sortShapes takes an array of Shapes and increasingly sorts them with respect to their areas.
* @param shapes Array of Shapes Shape[]
*/
public static void sortShapes(Shape[] shapes)
{
// Method sortShapes takes an array of Shapes and increasingly sorts them with respect to their areas.
int i,j;
int size = 0;
double area1, area2;
Shape shp;
for(i=0 ; i<shapes.length; i++)
if(shapes[i] != null)
size = i+1;
for(i = 0 ; i < size ; i++)
{
for(j = 0 ; j < size ; j++)
{
area1 = shapes[i].area();
area2 = shapes[j].area();
if(area1 < area2)
{
shp = shapes[i];
shapes[i] = shapes[j];
shapes[j] = shp;
}
}
}
}
}