1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5
10 public abstract class BasicShape implements Drawable, Transformable {
12 private Color color;
13 private Matrix matrix;
14 private Matrix rotator;
15 private BasicDrawableCollection parent;
16
17 public BasicShape() {
18 this.matrix = null; this.rotator = null; this.color = null; }
22
23 public Drawable setColor(int r, int g, int b) { return setColor(new Color(r, g, b));
25 }
26
27
31 public Drawable setColor(Color c) { this.color = c;
33 redraw();
34 return this;
35 }
36
37
38
42 public Color getColor() {
43 if ( color != null ) return this.color;
44 if ( parent == null ) { return Color.BLACK; }
45 return parent.getColor();
46 }
47
48
49
55 protected void initGraphics(Graphics g) {
56 if ( color == null ) {
57 if ( parent == null ) { g.setColor(Color.BLACK); return; }
58 parent.initGraphics(g);
59 return;
60 }
61 g.setColor(this.color);
62 }
63
64
70 public Matrix getTransform(Matrix a) { if ( matrix == null ) return a;
72 if ( a == null ) return getTransform();
73 return a.multiply(matrix);
74 }
75
76
82 public Matrix getRotator(Matrix a) { if ( rotator == null ) return a;
84 if( a == null) return rotator;
85 return a.multiply(rotator);
86 }
87
88
89 public Matrix getTransform() { if ( matrix == null ) matrix = new Matrix();
91 return matrix;
92 }
93
94
103 public SPoint getPoint(Matrix a,double x,double y, double z) { Matrix tr = getTransform(a);
105 if ( tr != null ) return tr.transform(x, y, z);
106 return new SPoint((int)x,(int)y);
107 }
108
109
114 abstract protected void drawShape(Graphics g, Matrix a);
115
116 public void draw(Graphics g, Matrix a) {
117 Matrix t = getRotator(getTransform(a));
118 initGraphics(g);
119 drawShape(g,t);
120 }
121
122 public void setParent(BasicDrawableCollection parent) {
123 if ( this.parent == parent ) return;
124 if ( this == parent ) return; if ( this.parent != null ) this.parent.justRemove(this);
126 this.parent = parent;
127 parent.justAdd(this);
128 redraw();
129 }
130
131 public void redraw() {
132 if ( this.parent == null ) return;
133 this.parent.redraw();
134 }
135
136 public Drawable transform(Matrix m) { if ( this.matrix == null ) this.matrix = m;
138 else matrix.multiplyThis(m);
139 redraw();
140 return this;
141 }
142
143 public Drawable setTransform(Matrix m) { if ( this.matrix == m ) return this;
145 this.matrix = m;
146 redraw();
147 return this;
148 }
149
150 public void changeTransform(Matrix m) { setTransform(m);
152 }
153
154 public void setRotator(Matrix m) { this.rotator = m;
156 redraw();
157 }
158
159 public Matrix getRotator() { if ( this.rotator == null ) this.rotator = new Matrix();
161 return this.rotator;
162 }
163
164 public Drawable scale(double dx, double dy, double dz) { return transform(new ScaleMatrix(dx,dy,dz));
166 }
167
168 public Drawable move(double sx, double sy, double sz) { return transform(new TranslateMatrix(sx,sy,sz));
170 }
171
172 public Drawable rotate(int axis, double deg) { return transform(new RotMatrix(axis,deg));
174 }
175
176
177 }
178