JSci.awt.ContourPlot Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsci Show documentation
Show all versions of jsci Show documentation
JSci is a set of open source Java packages. The aim is to encapsulate scientific methods/principles in the most natural way possible. As such they should greatly aid the development of scientific based software.
It offers: abstract math interfaces, linear algebra (support for various matrix and vector types), statistics (including probability distributions), wavelets, newtonian mechanics, chart/graph components (AWT and Swing), MathML DOM implementation, ...
Note: some packages, like javax.comm, for the astro and instruments package aren't listed as dependencies (not available).
The newest version!
package JSci.awt;
import java.awt.*;
/**
* A contour plot AWT component.
* @author Daniel Lemire
*/
public final class ContourPlot extends DoubleBufferedCanvas {
private float data[][];
private double min, max;
private int deltay;
private int[] deltax;
private float c1=1;
private float c2=1;
private float c3=1;
private int Contourx=1;
private int Contoury=1;
private int largeurMax;
private int hauteurMax;
/**
* Constructs a contour plot.
* @param z an array of the form z[i][j] = f(xi, yj)
.
*/
public ContourPlot(double z[][]) {
setBackground(Color.white);
setData(z);
setContourX(1);
setContourY(1);
}
/**
* Sets the data plotted by this ContourPlot to the specified data.
*/
public void setData(double feed[][]) {
//invert the rows
double[][] array=new double[feed.length][];
for(int k=0;kmax)
max=array[k][l];
if(array[k][l]largeurMax) {
largeurMax=data[k].length;
}
}
return new Dimension(largeurMax+2*Contourx,data.length+2*Contoury);
}
/**
* There is a box around the graph, this
* sets how wide it will be in the horizontal direction.
* @param k width (in pixel).
* @exception IllegalArgumentException if the width is not at least one.
*/
public void setContourX(int k) {
if(k<1)
throw new IllegalArgumentException("This parameter must be greater than 1 : "+k+" < 1");
Contourx=k;
}
/**
* There is a box around the graph, this
* sets how wide it will be in the vertical direction.
* @param k width (in pixel).
* @exception IllegalArgumentException if the width is not at least one.
*/
public void setContourY(int k) {
if(k<1)
throw new IllegalArgumentException("This parameter must be greater than 1 : "+k+" < 1");
Contoury=k;
}
public void setBounds(int x,int y,int width,int height) {
super.setBounds(x,y,width,height);
rescale();
}
private void rescale() {
Dimension d=getSize();
int largeur=d.width-2*Contourx;
int hauteur=d.height-2*Contoury;
deltay=(int) Math.floor(hauteur/data.length);
hauteurMax=deltay*data.length;
deltax=new int[data.length];
largeurMax=0;
for(int k=0;klargeurMax) largeurMax=data[k].length*deltax[k];
}
redraw();
}
/**
* Set the color for the graph. Note that the
* box around the graph has this same color.
* @exception IllegalArgumentException if
* one of the parameters is not between 0 and 1.
* @exception IllegalArgumentException if
* all three arguments are set to zero.
*/
public void setColor(float x1,float x2, float x3) {
if((x1<0)||(x1>1)||(x2<0)||(x2>1)||(x3<0)||(x3>1)) {
throw new IllegalArgumentException("Incorrect parameters : "+x1+", "+x2+", "+x3);
}
if(x1+x2+x3==0) {
throw new IllegalArgumentException("You have chosen black at the specified color. This would generate a completly black graph. Please choose another color.");
}
c1=x1;c2=x2;c3=x3;
}
/**
* Paint the graph.
*/
protected void offscreenPaint(Graphics g) {
g.setColor(new Color(1f-c1,1f-c2,1f-c3));
g.drawRect(Contourx-1,Contoury-1,largeurMax,hauteurMax);
g.setClip(Contourx,Contoury,largeurMax-1,hauteurMax-1);
for(int k=0;k