1 package JSci.awt; 2 3 import java.awt.*; 4 5 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 26 public ContourPlot(double z[][]) { 27 setBackground(Color.white); 28 setData(z); 29 setContourX(1); 30 setContourY(1); 31 } 32 35 public void setData(double feed[][]) { 36 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 D; 61 for(int k=0;k<array.length;k++) { 62 for(int l=0;l<array[k].length;l++) { 63 D=new Double (1-(array[k][l]-min)/(max-min)); 64 data[k][l]=D.floatValue(); 65 } 66 } 67 } 68 rescale(); 69 } 70 73 public Dimension getPreferredSize() { 74 return getMinimumSize(); 75 } 76 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 94 public void setContourX(int k) { 95 if(k<1) 96 throw new IllegalArgumentException ("This parameter must be greater than 1 : "+k+" < 1"); 97 Contourx=k; 98 } 99 105 public void setContourY(int k) { 106 if(k<1) 107 throw new IllegalArgumentException ("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 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 ("Incorrect parameters : "+x1+", "+x2+", "+x3); 141 } 142 if(x1+x2+x3==0) { 143 throw new IllegalArgumentException ("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 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 |