1 rizwank 1.1 /*
2 * @(#)AWGraphApplet.java
3 * $Revision: 1.6 $ - $Author: eldy $ - $Date: 2004/01/07 22:32:03 $
4 *
5 */
6
7 import java.applet.Applet;
8 import java.awt.*;
9 import java.util.Vector;
10
11
12 public class AWGraphApplet extends Applet
13 {
14
15 public AWGraphApplet()
16 {
17 special = "Not yet defined";
18 textVertSpacing = 0;
19 b_fontsize = 11;
20 blockSpacing = 5;
21 valSpacing = 0;
22 rizwank 1.1 valWidth = 5;
23 maxLabelWidth = 0;
24 background_color = Color.white;
25 border_color = Color.white;
26 special_color = Color.gray;
27 backgraph_colorl = Color.decode("#F6F6F6");
28 backgraph_colorm = Color.decode("#EDEDED");
29 backgraph_colorh = Color.decode("#E0E0E0");
30 }
31
32 // public synchronized void init() {
33 public synchronized void start()
34 {
35 special = getParameter("special");
36 if (special == null) { special = ""; }
37
38 Log("Applet "+VERSION+" ($Revision: 1.6 $) init");
39
40 String s = getParameter("b_fontsize");
41 if (s != null) { b_fontsize = Integer.parseInt(s); }
42
43 rizwank 1.1 title = getParameter("title");
44 if (title == null) { title = "Chart"; }
45
46 s = getParameter("nbblocks");
47 if (s != null) { nbblocks = Integer.parseInt(s); }
48
49 s = getParameter("nbvalues");
50 if (s != null) { nbvalues = Integer.parseInt(s); }
51
52 s = getParameter("blockspacing");
53 if (s != null) { blockSpacing = Integer.parseInt(s); }
54 s = getParameter("valspacing");
55 if (s != null) { valSpacing = Integer.parseInt(s); }
56 s = getParameter("valwidth");
57 if (s != null) { valWidth = Integer.parseInt(s); }
58
59 s = getParameter("orientation");
60 if (s == null) { orientation = VERTICAL; }
61 else if (s.equalsIgnoreCase("horizontal")) { orientation = HORIZONTAL; }
62 else { orientation = VERTICAL; }
63 s = getParameter("barsize");
64 rizwank 1.1 if (s != null) { barsize = Integer.parseInt(s); }
65
66 s = getParameter("background_color");
67 if (s != null) { background_color = Color.decode("#"+s); }
68 s = getParameter("border_color");
69 if (s != null) { border_color = Color.decode("#"+s); }
70 s = getParameter("special_color");
71 if (s != null) { special_color = Color.decode("#"+s); }
72
73 Log("bblocks "+nbblocks);
74 Log("nbvalues "+nbvalues);
75 Log("barsize "+barsize);
76
77 font = new Font("Verdana,Arial,Helvetica", 0, b_fontsize);
78 fontb = new Font("Verdana,Arial,Helvetica", Font.BOLD, b_fontsize);
79 fontmetrics = getFontMetrics(font);
80
81 blabels = new String[nbblocks];
82 vlabels = new String[nbvalues];
83 styles = new int[nbvalues];
84 max = new float[nbvalues];
85 rizwank 1.1 colors = new Color[nbvalues];
86 values = new float[nbblocks][nbvalues];
87
88 for (int i=0; i < nbvalues; i++) {
89 parseLabel(i);
90 parseStyle(i);
91 parseColor(i);
92 parseMax(i);
93 }
94 for (int j=0; j < nbblocks; j++) {
95 parsebLabel(j);
96 parseValue(j);
97 }
98 for (int i=0; i < nbvalues; i++) {
99 if (max[i]<=0.0F) { max[i]=1.0F; }
100 Log("max["+i+"]="+max[i]);
101 }
102 }
103
104 private synchronized void Log(String s)
105 {
106 rizwank 1.1 System.out.println(getClass().getName()+" ("+special+"): "+s);
107 }
108
109 private synchronized void parsebLabel(int i)
110 {
111 String s = getParameter("b" + (i+1) + "_label");
112 if (s==null) {
113 blabels[i] = "";
114 } else {
115 blabels[i] = s;
116 }
117 maxLabelWidth = Math.max(fontmetrics.stringWidth(blabels[i]), maxLabelWidth);
118 }
119
120 private synchronized void parseLabel(int i)
121 {
122 String s = getParameter("v" + (i+1) + "_label");
123 if (s==null) {
124 vlabels[i] = "";
125 } else {
126 vlabels[i] = s;
127 rizwank 1.1 }
128 }
129
130 private synchronized void parseStyle(int i)
131 {
132 String s = getParameter("v" + (i+1) + "_style");
133 if (s == null || s.equalsIgnoreCase("solid")) {
134 styles[i] = SOLID;
135 } else if (s.equalsIgnoreCase("striped")) {
136 styles[i] = STRIPED;
137 } else {
138 styles[i] = SOLID;
139 }
140 }
141
142 private synchronized void parseColor(int i)
143 {
144 String s = getParameter("v" + (i+1) + "_color");
145 if (s != null) {
146 colors[i] = Color.decode("#"+s);
147 } else {
148 rizwank 1.1 colors[i] = Color.gray;
149 }
150 }
151
152 private synchronized void parseMax(int i)
153 {
154 String s = getParameter("v" + (i+1) + "_max");
155 if (s != null) {
156 max[i] = Float.valueOf(s).floatValue();
157 } else {
158 max[i] = 1.0F;
159 }
160 }
161
162 private synchronized void parseValue(int i)
163 {
164 String s = getParameter("b" + (i+1));
165 if (s != null) {
166 String[] as=split(s," ",0);
167 for (int j=0; j<as.length; j++) {
168 // Log("as="+as[j]);
169 rizwank 1.1 if (as[j].compareTo("?")==0) { values[i][j] = 0; }
170 else { values[i][j] = Float.parseFloat(as[j]); }
171 // Log("values["+i+"]["+j+"]="+values[i][j]);
172 }
173 }
174 }
175
176 private String[] split(String s, String c, int iStart)
177 {
178 Vector v = new Vector();
179 boolean bFin = false;
180 String sub = "";
181 int i=iStart-1;
182 int iOld = i;
183 //System.out.println("s = " + s);
184 while (!bFin) {
185 iOld = i;
186 i = s.indexOf(c, iOld+1);
187 if (i!=-1) {
188 //System.out.println("i = " + i);
189 sub = s.substring(iOld+1, i);
190 rizwank 1.1 } else {
191 sub = s.substring(iOld+1, s.length());
192 bFin=true;
193 }
194 //System.out.println("sub = " + sub);
195 v.addElement(sub);
196 }
197 String[] tabS = new String[v.size()];
198 for (i=0 ; i<v.size() ; i++) {
199 tabS[i] = (String)v.elementAt(i);
200 }
201 return tabS;
202 }
203
204 private String remove(String s, String c)
205 {
206 Vector v = new Vector();
207 boolean bFin = false;
208 String sub = "";
209 int i=-1;
210 int iOld = i;
211 rizwank 1.1 //System.out.println("s = " + s);
212 while (!bFin) {
213 iOld = i;
214 i = s.indexOf(c, iOld+1);
215 if (i!=-1) {
216 //System.out.println("i = " + i);
217 sub = s.substring(iOld+1, i);
218 } else {
219 sub = s.substring(iOld+1, s.length());
220 bFin=true;
221 }
222 //System.out.println("sub = " + sub);
223 v.addElement(sub);
224 }
225 sub = "";
226 for (i=0 ; i<v.size() ; i++) {
227 sub += (String)v.elementAt(i);
228 }
229 return sub;
230 }
231
232 rizwank 1.1 private Graphics bfr;
233 private Image img;
234
235 public void init()
236 {
237 img = createImage(this.getSize().width, this.getSize().height);
238 bfr = img.getGraphics();
239 }
240
241 public synchronized void paint(Graphics g)
242 {
243 // background and border
244 bfr.setColor(background_color);
245 bfr.fillRect(0, 0, getSize().width, getSize().height);
246 bfr.setColor(border_color);
247 bfr.drawRect(0, 0, getSize().width, getSize().height);
248
249 // draw the bars and their titles
250 if(orientation == HORIZONTAL) { paintHorizontal(bfr); }
251 else { paintVertical(bfr); }
252
253 rizwank 1.1 g.drawImage(this.img, 0, 0, this);
254 }
255
256 private synchronized void draw3DBar(Graphics g,int x, int y, float w, float h, int shift, Color color)
257 {
258 // Draw a 3D bar at pos (x,y)
259 Polygon polygon = new Polygon();
260 int width=new Float(w).intValue();
261 int height=new Float(h).intValue();
262 // Log("draw3DBar "+x+","+y+","+w+"=>"+width+","+h+"=>"+height);
263
264 polygon.addPoint(x,y);
265 polygon.addPoint(x+width,y);
266 polygon.addPoint(x+width,y-height);
267 polygon.addPoint(x,y-height);
268 g.setColor(color);
269 g.fillPolygon(polygon);
270 g.setColor(color.darker());
271 g.drawPolygon(polygon);
272 Polygon polygon2 = new Polygon();
273 polygon2.addPoint(x+width,y);
274 rizwank 1.1 polygon2.addPoint(x+width+shift,y-shift);
275 polygon2.addPoint(x+width+shift,y-shift-height);
276 polygon2.addPoint(x+width,y-height);
277 g.setColor(color.darker());
278 g.fillPolygon(polygon2);
279 g.setColor(color.darker().darker());
280 g.drawPolygon(polygon2);
281 Polygon polygon3 = new Polygon();
282 polygon3.addPoint(x,y-height);
283 polygon3.addPoint(x+width,y-height);
284 polygon3.addPoint(x+width+shift,y-height-shift);
285 polygon3.addPoint(x+shift,y-height-shift);
286 g.setColor(color);
287 g.fillPolygon(polygon3);
288 g.setColor(color.darker());
289 g.drawPolygon(polygon3);
290 }
291
292 private synchronized void paintHorizontal(Graphics g)
293 {
294 }
295 rizwank 1.1
296 private synchronized void paintVertical(Graphics g)
297 {
298 g.setColor(Color.black);
299 g.setFont(font);
300
301 int shift=10;
302 int allbarwidth=(((nbvalues*(valWidth+valSpacing))+blockSpacing)*nbblocks);
303 int allbarheight=barsize;
304 int axepointx=(getSize().width-allbarwidth)/2 - 2*shift;
305 int axepointy = getSize().height - (2*fontmetrics.getHeight()) - 2 - textVertSpacing;
306
307 int cx=axepointx;
308 int cy=axepointy;
309
310 // Draw axes
311 Polygon polygon = new Polygon();
312 polygon.addPoint(cx,cy);
313 polygon.addPoint(cx+allbarwidth+3*shift,cy);
314 polygon.addPoint(cx+allbarwidth+4*shift,cy-shift);
315 polygon.addPoint(cx+shift,cy-shift);
316 rizwank 1.1 g.setColor(backgraph_colorl);
317 g.fillPolygon(polygon);
318 g.setColor(Color.lightGray);
319 g.drawPolygon(polygon);
320 Polygon polygon2 = new Polygon();
321 polygon2.addPoint(cx,cy);
322 polygon2.addPoint(cx+shift,cy-shift);
323 polygon2.addPoint(cx+shift,cy-shift-barsize);
324 polygon2.addPoint(cx,cy-barsize);
325 g.setColor(backgraph_colorh);
326 g.fillPolygon(polygon2);
327 g.setColor(Color.lightGray);
328 g.drawPolygon(polygon2);
329 Polygon polygon3 = new Polygon();
330 polygon3.addPoint(cx+shift,cy-shift);
331 polygon3.addPoint(cx+allbarwidth+4*shift,cy-shift);
332 polygon3.addPoint(cx+allbarwidth+4*shift,cy-shift-barsize);
333 polygon3.addPoint(cx+shift,cy-shift-barsize);
334 g.setColor(backgraph_colorm);
335 g.fillPolygon(polygon3);
336 g.setColor(Color.lightGray);
337 rizwank 1.1 g.drawPolygon(polygon3);
338
339 cx+=2*shift;
340
341 // Loop on each block
342 for (int j = 0; j < nbblocks; j++) {
343
344 // Draw the block label
345 // Log("Write block j="+j+" with cx="+cx);
346 cy = getSize().height - fontmetrics.getHeight() - 3 - textVertSpacing;
347 g.setColor(Color.black);
348
349 // Check if bold or highlight
350 int bold=0; int highlight=0; String label=blabels[j];
351 if (blabels[j].indexOf(":")>0) { bold=1; label=remove(blabels[j],":"); }
352 if (blabels[j].indexOf("!")>0) { highlight=1; label=remove(blabels[j],"!"); }
353
354 if (bold==1) { g.setFont(fontb); }
355 String as[] = split(label, "\247", 0);
356 // Write background for block legend
357 if (highlight==1) {
358 rizwank 1.1 g.setColor(special_color);
359 g.fillRect(cx-Math.max(-1+blockSpacing>>1,0),cy-fontmetrics.getHeight()+2,(nbvalues*(valWidth+valSpacing))+Math.max(blockSpacing-2,0)+1,((fontmetrics.getHeight()+textVertSpacing)*as.length)+2);
360 g.setColor(Color.black);
361 }
362 // Write text for block legend
363 for (int i=0; i<as.length; i++) {
364 int cxoffset=((nbvalues*(valWidth+valSpacing))-fontmetrics.stringWidth(as[i]))>>1;
365 if (cxoffset<0) { cxoffset=0; }
366 g.drawString(as[i], cx+cxoffset, cy);
367 cy+=fontmetrics.getHeight()+textVertSpacing-1;
368 }
369 if (bold==1) { g.setFont(font); }
370
371 // Loop on each value
372 for (int i = 0; i < nbvalues; i++) {
373
374 cy = getSize().height - fontmetrics.getHeight() - 6 - textVertSpacing;
375 cy -= fontmetrics.getHeight() - 4;
376
377 // draw the shadow and bar
378 draw3DBar(g,cx,cy,valWidth,(values[j][i]*(float)barsize)/max[i],SHIFTBAR,colors[i]);
379 rizwank 1.1
380 cy = (int)((float)cy - (values[j][i] + 5F));
381 cx += (valWidth + valSpacing);
382 }
383
384 cx += blockSpacing;
385 }
386 }
387
388 public synchronized String getAppletInfo()
389 {
390 return "Title: " + title + "\n";
391 }
392
393 public synchronized String[][] getParameterInfo()
394 {
395 String[][] as = {
396 {"version", "string", "AWGraphApplet "+VERSION},
397 {"copyright", "string", "GPL"},
398 {"title", "string", title}
399 };
400 rizwank 1.1 return as;
401 }
402
403 private static final int VERTICAL = 0;
404 private static final int HORIZONTAL = 1;
405 private static final int SOLID = 0;
406 private static final int STRIPED = 1;
407 private static final int DEBUG = 3;
408 private static final int SHIFTBAR = 3;
409 private static final String VERSION = "1.1";
410
411 private String title;
412 private String special;
413 private Font font;
414 private Font fontb;
415 private FontMetrics fontmetrics;
416 private int orientation;
417 private int barsize;
418
419 private int nbblocks;
420 private String blabels[];
421 rizwank 1.1 private int b_fontsize;
422 private int blockSpacing;
423 private int textVertSpacing;
424
425 private int nbvalues;
426 private Color colors[];
427 private String vlabels[];
428 private int styles[];
429 private float max[];
430 private int valSpacing;
431 private int valWidth;
432
433 private float values[][];
434
435 private int maxLabelWidth;
436 private Color background_color;
437 private Color border_color;
438 private Color special_color;
439 private Color backgraph_colorl;
440 private Color backgraph_colorm;
441 private Color backgraph_colorh;
442 rizwank 1.1 }
443
444
445
446 // # Applet Applet.getAppletContext().getApplet( "receiver" )
447 // that accesses another Applet uniquely identified via a name you assign in the HTML <applet name= tag.
448 // # Applet Enumeration Applet.getAppletContext().getApplets()
449 // that gets you a list of all the Applets, of any class, not just yours, running on the page (including yourself).
450
|