1 package fi.jyu.mit.graphics;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5
6
11 public abstract class BasicShape implements Drawable {
13 private Color color;
14 private Matrix matrix;
15 private Matrix rotator;
16 private BasicDrawableCollection parent;
17
18 public BasicShape() {
19 this.matrix = null; this.rotator = null; this.color = null; }
23
24 @Override
25 public Drawable setColor(int r, int g, int b) { return setColor(new Color(r, g, b));
27 }
28
29
33 @Override
34 public Drawable setColor(Color c) { this.color = c;
36 redraw();
37 return this;
38 }
39
40
41
45 @Override
46 public Color getColor() {
47 if ( color != null ) return this.color;
48 if ( parent == null ) { return Color.BLACK; }
49 return parent.getColor();
50 }
51
52
53
59 protected void initGraphics(Graphics g) {
60 if ( color == null ) {
61 if ( parent == null ) { g.setColor(Color.BLACK); return; }
62 parent.initGraphics(g);
63 return;
64 }
65 g.setColor(this.color);
66 }
67
68
74 public Matrix getTransform(Matrix a) { if ( matrix == null ) return a;
76 if ( a == null ) return getTransform();
77 return a.multiply(matrix);
78 }
79
80
86 public Matrix getRotator(Matrix a) { if ( rotator == null ) return a;
88 if( a == null) return rotator;
89 return a.multiply(rotator);
90 }
91
92
93 @Override
94 public Matrix getTransform() { if ( matrix == null ) matrix = new Matrix();
96 return matrix;
97 }
98
99
108 public SPoint getPoint(Matrix a,double x,double y, double z) { Matrix tr = getTransform(a);
110 if ( tr != null ) return tr.transform(x, y, z);
111 return new SPoint((int)x,(int)y);
112 }
113
114
119 abstract protected void drawShape(Graphics g, Matrix a);
120
121 @Override
122 public void draw(Graphics g, Matrix a) {
123 Matrix t = getRotator(getTransform(a));
124 initGraphics(g);
125 drawShape(g,t);
126 }
127
128 @Override
129 public void setParent(BasicDrawableCollection parent) {
130 if ( this.parent == parent ) return;
131 if ( this == parent ) return; if ( this.parent != null ) this.parent.justRemove(this);
133 this.parent = parent;
134 if (parent != null) parent.justAdd(this);
135 redraw();
136 }
137
138 @Override
139 public void redraw() {
140 if ( this.parent == null ) return;
141 this.parent.redraw();
142 }
143
144 @Override
145 public Drawable transform(Matrix m) { if ( this.matrix == null ) this.matrix = new Matrix();
147 matrix.multiplyThis(m);
148 redraw();
149 return this;
150 }
151
152 @Override
153 public Drawable setTransform(Matrix m) { if ( this.matrix == m ) return this;
155 this.matrix = m;
156 redraw();
157 return this;
158 }
159
160 @Override
161 public void changeTransform(Matrix m) { setTransform(m);
163 }
164
165 @Override
166 public void setRotator(Matrix m) { this.rotator = m;
168 redraw();
169 }
170
171 @Override
172 public Matrix getRotator() { if ( this.rotator == null ) this.rotator = new Matrix();
174 return this.rotator;
175 }
176
177 @Override
178 public Drawable scale(double dx, double dy, double dz) { return transform(new ScaleMatrix(dx,dy,dz));
180 }
181
182 @Override
183 public Drawable move(double sx, double sy, double sz) { return transform(new TranslateMatrix(sx,sy,sz));
185 }
186
187 @Override
188 public Drawable rotate(int axis, double deg) { return transform(new RotMatrix(axis,deg));
190 }
191
192
193 }
194