| Marker.java |
1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5 /**
6 * Luokka merkkipisteille.
7 * Oliota voi käyttää prototyyppinä luomaan vastaavanlaisia
8 * merkkejä lisää.
9 * <pre>
10 * Käyttö esimerkiksi:
11 * Marker malliMerkki = new Marker(0,0,0.25).setMaxR(5).setType(Marker.CIRCLE);
12 * ...
13 * ikkuna.add(malliMerkki.create(x,y));
14 * </pre>
15 * @author Vesa Lappalainen
16 *
17 */
18 public class Marker extends BasicShape implements Drawable {
19 /**
20 * Rajapinta markkerin piirtämiseksi
21 * @author vesal
22 * @version 19.10.2008
23 */
24 public interface MarkerInterface {
25 /**
26 * Piirtää markerin ruudulle
27 * @param g grafiikkapinta johon piirretään
28 * @param x markerin x-keskipiste skaalattuna kokonaislukuna
29 * @param y markerin y-keskipiste skaalattuna kokonaislukuna
30 * @param r markerin säden skaalattuna kokonaislukuna
31 */
32 public void draw(Graphics g,int x,int y,int r);
33 }
34
35
36 /**
37 * Valmis luokka ympyrämerkkien piirtämistä varten
38 */
39 public static class Circle implements MarkerInterface {
40 /**
41 * @see fi.jyu.mit.graphics.Marker.MarkerInterface#draw(java.awt.Graphics, int, int, int)
42 */
43 public void draw(Graphics g,int x,int y,int r) {
44 g.drawOval((int) Math.round(x-r), (int) Math.round(y-r), 2*r, 2*r);
45 }
46 }
47
48
49 /**
50 * Valmis luokka Plus-merkkien piirtämistä varten
51 */
52 public static class Plus implements MarkerInterface {
53 /**
54 * @see fi.jyu.mit.graphics.Marker.MarkerInterface#draw(java.awt.Graphics, int, int, int)
55 */
56 public void draw(Graphics g,int x,int y,int r) {
57 g.drawLine(x-r-1, y , x+r+1, y );
58 g.drawLine(x ,y+r+1, x , y-r-1);
59 }
60 }
61
62 /**
63 * Valmis luokka X-merkkien piirtämistä varten
64 */
65 public static class Cross implements MarkerInterface {
66 /**
67 * @see fi.jyu.mit.graphics.Marker.MarkerInterface#draw(java.awt.Graphics, int, int, int)
68 */
69 public void draw(Graphics g,int x,int y,int r) {
70 g.drawLine(x-r-1,y-r-1, x+r+1,y+r+1);
71 g.drawLine(x-r-1,y+r+1, x+r+1,y-r-1);
72 }
73 }
74
75
76 /** Ympyrän piirtävä markkeri */
77 public static final MarkerInterface CIRCLE = new Circle();
78 /** Plussan piirtävä markkeri */
79 public static final MarkerInterface PLUS = new Plus();
80 /** X:n piirtävä piirtävä markkeri */
81 public static final MarkerInterface CROSS = new Cross();
82
83 private MarkerInterface marker = CIRCLE;
84 private final RPoint center;
85 private double r;
86 private int maxR = Integer.MAX_VALUE;
87 private int minR = 1;
88
89 /**
90 * Luo merkkipisteen
91 * @param x merkin keskipisteen x-koordinaatti
92 * @param y merkin keskipisteen y-koordinaatti
93 * @param r ympyrän säde
94 */
95 public Marker(double x, double y, double r) {
96 super();
97 this.center = new RPoint(x, y);
98 this.r = r;
99 initialize();
100 }
101
102 /**
103 * Luo merkkipisteen
104 * @param x merkin keskipisteen x-koordinaatti
105 * @param y merkin keskipisteen y-koordinaatti
106 * @param r ympyrän säde
107 * @param type piirrettävän merkin tyyppi
108 */
109 public Marker(double x, double y, double r, MarkerInterface type) {
110 this(x,y,r);
111 marker = type;
112 }
113
114 /**
115 * Luo uuden ympyrän
116 * @param x ympyrän keskipisteen x-koordinaatti
117 * @param y ympyrän keskipisteen y-koordinaatti
118 * @param z ympyrän keskipisteen z-koordinaatti
119 * @param r ympyrän säde
120 */
121 public Marker(double x, double y, double z, double r) {
122 super();
123 this.center = new RPoint(x, y, z);
124 this.r = r;
125 initialize();
126 }
127
128 /**
129 * Luo uuden ympyrän
130 * @param x ympyrän keskipisteen x-koordinaatti
131 * @param y ympyrän keskipisteen y-koordinaatti
132 * @param z ympyrän keskipisteen z-koordinaatti
133 * @param r ympyrän säde
134 * @param type piirrettävän merkin tyyppi
135 */
136 public Marker(double x, double y, double z, double r, MarkerInterface type) {
137 this(x,y,z,r);
138 marker = type;
139 }
140
141 /**
142 * Alustaa markerin asetukset
143 */
144 private void initialize() {
145 }
146
147
148 /**
149 * Asetetaan merkille uusi säde. Negatiivinen säde
150 * tarkoittaa määrää pikseleinä. Positiivinen alueen
151 * koordinaatteina.
152 * @param r uusi säteen arvo
153 * @return tämä olio ketjuttamista varten
154 */
155 public Marker setR(double r) {
156 this.r = r;
157 redraw();
158 return this;
159 }
160
161
162 /**
163 * Asetetaan uusi tapa piirtää merkki
164 * @param type merkin piirtäjä
165 * @return tämä olio ketjuttamista varten
166 */
167 public Marker setType(MarkerInterface type) {
168 this.marker = type;
169 redraw();
170 return this;
171 }
172
173 /**
174 * Asetetaan merkin maksimikoko pikseleinä
175 * @param maxR merkin maksimikoko pikseleinä
176 * @return tämä olio ketjuttamista varten
177 */
178 public Marker setMaxR(int maxR) {
179 this.maxR = maxR;
180 redraw();
181 return this;
182 }
183
184
185 /**
186 * Asetetaan merkin maksimikoko pikseleinä
187 * @param minR merkin maksimikoko pikseleinä
188 * @return tämä olio ketjuttamista varten
189 */
190 public Marker setMinR(int minR) {
191 this.minR = minR;
192 redraw();
193 return this;
194 }
195
196
197
198 /**
199 * Piirretään merkki
200 * @see fi.jyu.mit.graphics.BasicShape#drawShape(java.awt.Graphics, fi.jyu.mit.graphics.Matrix)
201 */
202 protected void drawShape(Graphics g, Matrix a) {
203 Vector rp = new Vector(); // NOPMD, luulee vääräksi vektoriksi
204 SPoint sp = new SPoint(0,0);
205 Vector spv = new Vector();
206 double r0;
207 int cx,cy,ri = (int)Math.round(-r);
208 a.transform(center.getX(),center.getY(),center.getZ(),rp,sp);
209 cx = sp.getX();
210 cy = sp.getY();
211 if ( r >= 0 ) {
212 // a.transform(center.getX()-r,center.getY(),center.getZ(),rp,sp);
213 // ri = Math.abs(sp.getX()-cx); // TODO: tämä pyöristyy välillä väärin
214 a.transform(r,0,0,rp,spv); // TODO: ei ota perpektiiviä huomioon
215 r0 = spv.a(0);
216 a.transform(0,0,0,rp,spv);
217 ri = (int) Math.round(Math.abs(r0-spv.a(0)));
218 }
219 if ( ri > maxR ) ri = maxR;
220 if ( ri < minR ) ri = minR;
221 marker.draw(g, cx, cy, ri);
222 }
223
224
225 /**
226 * Metodi joka luo vastaavilla arvoilla olevan merkin
227 * @param x merkin keskipisteen x-koordinaatti
228 * @param y merkin keskipisteen y-koordinaatti
229 * @param z merkin keskipisteen z-koordinaatti
230 * @return uusi merkki
231 */
232 public Marker create(double x, double y, double z) {
233 Marker m = new Marker(x,y,x,r,marker);
234 m.setColor(getColor());
235 m.minR = minR;
236 m.maxR = maxR;
237 return m;
238 }
239
240 /**
241 * Metodi joka luo vastaavilla arvoilla olevan merkin
242 * @param x merkin keskipisteen x-koordinaatti
243 * @param y merkin keskipisteen y-koordinaatti
244 * @return uusi merkki
245 */
246 public Marker create(double x, double y) {
247 return create(x,y,0);
248 }
249
250 }
251