-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNestedShape.java
More file actions
110 lines (95 loc) · 4.02 KB
/
Copy pathNestedShape.java
File metadata and controls
110 lines (95 loc) · 4.02 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
/*
* ===============================================================================
* NestedShape.java : A shape that is a rectangle.
* NAME: JANHAV KHANNA
* YOUR UPI: JKHA639
* DATE: 20/05/21
* DESCRIPTION: This is the nested shape class which handles the values and drawing of the shape Nested.
* =============================================================================== */
import java.awt.*;
import java.util.ArrayList;
class NestedShape extends RectangleShape{
private ArrayList<Shape> nestedShapes = new ArrayList<Shape>();
private static ShapeType nextShapeType = ShapeType.NESTED;
public NestedShape(){
}
public NestedShape(int x, int y, int w, int h, int mw, int mh, Color bc, Color fc, PathType pt){
super(x, y, w, h, mw, mh, bc, fc, pt);
nextShapeType = nextShapeType.next();
createAddInnerShape(nextShapeType);
}
public NestedShape(int x, int y, int w, int h, int mw, int mh, Color bc, Color fc, PathType pt, String t){
super(x, y, w, h, mw, mh, bc, fc, pt, t);
nextShapeType = nextShapeType.next();
createAddInnerShape(nextShapeType);
}
public void createAddInnerShape(ShapeType st){
switch (st) {
case RECTANGLE:
Shape r = new RectangleShape(0, 0, super.width / 2, super.height / 2, super.width, super.height, super.borderColor, super.fillColor, PathType.BOUNCE, super.text);
r.setParent(this);
nestedShapes.add(r);
break;
case XRECTANGLE:
Shape t = new XRectangleShape(0, 0, super.width / 2, super.height / 2, super.width, super.height, super.borderColor, super.fillColor, PathType.BOUNCE, super.text);
t.setParent(this);
nestedShapes.add(t);
break;
case SQUARE:
int h = Math.min(super.width, super.height);
Shape j = new SquareShape(0, 0, h / 2, super.width, super.height, super.borderColor, super.fillColor, PathType.BOUNCE, super.text);
j.setParent(this);
nestedShapes.add(j);
break;
case OVAL:
Shape k = new OvalShape(0, 0, super.width / 2, super.height / 2, super.width, super.height, super.borderColor, super.fillColor, PathType.BOUNCE, super.text);
k.setParent(this);
nestedShapes.add(k);
break;
case NESTED:
Shape i = new NestedShape(0, 0, super.width / 2, super.height / 2, super.width, super.height, super.borderColor, super.fillColor, PathType.BOUNCE, super.text);
i.setParent(this);
nestedShapes.add(i);
break;
}
}
public Shape getShapeAt(int index){
return nestedShapes.get(index);
}
public int getSize(){
return nestedShapes.size();
}
public void draw(Painter g){
g.setPaint(Color.BLACK);
g.drawRect(super.getX(), super.getY(), super.getWidth(), super.getHeight());
g.translate(super.getX(), super.getY());
for(int i = 0; i < getSize(); i++){
Shape s = nestedShapes.get(i);
s.draw(g);
}
g.translate(-super.getX(), -super.getY());
}
public void move(){
super.move();
for(int i = 0; i < getSize(); i++){
Shape s = nestedShapes.get(i);
s.move();
}
}
public void add(Shape s){
s.setParent(this);
nestedShapes.add(s);
}
public void remove(Shape s){
int ind = nestedShapes.indexOf(s);
nestedShapes.get(ind).setParent(null);
nestedShapes.remove(ind);
}
public int indexOf(Shape s){
return nestedShapes.indexOf(s);
}
public Shape[] getChildren(){
Shape[] sh = new Shape[getSize()];
return nestedShapes.toArray(sh);
}
}