KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > awt > ContourPlot


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

9 public final class ContourPlot extends DoubleBufferedCanvas {
10     private float data[][];
11     private double min, max;
12     private int deltay;
13     private int[] deltax;
14         private float c1=1;
15     private float c2=1;
16     private float c3=1;
17     private int Contourx=1;
18     private int Contoury=1;
19     private int largeurMax;
20     private int hauteurMax;
21
22         /**
23         * Constructs a contour plot.
24         * @param z an array of the form <code>z[i][j] = f(x<sub>i</sub>, y<sub>j</sub>)</code>.
25         */

26     public ContourPlot(double z[][]) {
27                 setBackground(Color.white);
28                 setData(z);
29                 setContourX(1);
30                 setContourY(1);
31     }
32         /**
33         * Sets the data plotted by this ContourPlot to the specified data.
34         */

35     public void setData(double feed[][]) {
36     //invert the rows
37
double[][] array=new double[feed.length][];
38                 for(int k=0;k<array.length;k++) {
39                         array[k]=feed[array.length-k-1];
40                 }
41         min=array[0][0];
42         max=array[0][0];
43         data=new float[array.length][];
44         for(int k=0;k<array.length;k++) {
45             data[k]=new float[array[k].length];
46             for(int l=0;l<array[k].length;l++) {
47                 if(array[k][l]>max)
48                     max=array[k][l];
49                 if(array[k][l]<min)
50                     min=array[k][l];
51             }
52         }
53         if(max==min) {
54             for(int k=0;k<array.length;k++) {
55                 for(int l=0;l<array[k].length;l++) {
56                     data[k][l]=1;
57                 }
58             }
59         } else {
60             Double JavaDoc D;
61             for(int k=0;k<array.length;k++) {
62                 for(int l=0;l<array[k].length;l++) {
63                     D=new Double JavaDoc(1-(array[k][l]-min)/(max-min));
64                     data[k][l]=D.floatValue();
65                 }
66             }
67         }
68         rescale();
69     }
70         /**
71         * Returns the preferred size of this component.
72         */

73     public Dimension getPreferredSize() {
74         return getMinimumSize();
75     }
76         /**
77         * Returns the minimum size of this component.
78         */

79     public Dimension getMinimumSize() {
80         int largeurMax=data[0].length;
81         for(int k=1;k<data.length;k++) {
82             if(data[k].length>largeurMax) {
83                 largeurMax=data[k].length;
84             }
85         }
86         return new Dimension(largeurMax+2*Contourx,data.length+2*Contoury);
87     }
88         /**
89         * There is a box around the graph, this
90         * sets how wide it will be in the horizontal direction.
91         * @param k width (in pixel).
92         * @exception IllegalArgumentException if the width is not at least one.
93         */

94     public void setContourX(int k) {
95         if(k<1)
96                         throw new IllegalArgumentException JavaDoc("This parameter must be greater than 1 : "+k+" < 1");
97         Contourx=k;
98     }
99         /**
100         * There is a box around the graph, this
101         * sets how wide it will be in the vertical direction.
102         * @param k width (in pixel).
103         * @exception IllegalArgumentException if the width is not at least one.
104         */

105     public void setContourY(int k) {
106         if(k<1)
107             throw new IllegalArgumentException JavaDoc("This parameter must be greater than 1 : "+k+" < 1");
108         Contoury=k;
109     }
110     public void setBounds(int x,int y,int width,int height) {
111         super.setBounds(x,y,width,height);
112         rescale();
113     }
114
115     private void rescale() {
116         Dimension d=getSize();
117         int largeur=d.width-2*Contourx;
118         int hauteur=d.height-2*Contoury;
119         deltay=(int) Math.floor(hauteur/data.length);
120         hauteurMax=deltay*data.length;
121         deltax=new int[data.length];
122         largeurMax=0;
123         for(int k=0;k<data.length;k++) {
124             deltax[k]=(int) Math.floor(largeur/data[k].length);
125             if(data[k].length*deltax[k]>largeurMax) largeurMax=data[k].length*deltax[k];
126         }
127         redraw();
128     }
129
130         /**
131         * Set the color for the graph. Note that the
132         * box around the graph has this same color.
133         * @exception IllegalArgumentException if
134         * one of the parameters is not between 0 and 1.
135         * @exception IllegalArgumentException if
136         * all three arguments are set to zero.
137         */

138     public void setColor(float x1,float x2, float x3) {
139         if((x1<0)||(x1>1)||(x2<0)||(x2>1)||(x3<0)||(x3>1)) {
140             throw new IllegalArgumentException JavaDoc("Incorrect parameters : "+x1+", "+x2+", "+x3);
141         }
142     if(x1+x2+x3==0) {
143       throw new IllegalArgumentException JavaDoc("You have chosen black at the specified color. This would generate a completly black graph. Please choose another color.");
144     }
145         c1=x1;c2=x2;c3=x3;
146     }
147         /**
148         * Paint the graph.
149         */

150     protected void offscreenPaint(Graphics g) {
151         g.setColor(new Color(1f-c1,1f-c2,1f-c3));
152         g.drawRect(Contourx-1,Contoury-1,largeurMax,hauteurMax);
153         g.setClip(Contourx,Contoury,largeurMax-1,hauteurMax-1);
154                 for(int k=0;k<data.length;k++) {
155             for(int l=0;l<data[k].length;l++) {
156                                 g.setColor(Color.getHSBColor(data[k][l],1f,1f));
157                 g.fillRect(l*deltax[k]+Contourx,k*deltay+Contoury,(l+1)*deltax[k]+Contourx,(k+1)*deltay+Contoury);
158             }
159         }
160     }
161 }
162
163
Popular Tags