KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > swing > JContourPlot


1 package JSci.swing;
2
3 import java.awt.*;
4 import javax.swing.*;
5 import JSci.awt.*;
6
7 /**
8 * A contour plot Swing component.
9 * @author Daniel Lemire
10 */

11 public final class JContourPlot extends JDoubleBufferedComponent implements ColorScheme {
12     private float data[][];
13     private double min, max;
14     private int deltay;
15   private ColorScheme CS;
16       //modifié
17
private double[] deltax;
18       //modifié
19
private float c1=1;
20     private float c2=1;
21     private float c3=1;
22     private int Contourx=2;
23     private int Contoury=2;
24     private int largeurMax;
25     private int hauteurMax;
26   //modifié
27
private int ColorScaleWidth=40;
28   private String JavaDoc MaxString, MinString, MiddleString;
29
30         /**
31         * Constructs a contour plot.
32         * @param z an array of the form <code>z[i][j] = f(x<sub>i</sub>, y<sub>j</sub>)</code>.
33         */

34     public JContourPlot(double z[][]) {
35                 super();
36                 CS=this;
37                 setBackground(Color.white);
38                 setData(z);
39                 setContourX(1);
40                 setContourY(1);
41     }
42         /**
43         * Sets the data plotted by this JContourPlot to the specified data.
44         */

45     public void setData(double feed[][]) {
46     //invert the rows
47
double[][] array=new double[feed.length][];
48                 for(int k=0;k<array.length;k++) {
49                         array[k]=feed[array.length-k-1];
50                 }
51         min=array[0][0];
52         max=array[0][0];
53         data=new float[array.length][];
54         for(int k=0;k<array.length;k++) {
55             data[k]=new float[array[k].length];
56             for(int l=0;l<array[k].length;l++) {
57                 if(array[k][l]>max)
58                     max=array[k][l];
59                 if(array[k][l]<min)
60                     min=array[k][l];
61             }
62         }
63         if(max==min) {
64             for(int k=0;k<array.length;k++) {
65                 for(int l=0;l<array[k].length;l++) {
66                     data[k][l]=1;
67                 }
68             }
69         } else {
70             Double JavaDoc D;
71             for(int k=0;k<array.length;k++) {
72                 for(int l=0;l<array[k].length;l++) {
73                     D=new Double JavaDoc(1-(array[k][l]-min)/(max-min));
74                     data[k][l]=D.floatValue();
75                 }
76             }
77         }
78
79     //modifié
80
int precision=getPrecision(max-min);
81     MaxString=Double.toString(round(max,precision));
82     MinString=Double.toString(round(min,precision));
83     MiddleString=Double.toString(round((max+min)/2.0,precision));
84     //modifié
85
rescale();
86     }
87
88   private int getPrecision (double d) {
89     d=Math.abs(d);
90     int ans=0; double compare=1;
91     if(d<1) {
92       while(compare>d){
93         compare/=10;
94         ans--;
95       }
96       return(ans);
97     }
98     while(compare<d) {
99       compare*=10;
100       ans++;
101     }
102     return(ans);
103   }
104
105   private double round(double d,int k) {
106     if(d==0)
107       return(0);
108     double sign=d/Math.abs(d);
109     d=Math.abs(d);
110     if(k<0) {
111       int k1=k;
112       while(k1<0) {
113         k1++;
114         d*=10;
115       }
116       d=Math.round(d);
117       while(k<0) {
118         k++;
119         d/=10;
120       }
121       return(d*sign);
122     }
123     int k1=k;
124     while(k1<0) {
125       k1++;
126       d/=10;
127     }
128     d=Math.round(d);
129     while(k<0) {
130       k++;
131       d*=10;
132     }
133     return(d*sign);
134
135   }
136         /**
137         * Returns the preferred size of this component.
138         */

139     public Dimension getPreferredSize() {
140         return getMinimumSize();
141     }
142         /**
143         * Returns the minimum size of this component.
144         */

145     public Dimension getMinimumSize() {
146         int widthMax=data[0].length;
147         for(int k=1;k<data.length;k++) {
148             if(data[k].length>widthMax)
149                 widthMax=data[k].length;
150         }
151         return new Dimension(widthMax+3*Contourx+ColorScaleWidth+5,(int)Math.max(data.length*2+2*Contoury,100));
152     }
153         /**
154         * There is a box around the graph, this
155         * sets how wide it will be in the horizontal direction.
156         * @param k width (in pixel)
157         * @exception IllegalArgumentException if the width is not at least one.
158         */

159     public void setContourX(int k) {
160         if(k<1)
161                         throw new IllegalArgumentException JavaDoc("This parameter must be greater than 1 : "+k+" < 1");
162         Contourx=k;
163     }
164         /**
165         * There is a box around the graph, this
166         * sets how wide it will be in the vertical direction.
167         * @param k width (in pixel).
168         * @exception IllegalArgumentException if the width is not at least one.
169         */

170     public void setContourY(int k) {
171         if(k<1)
172             throw new IllegalArgumentException JavaDoc("This parameter must be greater than 1 : "+k+" < 1");
173         Contoury=k;
174     }
175     public void setBounds(int x,int y,int width,int height) {
176         super.setBounds(x,y,width,height);
177         rescale();
178     }
179
180     private void rescale() {
181     int thiswidth=getWidth();
182     int thisheight=getHeight();
183     Dimension s=getMinimumSize();
184     thiswidth=Math.max(thiswidth,s.width);
185     thisheight=Math.max(thisheight,s.height);
186         int largeur=thiswidth-3*Contourx;
187         int hauteur=thisheight-2*Contoury-ColorScaleWidth;
188         deltay=(int) Math.floor(hauteur/data.length);
189         hauteurMax=deltay*data.length;
190       //modifié
191
deltax=new double[data.length];
192       //modifié
193
largeurMax=0;
194     int MaxNumberOfElements=data[0].length;
195     for(int k=1;k<data.length;k++) {
196       MaxNumberOfElements=Math.max(data[k].length,MaxNumberOfElements);
197     }
198     int UsableWidth=(int)Math.floor(largeur/(double)MaxNumberOfElements)*MaxNumberOfElements;
199         for(int k=0;k<data.length;k++) {
200             deltax[k]=UsableWidth/(double)data[k].length;
201       largeurMax=Math.max(largeurMax,(int)Math.floor(data[k].length*deltax[k]));
202         }
203         redraw();
204     }
205
206         /**
207         * Set the color for the contour of the graph.
208         * @exception IllegalArgumentException if
209         * one of the parameters is not between 0 and 1.
210         * @exception IllegalArgumentException if
211         * all three arguments are set to zero.
212         */

213     public void setColor(float x1,float x2, float x3) {
214         if((x1<0)||(x1>1)||(x2<0)||(x2>1)||(x3<0)||(x3>1)) {
215             throw new IllegalArgumentException JavaDoc("Incorrect parameters : "+x1+", "+x2+", "+x3);
216         }
217     if(x1+x2+x3==0) {
218       throw new IllegalArgumentException JavaDoc("You have chosen black at the specified color. This would generate a completly black graph. Please choose another color.");
219     }
220         c1=x1;c2=x2;c3=x3;
221     }
222
223   public Color getColor(float f) {
224     if((f<0)||(f>1))
225       throw new IllegalArgumentException JavaDoc("Color are given for values between 0 and 1 : "+f);
226     return(Color.getHSBColor(f,1f,1f));
227   }
228
229   public void setColorScheme (ColorScheme cs) {
230     CS=cs;
231   }
232         /**
233         * Paint the graph.
234         */

235     protected void offscreenPaint(Graphics g) {
236     int FontHeight=g.getFontMetrics(g.getFont()).getHeight();
237         g.setColor(new Color(1f-c1,1f-c2,1f-c3));
238         g.drawRect(Contourx+ColorScaleWidth,Contoury-1,largeurMax,hauteurMax);
239         g.drawRect(Contourx-1,Contoury-1,ColorScaleWidth,hauteurMax);
240     g.setClip(Contourx,Contoury,ColorScaleWidth-1,hauteurMax-1);
241     //double step255=hauteurMax/255;
242
for(float y=0;y<hauteurMax;y+=hauteurMax/255.0f) {
243       g.setColor(CS.getColor(y/(float)hauteurMax));
244       g.fillRect(Contourx,Contoury+(int)Math.floor(y),ColorScaleWidth-1,2);
245     }
246     g.setColor(Color.black);
247     g.drawString(MaxString, Contourx,+FontHeight+Contoury);
248     g.drawString(MiddleString, Contourx,+Contoury+(int)Math.round((hauteurMax+FontHeight)/2.0));
249     g.drawString(MinString, Contourx,hauteurMax-1);
250         g.setClip(2*Contourx+ColorScaleWidth,Contoury,largeurMax-1,hauteurMax-1);
251     for(int k=0;k<data.length;k++) {
252             for(int l=0;l<data[k].length;l++) {
253                 g.setColor(CS.getColor(data[k][l]));
254                 g.fillRect((int)Math.floor(l*deltax[k])+2*Contourx+ColorScaleWidth,k*deltay+Contoury,(int)Math.floor((l+1.0)*deltax[k])+Contourx,(k+1)*deltay+Contoury);
255             }
256       //modifié
257
g.setColor(this.getBackground());
258       g.fillRect((int) Math.floor(data[k].length*deltax[k])+2*Contourx+ColorScaleWidth,k*deltay+Contoury,largeurMax-1,(k+1)*deltay+Contoury);
259       //modifié
260
}
261     }
262 }
263
264
Popular Tags