import java.awt.*;
import javax.swing.*;
import java.text.*;

public class ColorMapPanel extends JPanel {
    double[][] data;
 
    ColorScheme colorScheme;

    int borderheight = 1, borderwidth = 1;
    int spacing=1, nx, ny;
    double stretch=1, level=0;

    boolean autoscaled=false;

    double min, max;
    DecimalFormat df = new DecimalFormat("0.###");

    public ColorMapPanel() {
	setDefaults();
    }

    public ColorMapPanel(double[][] map) {
	data=map;
	setDefaults();
    }

    public void setDefaults() {
	colorScheme=new CameraLike();
	setRange(0, 1);
    }
	
    public void paintComponent(Graphics g) {
	super.paintComponent(g);
	draw(g);
    }

    public void draw(Graphics g){
	nx = data[0].length;
	ny = data.length;

	float dw = (float) (getWidth()-2*borderwidth)/nx;
	float dh = (float) (getHeight()-2*borderheight)/ny;
	
	int nextw=0, nexth=0;

	for(int i=0; i<ny; i++) {
	    for(int j=0; j<nx; j++) {
		int w=(int) (j*dw+0.5) + borderwidth;
		int h=(int) (i*dh+0.5) + borderheight;
		nextw=(int) ((j+1)*dw+0.5) + borderwidth;
		nexth=(int) ((i+1)*dh+0.5) + borderheight;
		int Dw=nextw-w-spacing;
		int Dh=nexth-h-spacing;
		
		float value = (float) (stretch*(data[i][j]-level));
		g.setColor(colorScheme.getColor(value));
		g.fillRect(w, h, Dw, Dh );
		
		Thread.yield();
	    }
	}
	
    }
    
    public void setRange(double min, double max) {
	level=min;
	stretch=1.0/(max-min);
    }

    public void setBorder(int xborder, int yborder) {
	borderwidth = xborder;
	borderheight = yborder;
    }
	
}
   

// 02.11.08  Added crosshairs



