Source code: graph/Markers.java
1 package graph;
2
3 import java.awt.*;
4 import java.util.*;
5 import java.lang.*;
6 import java.io.StreamTokenizer;
7 import java.io.InputStream;
8 import java.io.IOException;
9 import java.net.URL;
10
11
12 /*
13 **************************************************************************
14 **
15 ** Class graph.Marker
16 **
17 **************************************************************************
18 ** Copyright (C) 1996 Leigh Brookshaw
19 **
20 ** This program is free software; you can redistribute it and/or modify
21 ** it under the terms of the GNU General Public License as published by
22 ** the Free Software Foundation; either version 2 of the License, or
23 ** (at your option) any later version.
24 **
25 ** This program is distributed in the hope that it will be useful,
26 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ** GNU General Public License for more details.
29 **
30 ** You should have received a copy of the GNU General Public License
31 ** along with this program; if not, write to the Free Software
32 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33 **************************************************************************
34 ** Modified by Jim Cochrane, last changed in July, 2000
35 **************************************************************************
36 **
37 ** class Marker extends Object
38 ** This class is designed to help install and manipulate
39 ** markers
40 **
41 *************************************************************************/
42
43
44 /**
45 * This class installs, manipulates and draws markers.
46 * Markers are stroked using the line drawing method of the class Graph.
47 * This means that any stroked figure can become a marker.
48 *
49 * @version $Revision: 2.4 $, $Date: 2001/05/29 09:36:38 $
50 * @author Leigh Brookshaw
51 */
52
53 public class Markers extends Object {
54
55 /*
56 **********************
57 **
58 ** Protected Variables
59 **
60 *********************/
61
62 /**
63 * index of the last marker loaded
64 */
65 protected int last;
66 /**
67 * maximum number of markers allowed
68 */
69 protected int max = 10;
70 /**
71 * An array of vectors. Each element in the array contains the vertex
72 * vectors for a marker. Marker 1 is at element vert[0].
73 */
74 protected Vector vert[];
75
76 /*
77 *******************
78 **
79 ** Constructors
80 **
81 ******************/
82 /**
83 * The class contructor
84 */
85 public Markers() {
86 last = 0;
87 vert = new Vector[max];
88 }
89
90 /**
91 * Instantiate the Marker class and load marker definitions from the parsed URL.
92 * The format of the file is easily worked out from the
93 * default marker file <a href="marker.txt">marker.txt</a>.
94 *
95 * @param file The URL of the data file to read
96 * @exception IOException if there is an error with the IO stream.
97 */
98 public Markers(URL file) throws IOException {
99
100 this();
101
102 LoadMarkers(file);
103 }
104
105 /**
106 * Add the definition of a new marker. The arrays contain the vertex
107 * points of the marker. The boolean array is used to define a relative draw
108 * or move to the vertex.
109 * The first vertex should always be a move (The boolean array is "true"
110 * for a relative draw.
111 *
112 * @param m The index of the marker. The first marker has index 1.
113 * @param n The number of vertices required to stroke the marker.
114 * @param draw Boolean array containing relative move/draw instructions.
115 * "true" is a draw.
116 * @param x Integer array containing the pixel x position of the vertices.
117 * All positions are relative to the center of the marker.
118 * @param y Integer array containing the pixel y postions of the vertices.
119 */
120 public void AddMarker( int m, int n, boolean draw[], int x[], int y[]) {
121 MarkerVertex v;
122 int i;
123
124 if(m < 1 || m > max ) return;
125 if( n <= 0 ) return;
126
127 m--;
128 last = m;
129 vert[m] = new Vector();
130
131 for(i=0; i<n; i++) {
132 v = new MarkerVertex();
133
134 v.draw = draw[i];
135 v.x = x[i];
136 v.y = y[i];
137
138 vert[m].addElement(v);
139 }
140
141 }
142 /**
143 * Add the definition of a new marker. The new marker is appended onto
144 * the marker list. The center of the marker is assumed to be at (0,0).
145 * @param n number of move/draw commands
146 * @param draw <i>true</i> if the point is to drawn to, <i>false</i> if
147 * the point is to be moved to.
148 * @param x X pixel to move/draw to.
149 * @param x Y pixel to move/draw to.
150 */
151 public void AddMarker(int n, boolean draw[], int x[], int y[]) {
152 AddMarker(last+1,n,draw,x,y);
153 }
154
155 /**
156 * Delete the marker with the given index. the first marker has index 1.
157 * @param n The index of the marker to delete. Markers start at index 1.
158 */
159 public void DeleteMarker( int n ) {
160
161 if(n<1 || n>max) return;
162 vert[n-1] = null;
163
164 }
165 /**
166 * Clear All markers.
167 */
168 public void ClearMarkers() {
169 int i;
170
171 if(last == 0) return;
172 for(i=0; i<max; i++) {
173 vert[i] = null;
174 }
175
176 last = 0;
177
178 }
179 /**
180 * This method reads the marker file and loads the marker definitions.
181 * The format of the file is simple. The following are the keywords.
182 * <dl>
183 * <dt><b>start</b>
184 * <dd> starts a new marker definition.
185 * <dt><b>end</b>
186 * <dd> ends a marker definition.
187 * <dt><b>m x y</b>
188 * <dd> move to position x,y
189 * <dt><b>l x y</b>
190 * <dd> line to position x,y
191 * </dl>
192 * All line drawing is relative to the previous position. The center
193 * of the marker is assumed to be at (0,0).
194 * As always blank lines are ignored and comments begin with a # character.
195 *
196 * @param file URL of file to load
197 * @exception IOException If there is an IO error
198 */
199 public void LoadMarkers(URL file) throws IOException {
200 InputStream is;
201 StreamTokenizer st;
202 MarkerVertex v;
203
204 is = file.openStream();
205
206 st = new StreamTokenizer(is);
207 st.eolIsSignificant(true);
208 st.commentChar('#');
209
210 scan:
211 while (true) {
212 switch (st.nextToken()) {
213 default:
214 break scan;
215 case StreamTokenizer.TT_EOL:
216 break;
217 case StreamTokenizer.TT_WORD:
218
219 if ("start".equals(st.sval)) {
220 vert[last] = new Vector();
221 } else
222 if ("end".equals(st.sval)) {
223 last++;
224 } else
225 if ("m".equals(st.sval)) {
226 v = new MarkerVertex();
227 v.draw = false;
228 if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
229 v.x = (int)st.nval;
230 if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
231 v.y = (int)st.nval;
232 vert[last].addElement(v);
233 }
234 }
235 } else
236 if ("l".equals(st.sval)) {
237 v = new MarkerVertex();
238 v.draw = true;
239 if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
240 v.x = (int)st.nval;
241 if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
242 v.y = (int)st.nval;
243 vert[last].addElement(v);
244 }
245 }
246 }
247 break;
248 }
249
250
251 }
252
253 is.close();
254
255 }
256
257 /**
258 * draw the marker
259 * @param g Graphics context
260 * @param m Index of the marker to draw
261 * @param scale scale factor. All coordinates are multiplied by this factor.
262 * @param x Coordinate where to draw the marker
263 * @param y Coordinate where to draw the marker
264 */
265 public void draw(Graphics g, int m, double scale, int x, int y) {
266 int i;
267 MarkerVertex mv;
268 int x0 = x, x1 = x, y0 = y, y1 = y;
269 Vector v;
270
271
272 if(m < 1 || m > max ) return;
273 if(scale <= 0) return;
274
275 v = vert[m-1];
276 if( v == null) return;
277
278 for (i=0; i<v.size(); i++) {
279 mv = (MarkerVertex)v.elementAt(i);
280
281 if( mv.draw ) {
282 x1 = x + (int)(mv.x*scale);
283 y1 = y + (int)(mv.y*scale);
284
285 g.drawLine(x0,y0,x1,y1);
286
287 x0 = x1;
288 y0 = y1;
289 } else {
290 x0 = x + (int)(mv.x*scale);
291 y0 = y + (int)(mv.y*scale);
292
293 }
294
295 }
296
297 }
298 }
299
300
301 /**
302 * This class is a structure class. It defines one vertex of the marker.
303 */
304
305 class MarkerVertex extends Object {
306 boolean draw;
307 int x;
308 int y;
309 }