1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4 import java.awt.event.ComponentAdapter;
5 import java.awt.event.ComponentEvent;
6
7
8
14 public class SpacePanel extends Component implements SpaceParent {
15
16 private static final long serialVersionUID = 1L;
17 private Matrix sm; private Matrix a; private Matrix tm; private Matrix rotator;
21 private boolean isotrophic;
22 private boolean allowRescale = true;
23 private Space space;
24 private RRect rRect = null;
25 private double left, right, top, bottom;
26
27
30 public SpacePanel() {
31 super();
32 initialize(); setBackground(Color.WHITE);
34 setBackground(Color.WHITE);
36 }
37
38
42 private final void initialize() {
43 this.space = new Space();
44 this.space.registerWindow(this);
45 this.sm = new TMatrix();
46 this.a = new TMatrix();
47 this.tm = new TMatrix();
48 this.rotator = new TMatrix();
49 this.isotrophic = true;
51 addComponentListener(new ComponentAdapter() {
52 @SuppressWarnings("synthetic-access")
53 @Override
54 public void componentResized(ComponentEvent e) {
55 if ( allowRescale ) reScale(); }
57 });
58 }
59
60
71 public void scale(double left, double bottom, double right, double top) {
72 this.left = left;
73 this.bottom = bottom;
74 this.right = right;
75 this.top = top;
76 rRect = new RRect(left, top, right, bottom);
77 reScale();
78 }
79
80
83 public final void reScale() {
84 if ( rRect == null ) return;
85 SRect sRect = new SRect(0, 0, getWidth(), getHeight());
86 this.sm = new TMatrix(rRect, sRect, isotrophic);
87 redraw();
88 }
89
90
98 public void rotate(int axis, double deg) {
99 transform(new RotMatrix(axis, deg));
100 }
101
102
111 public void move(double dx, double dy, double dz) {
112 transform(new TranslateMatrix(dx,dy,dz));
113 }
114
115
124 public void scale(double sx, double sy, double sz) {
125 this.left = this.left*sx;
126 this.right = this.right*sx;
127 this.bottom = this.bottom*sy;
128 this.top = this.top*sy;
129 transform(new ScaleMatrix(sx,sy,sz));
130 }
131
132
135 @Override
136 public Transformable transform(Matrix m) {
137 if ( m == null ) return this;
138 this.a.multiplyThis(m);
139 redraw();
140 return this;
141 }
142
143
147 public void setTransform(Matrix m) {
148 this.a = m;
149 redraw();
150 }
151
152
155 @Override
156 public void changeTransform(Matrix m) {
157 setTransform(m);
158 }
159
160
163 @Override
164 public void setRotator(Matrix m) {
165 this.rotator = m;
166 redraw();
167 }
168
169
172 @Override
173 public Matrix getRotator() {
174 return this.rotator;
175 }
176
177
181 public void setSpace(Space space) {
182 this.space = space;
183 this.space.registerWindow(this);
184 }
185
186
195 public Drawable add(Drawable shape) {
196 return this.space.add(shape);
197 }
198
199
204 public Drawable getSavedPath() {
205 return this.space.getSavedPath();
206 }
207
208
212 public void remove(Drawable shape) {
213 this.space.remove(shape);
214 }
215
216 public void removeAll() {
217 this.space.removeAll();
218 }
219
220
223 @Override
224 public void redraw() {
225 repaint(1000L);
226 }
227
228
232 public Matrix getFullTransform() {
233 return tm;
234 }
235
236
240 @Override
241 public void paint(Graphics g) {
242 g.setColor(getBackground());
243 g.fillRect(0,0,getWidth(),getHeight());
244 if ( space == null ) return;
245 sm.multiply(a,tm);
246 tm.multiplyThis(rotator);
247 this.space.draw(g, tm);
248 }
249
250
253 public void showWindow() {
254 this.setVisible(true);
255 }
256
257
261 public void setIsotrophic(boolean isotrophic) {
262 this.isotrophic = isotrophic;
263 }
264
265
269 public void setAllowRescale(boolean allowRescale) {
270 this.allowRescale = allowRescale;
271 if ( allowRescale ) reScale();
272 }
273
274
278 @Override
279 public Matrix getTransform() {
280 return a;
281 }
282
283 public double getLeft() {
284 return this.left;
285 }
286
287 public double getRight() {
288 return this.right;
289 }
290
291 public double getTop() {
292 return this.top;
293 }
294
295 public double getBottom() {
296 return this.bottom;
297 }
298 }
299