1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5
14 public class BasicDrawableCollection extends BasicShape {
15
16 private Drawable[] collection;
17 private Space[] spaces;
18 private int drawables, numOfSpaces;
19
20
24 public BasicDrawableCollection() {
25 super();
26 this.collection = new Drawable[20];
27 this.spaces = new Space[20];
28 this.drawables = 0;
29 }
30
31
35 public void registerSpace(Space space) {
36 if (numOfSpaces == this.spaces.length) {
37 Space[] temp = new Space[this.numOfSpaces+20];
38 for (int i = 0; i < numOfSpaces; i++) temp[i] = this.spaces[i];
39 this.spaces = temp;
40 }
41 this.spaces[numOfSpaces] = space;
42 numOfSpaces++;
43 }
44
45
50 public void unRegisterSpace(Space space) {
51 int index = this.numOfSpaces;
52 for (int i = 0; i < this.numOfSpaces; i++) {
53 if (this.spaces[i] == (space)) {
54 index = i;
55 }
56 }
57 if (index == this.numOfSpaces) return;
58 for (int i = index; i < (this.numOfSpaces-1); i++) {
59 this.spaces[i] = this.spaces[i+1];
60 }
61 this.spaces[this.numOfSpaces-1] = null; this.numOfSpaces--;
63 }
64
65
69 public int getDrawables() {
70 return this.drawables;
71 }
72
73
74
79 protected Drawable getDrawable(int i) {
80 return collection[i];
81 }
82
83
84
88 public void justAdd(Drawable pDrawable) {
89 if (this.drawables == collection.length) {
90 Drawable[] temp = new Drawable[this.drawables+20];
91 for (int i = 0; i < this.drawables; i++) temp[i] = this.collection[i];
92 this.collection = temp;
93 }
94 this.collection[this.drawables] = pDrawable;
95 this.drawables++;
96 this.redraw();
97 }
98
99
103 public void justRemove(Drawable pDrawable) {
104 int start = this.getIndex(pDrawable);
105 if (start == this.drawables) return;
106 for (int i = start; i < (this.drawables-1); i++) {
107 this.collection[i] = this.collection[i+1];
108 }
109 this.collection[this.drawables-1] = null; this.drawables--;
111 }
112
113
118 protected Drawable add(Drawable pDrawable) {
119 if ( pDrawable == null ) return null;
120 pDrawable.setParent(this); return pDrawable;
122 }
123
124
128 protected void remove(Drawable pDrawable) {
129 if ( pDrawable == null ) return;
130 pDrawable.setParent(null); this.justRemove(pDrawable);
132 }
133
134
138 protected void removeAll() {
139 int drawables1 = this.drawables;
140 for (int i = 0; i < drawables1; i++) {
141 this.remove(this.collection[0]);
142 }
143 }
144
145
150 protected int getIndex(Drawable pDrawable) {
151 for (int i = 0; i < this.drawables; i++) {
152 if (this.collection[i] == (pDrawable)) {
153 return i;
154 }
155 }
156 return this.drawables;
157 }
158
159
163 @Override
164 public void redraw() {
165 super.redraw();
166 for (int i = 0; i < this.numOfSpaces; i++) this.spaces[i].redraw();
167 }
168
169
170
174 @Override
175 protected void drawShape(Graphics g, Matrix a) {
176 if ( g == null ) System.out.println("g = null");
178 if ( a == null ) System.out.println("a == null");
179 if ( this.collection == null ) System.out.println("collection == null");
180 for (int i = 0; i < this.drawables; i++) {
181 if ( this.collection[i] == null )
182 System.out.println("collection["+i+"] == null");
183 this.collection[i].draw(g, a);
184 }
185 }
186 }
187