1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5
15 public class Space {
16
17 private final DrawableCollection objects;
18 private SpaceParent[] windows;
19 private int numOfWins;
20
21
24 public Space() {
25 this.objects = new DrawableCollection();
26 this.objects.registerSpace(this);
27 this.windows = new SpaceParent[20];
28 this.numOfWins = 0;
29 }
30
31
35 public void registerWindow(SpaceParent window) {
36 if (numOfWins == this.windows.length) {
37 SpaceParent[] temp = this.windows;
38 this.windows = new SpaceParent[this.numOfWins+20];
39 for (int i = 0; i < temp.length; i++) this.windows[i] = temp[i];
40 }
41 this.windows[numOfWins] = window;
42 numOfWins++;
43 }
44
45
50 public void unRegisterWindow(Window window) {
51 int index = this.numOfWins;
52 for (int i = 0; i < this.numOfWins; i++) {
53 if (this.windows[i] == (window)) {
54 index = i;
55 }
56 }
57 if (index == this.numOfWins) return;
58 for (int i = index; i < (this.numOfWins-1); i++) {
59 this.windows[i] = this.windows[i+1];
60 }
61 this.windows[this.numOfWins-1] = null; this.numOfWins--;
63 }
64
65
70 public Drawable getSavedPath() {
71 return this.objects;
72 }
73
74
77 public void redraw() {
78 for (int i = 0; i < this.numOfWins; i++) this.windows[i].redraw();
79 }
80
81
86 public void draw(Graphics g, Matrix a) {
87 this.objects.draw(g, a);
88 }
89
90
99 public Drawable add(Drawable shape) {
100 objects.add(shape);
101 redraw();
102 return shape;
103 }
104
105
109 public void remove(Drawable shape) {
110 objects.remove(shape);
111 redraw();
112 }
113
114
118 public void removeAll() {
119 objects.removeAll();
120 redraw();
121 }
122 }
123