Source code: org/fudaa/ebli/graphe/Axe.java
1 /*
2 * @file Axe.java
3 * @creation 1999-07-29
4 * @modification $Date: 2001/09/17 16:46:53 $
5 * @license GNU General Public License 2
6 * @copyright (c)1998-2001 CETMEF 2 bd Gambetta F-60231 Compiegne
7 * @mail devel@fudaa.org
8 */
9
10 package org.fudaa.ebli.graphe;
11
12 import java.awt.*;
13 import java.util.Vector;
14
15 /**
16 * Etiquette
17 *
18 * @version $Revision: 1.11 $ $Date: 2001/09/17 16:46:53 $ by $Author: deniger $
19 * @author Guillaume Desnoix
20 * @see Graphe
21 */
22 public class Axe
23 {
24 public static boolean DEBUG=BGraphe.DEBUG;
25
26 public String titre;
27 public boolean visible;
28 public String unite;
29 public boolean vertical;
30 public boolean graduations;
31 public boolean grille;
32 public int ecart;
33 public double minimum;
34 public double maximum;
35 public double pas;
36 public Vector etiquettes;
37 public Aspect aspect;
38
39 public Axe()
40 {
41 titre="";
42 visible=true;
43 unite="";
44 vertical=false;
45 graduations=false;
46 grille=false;
47 ecart=50;
48 minimum=0.;
49 maximum=10.;
50 pas=0.;
51 etiquettes=new Vector(0,1);
52 aspect =new Aspect();
53 aspect.contour=Color.black;
54 }
55
56 public static Axe parse(Lecteur lin)
57 {
58 Axe r=new Axe();
59
60 String t=lin.get();
61 if(t.equals("axe")) t=lin.get();
62 if(!t.equals("{")) System.err.println("Erreur de syntaxe: "+t);
63
64 t=lin.get();
65 while(!t.equals("")&&!t.equals("}"))
66 {
67 if(t.equals("titre" )) r.titre =lin.get();
68 else if(t.equals("unite" )) r.unite =lin.get();
69 else if(t.equals("orientation")) r.vertical =lin.get().equals("vertical");
70 else if(t.equals("graduations")) r.graduations=lin.get().equals("oui");
71 else if(t.equals("grille" )) r.grille =lin.get().equals("oui");
72 else if(t.equals("ecart" )) {
73 try {
74 r.ecart=Integer.parseInt(lin.get());
75 } catch( NumberFormatException e ) {}
76 }
77 else if(t.equals("minimum" )) r.minimum =Double.valueOf(lin.get()).doubleValue();
78 else if(t.equals("maximum" )) r.maximum =Double.valueOf(lin.get()).doubleValue();
79 else if(t.equals("pas" )) r.pas =Double.valueOf(lin.get()).doubleValue();
80 else if(t.equals("etiquettes" )) r.etiquettes =Etiquette.parse(lin);
81 else if(t.equals("visible" )) r.visible =lin.get().equals("oui");
82 else if(t.equals("aspect" )) r.aspect =Aspect.parse(lin);
83 else System.err.println("Erreur de syntaxe: "+t);
84
85 t=lin.get();
86 }
87
88 return r;
89 }
90
91 void dessine(Graphics g,int x, int y, int w, int h)
92 {
93 if( !visible ) return;
94
95 FontMetrics fm = g.getFontMetrics();
96 String t;
97
98 if(pas==0.)
99 {
100 pas=Math.ceil(Math.log(maximum-minimum)/Math.log(10.)-1.5);
101 if( DEBUG ) System.err.println("PASTMP = "+pas);
102 pas=Math.pow(10.,pas);
103 if( DEBUG ) System.err.println("PAS = "+pas);
104 }
105 // BUG: il arrive que pas ressorte avec la valeur 0, d'ou boucle infinie
106 // dans le for qui suit!!
107 // -> hack temporaire
108 if( pas==0. ) pas=1.;
109
110 g.setColor(aspect.contour);
111 int rc=aspect.contour.getRed();
112 int gc=aspect.contour.getGreen();
113 int bc=aspect.contour.getBlue();
114 int incr=Math.max(Math.max((200-rc), (200-gc)), (200-bc));
115 Color lightfg=new Color(Math.min(255, rc+incr/5), Math.min(255, gc+incr/5), Math.min(255, bc+incr/5));
116 Color superlightfg=new Color(Math.min(255, rc+incr), Math.min(255, gc+incr), Math.min(255, bc+incr));
117
118 if(graduations||grille)
119 for(double s=minimum;s<=maximum;s+=pas)
120 {
121 g.setColor(lightfg);
122 // HACK: NumberFormat (jdk1.1)
123 java.text.NumberFormat nf=java.text.NumberFormat.getInstance(java.util.Locale.US);
124 nf.setMaximumFractionDigits(2);
125 nf.setGroupingUsed(false);
126 if(vertical)
127 {
128 int ye=y+h-(int)((double)h*((s-minimum)/(maximum-minimum)));
129 if(graduations)
130 {
131 g.drawLine(x-2,ye,x,ye);
132 t=""+nf.format(s);
133 g.drawString(t,x-2-fm.stringWidth(t),ye+4);
134 }
135 if((s>minimum)&&grille)
136 {
137 g.setColor(superlightfg);
138 g.drawLine(x,ye,x+w,ye);
139 }
140 }
141 else
142 {
143 int xe=x+(int)((double)w*((s-minimum)/(maximum-minimum)));
144 if(graduations)
145 {
146 g.drawLine(xe,y-1,xe,y+1);
147 t=""+nf.format(s);
148 g.drawString(t,xe-fm.stringWidth(t)/2,y+12);
149 }
150 if((s>minimum)&&grille)
151 {
152 g.setColor(superlightfg);
153 g.drawLine(xe,y,xe,y-h);
154 }
155 }
156 }
157
158 t=titre;
159 if(!unite.equals("")) t+=" ["+unite+"]";
160
161 g.setColor(aspect.contour);
162 if(vertical)
163 {
164 g.drawLine(x-1,y,x-1,y+h-1);
165 g.drawLine(x-1,y,x-3,y+2);
166 g.drawLine(x-1,y,x+1,y+2);
167 g.drawString(t,x-fm.stringWidth(t),y-6);
168 }
169 else
170 {
171 g.drawLine(x,y,x+w-1,y);
172 g.drawLine(x+w-1,y,x+w-3,y-2);
173 g.drawLine(x+w-1,y,x+w-3,y+2);
174 g.drawString(t,x+w+3,y+2);
175 }
176
177 for(int i=0;i<etiquettes.size();i++)
178 {
179 Etiquette o=(Etiquette)etiquettes.elementAt(i);
180
181 if(vertical)
182 {
183 int ye=y+h-(int)((double)h*((o.s-minimum)/(maximum-minimum)));
184 g.drawLine(x-2,ye,x,ye);
185 g.drawString(o.titre,x+2,ye+4);
186 }
187 else
188 {
189 int xe=x+(int)((double)w*((o.s-minimum)/(maximum-minimum)));
190 g.drawLine(xe,y-1,xe,y+1);
191 g.drawString(o.titre,xe-fm.stringWidth(o.titre)/2,y-5);
192 }
193 }
194 }
195 }
196