| BasicDrawableCollection.java |
1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5 /**
6 * Luokka kuvio-oliokokoelmille.
7 * Tämä on tarkoitettu perinnän pohjaksi luotaessa uusia kuviokokoelmia.
8 * Siksi tässä on useat metodit protected-tasolla.
9 * @author Markus Kivioja
10 * @author vesal
11 * @version 4.10.2008
12 *
13 */
14 public class BasicDrawableCollection extends BasicShape {
15
16 private Drawable[] collection;
17 private Space[] spaces;
18 private int drawables, numOfSpaces;
19
20 /**
21 * Luo uuden oliokokelman
22 *
23 */
24 public BasicDrawableCollection() {
25 super();
26 this.collection = new Drawable[20];
27 this.spaces = new Space[20];
28 this.drawables = 0;
29 }
30
31 /**
32 * Rekisteröi annetun avaruuden sisältämään tämän kokoelmaa
33 * @param space avaruus joka sisältää tämän kokelman
34 */
35 public void registerSpace(Space space) {
36 if (numOfSpaces == this.spaces.length) {
37 Space[] temp = this.spaces;
38 this.spaces = new Space[this.numOfSpaces+20];
39 for (int i = 0; i < temp.length; i++) this.spaces[i] = temp[i];
40 }
41 this.spaces[numOfSpaces] = space;
42 numOfSpaces++;
43 }
44
45 /**
46 * Poistaa annetun avaruuden tämän kokoelman rekisteristä,
47 * kutsutaan kun annettu avaruus ei enää sisällä tätä kokoelmaa
48 * @param space poistettava avaruus
49 */
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; // NOPMD
62 this.numOfSpaces--;
63 }
64
65 /**
66 * Palauttaa tämän oliokokoelman
67 * @return tämä kokoelma
68 */
69 public int getDrawables() {
70 return this.drawables;
71 }
72
73
74 /**
75 * Paluattaa paikaas i oleva piirrettävän olion
76 * @param i missä paikass aoleva olio otetaan
77 * @return paikassa i oleva olio
78 */
79 protected Drawable getDrawable(int i) {
80 return collection[i];
81 }
82
83
84 /**
85 * This is only for inner purposes
86 * @param pDrawable
87 */
88 public void justAdd(Drawable pDrawable) {
89 if (this.drawables == collection.length) {
90 Drawable[] temp = this.collection;
91 this.collection = new Drawable[this.drawables+20];
92 for (int i = 0; i < temp.length; i++) this.collection[i] = temp[i];
93 }
94 this.collection[this.drawables] = pDrawable;
95 this.drawables++;
96 this.redraw();
97 }
98
99 /**
100 * This is only for inner purposes
101 * @param pDrawable
102 */
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; // NOPMD
110 this.drawables--;
111 }
112
113 /**
114 * Lisää olion tähän kokoelmaan
115 * @param pDrawable lisättävä olio
116 * @return lisätty olio
117 */
118 protected Drawable add(Drawable pDrawable) {
119 if ( pDrawable == null ) return null;
120 pDrawable.setParent(this); // Should call justAdd
121 return pDrawable;
122 }
123
124 /**
125 * Poistaa olion tästä kokoelmasta
126 * @param pDrawable poistettava olio
127 */
128 protected void remove(Drawable pDrawable) {
129 if ( pDrawable == null ) return;
130 pDrawable.setParent(null); // Should call justRemove
131 }
132
133 /**
134 * Palauttaa annetun olion indeksin tässä kokoelmassa
135 * @param pDrawable olio jonka indeksia haetaan
136 * @return annetun olion indeksi
137 */
138 protected int getIndex(Drawable pDrawable) {
139 for (int i = 0; i < this.drawables; i++) {
140 if (this.collection[i] == (pDrawable)) {
141 return i;
142 }
143 }
144 return this.drawables;
145 }
146
147 /**
148 * Piirretään koko kokoelma uudelleen.
149 * @see fi.jyu.mit.graphics.BasicShape#redraw()
150 */
151 public void redraw() {
152 super.redraw();
153 for (int i = 0; i < this.numOfSpaces; i++) this.spaces[i].redraw();
154 }
155
156
157 /**
158 * Kokoelman piirto
159 * @see fi.jyu.mit.graphics.BasicShape#drawShape(java.awt.Graphics, fi.jyu.mit.graphics.Matrix)
160 */
161 protected void drawShape(Graphics g, Matrix a) {
162 for (int i = 0; i < this.drawables; i++)
163 this.collection[i].draw(g, a);
164 }
165 }
166